如何使用最新的 ActivityScenarioRule/ActivityScenario 放置额外数据?[Espresso/Jetpack]

Akb*_*SSS 10 android automated-tests android-testing android-espresso

添加了新的类来测试活动,例如ActivityScenarioRuleActivityScenario 并且没有文档说明如何在您想要放置一些额外数据时使用它们。

现在我找到了两种工作方式,简而言之:

1) 使用 ActivityScenarioRule 并使用#onActivity将您的额外内容添加到带有 @Before 注释的方法中。但是,某些测试用例会有不必要的数据。

2) 其次,当你不需要额外的时候使用 ActivityScenario 和#launch(Class activityClass),当你想要额外的时候使用#launch(Intent startActivityIntent)但是,这一次我失去了使用#onActivity的能力,可以在所有测试用例中通用

PS 这是我第一次进行 android 测试 :)

小智 9

我能够通过在我的测试类中使用静态 Intent 来解决这个问题。这样,我的意图在我将它传递给我的 ActivityScenarioRule 之前被实例化。

我的浓缩咖啡测试课:

package your.package.tests.example;

import android.content.Intent;
import android.os.Bundle;

import androidx.test.core.app.ApplicationProvider;
import androidx.test.espresso.assertion.ViewAssertions;
import androidx.test.ext.junit.rules.ActivityScenarioRule;
import androidx.test.filters.LargeTest;
import androidx.test.runner.AndroidJUnit4;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import static androidx.test.espresso.Espresso.onView;
import static androidx.test.espresso.matcher.ViewMatchers.*;

@RunWith(AndroidJUnit4.class)
@LargeTest
public class TestExample {

    static Intent intent;
    static {
        intent = new Intent(ApplicationProvider.getApplicationContext(), YourActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("YOUR_BUNDLE_TAG", "YOUR_VALUE");
        intent.putExtras(bundle);
    }

    @Rule
    public ActivityScenarioRule<YourActivity> activityScenarioRule = new ActivityScenarioRule<>(intent);

    @Test
    public void yourTest() {
        onView(withId(R.id.simple_text)).check(ViewAssertions.matches(isDisplayed()));
    }
}
Run Code Online (Sandbox Code Playgroud)

我的 espresso gradle 依赖项:

dependencies {
    // Testing-only dependencies
    androidTestImplementation 'androidx.test:core:1.2.1-alpha02'
    androidTestImplementation 'androidx.test:core-ktx:1.2.1-alpha02'
    androidTestImplementation 'androidx.test.ext:junit:1.1.2-alpha02'
    androidTestImplementation 'androidx.test.ext:junit-ktx:1.1.2-alpha02'
    androidTestImplementation 'androidx.test:runner:1.3.0-alpha02'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0-alpha02'
    androidTestImplementation 'androidx.test.espresso:espresso-intents:3.3.0-alpha02'
    ...
}
Run Code Online (Sandbox Code Playgroud)

参考:Medium 教程如何使用 espresso 测试:https : //medium.com/@heitorcolangelo/testes-no-android-com-espresso-parte-1-8d739672a235


Vai*_*ios 6

我个人就是这样做的

lateinit var activityScenario: ActivityScenario<MyActivity>

@After
fun tearDown() {
    activityScenario.close()
}

@Test
fun myTest() {
    val intent = Intent(ApplicationProvider.getApplicationContext(), MyActivity::class.java)
    intent.putExtra("key", "value") //obviously use a const for key
    activityScenario = ActivityScenario.launch<MyActivity>(intent)

    activityScenario.onActivity {
        //whatever you like
    }
}
Run Code Online (Sandbox Code Playgroud)