运行单元测试时运行的Junit5和Instrumentation测试

时间:2019-03-27 22:05:50

标签: android unit-testing testing instrumentation junit5

现在,当我运行单元测试时,我已将测试升级到junit5,它也运行了仪器测试,这当然会导致错误,因为它们不在模拟器中运行。如何停止运行它们?我可以使用@RunWith(AndroidJUnit4 :: class)解决一些错误,但仍然有一个错误,这让我发疯了

这是错误,但是,当我单击“运行”时,我不理解为什么现在正在运行仪器测试?

java.lang.NoClassDefFoundError: androidx/test/espresso/matcher/ViewMatchers

    at com.magtec.magtecmcc.MainActivityTest.testLaunch(MainActivityTest.kt:46)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at androidx.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:531)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.robolectric.internal.SandboxTestRunner$2.lambda$evaluate$0(SandboxTestRunner.java:256)
    at org.robolectric.internal.bytecode.Sandbox.lambda$runOnMainThread$0(Sandbox.java:89)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.ClassNotFoundException: androidx.test.espresso.matcher.ViewMatchers
    at org.robolectric.internal.bytecode.SandboxClassLoader.getByteCode(SandboxClassLoader.java:164)
    at org.robolectric.internal.bytecode.SandboxClassLoader.maybeInstrumentClass(SandboxClassLoader.java:119)
    at org.robolectric.internal.bytecode.SandboxClassLoader.lambda$findClass$0(SandboxClassLoader.java:112)
    at org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:53)
    at org.robolectric.internal.bytecode.SandboxClassLoader.findClass(SandboxClassLoader.java:111)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 19 more

1 个答案:

答案 0 :(得分:0)

多个选项首先分别用于测试:

  • @忽略是否要完全禁用
  • 输入承担条件(条件);停用运行时条件下的测试

  • ,然后您还可以执行以下操作: 首先开始测试 然后点击顶部的下拉菜单,显示您的运行配置 点击“编辑配置”

    • 将配置文件夹更改为不包含特定测试的文件夹
    • 或将测试类型切换为模式,并希望您对排除的测试文件有某种命名约定,然后您可以编写仅包含所需的正则表达式模式