Suite类没有使用junit4 android在espresso中运行第二个测试类

时间:2017-04-03 10:35:53

标签: android android-espresso

我在android中使用espresso和ui自动化编写了我的测试用例。现在当我运行我的两个不同类的测试用例时。它总是给出错误

  

java.lang.RuntimeException:找不到任何活动。您是否忘记通过调用getActivity()或startActivitySync或类似方法来启动活动?

并且不会运行MainActivity2Test课程。我无法理解我做错了什么。搜索有关此错误但无法找到任何解决方案。请帮帮我。

AllTestsSuite.java

@RunWith(Suite.class)
@Suite.SuiteClasses ({
        MainActivityTest.class,
        MainActivity2Test.class,

})
public class AllTestsSuite {

}

MainActivity.java

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@RunWith(AndroidJUnit4.class)
public class MainActivityTest {
   UiDevice device;

    @Rule
    public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule(MainActivity.class, true, false);

   @Test
    public void test1_doLogin(){
        device = UiDevice.getInstance(getInstrumentation());
        device.pressHome();
        device.wait(hasObject(desc("Apps")), 3000);
        UiObject2 appsButton = device.findObject(desc("Apps"));
        appsButton.click();
        UiObject2 appsButton1 = device.findObject(desc("MyApp"));
        appsButton1.click();
test2_emaillPass();
}
 @Test
    public void test2_emaillPass(){
}

}

MainActivity2Test.java

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@RunWith(AndroidJUnit4.class)
public class MainActivity2Test {
     UiDevice device;
    @Rule
    public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule(MainActivity.class, true, false);

    @Test
    public  void test3_productFamily(){
       onView(withId(R.id.list)).perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
       test4_Product();


    }
    @Test
    public  void test4_Product() {
        device = UiDevice.getInstance(getInstrumentation());
        onView(withId(R.id.list)).perform(RecyclerViewActions.actionOnItemAtPosition(0, click()));
        device.wait(hasObject(By.text("First product")), 4000);
        test5_Detail();
    }

1 个答案:

答案 0 :(得分:0)

这一行是你的问题:

@Rule
public ActivityTestRule<MainActivity> activityRule = new ActivityTestRule(MainActivity.class, true, false);

最后一个false参数指出在测试开始时是否应该启动活动,请参阅方法签名:

public ActivityTestRule(Class<T> activityClass, boolean initialTouchMode,
        boolean launchActivity)

因此,您要么将最后一个标记更改为true,要么在每个测试中手动启动您的活动,如下所示:

mActivityRule.launchActivity(null);