如何在同一活动中同步运行Espresso Test Suite测试类?

时间:2019-10-25 18:04:12

标签: java android junit4 android-espresso android-instrumentation

我想同步运行一组测试。理想情况下,我希望将这些测试集模块化为单独的类,因为将它们组合成1个类对于文件来说太大了。例如,我有以下测试套件文件:

@RunWith(Suite.class)
@Suite.SuiteClasses({
        LoginTest.class,
        AddItemsTest.class,
        ItemTest.class,
        ArchiveTest.class,
        LogoutTest.class
})
public class FullTest{}

这些测试用例按顺序运行,但是我对继续在这些测试用例之间使用相同的上下文和活动感到困惑。我在运行测试找不到活动时遇到错误...

E/TestRunner: failed: runAddItems(com.XXX.XXX.functional.TestSuite.AddItemsTest)
    ----- begin exception -----
E/TestRunner: java.lang.RuntimeException: No activities found. Did you forget to launch the activity by calling getActivity() or startActivitySync or similar?
        at androidx.test.espresso.base.RootViewPicker.waitForAtLeastOneActivityToBeResumed(RootViewPicker.java:176)
        at androidx.test.espresso.base.RootViewPicker.get(RootViewPicker.java:88)
        at androidx.test.espresso.ViewInteractionModule.provideRootView(ViewInteractionModule.java:77)
        at androidx.test.espresso.ViewInteractionModule_ProvideRootViewFactory.provideRootView(ViewInteractionModule_ProvideRootViewFactory.java:37)
        at androidx.test.espresso.ViewInteractionModule_ProvideRootViewFactory.get(ViewInteractionModule_ProvideRootViewFactory.java:27)
        at androidx.test.espresso.ViewInteractionModule_ProvideRootViewFactory.get(ViewInteractionModule_ProvideRootViewFactory.java:10)
        at androidx.test.espresso.base.ViewFinderImpl.getView(ViewFinderImpl.java:63)
        at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:280)
        at androidx.test.espresso.ViewInteraction$2.call(ViewInteraction.java:272)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6949)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
    ----- end exception -----

在上一个测试用例中放弃的工作流程中,我需要做些什么具体的事情吗?

1 个答案:

答案 0 :(得分:0)

我不认为这是一个有效的解决方案,但我知道了。我只是创建了一个单独的Test类,并在测试中创建了它的一个实例。