【问题标题】:Tell Espresso to run specific tests on an emulator告诉 Espresso 在模拟器上运行特定测试
【发布时间】:2017-08-26 00:07:25
【问题描述】:

我使用 Espresso 进行了 Android 仪器测试。由于使用了 LinkedIn 的 TestButler (https://github.com/linkedin/test-butler) 库,我的一些测试必须在模拟器上运行。这个库为特定的测试运行切换 wifi/gsm,这就是为什么这些测试必须在模拟器上运行。

我的问题是 - 我可以注释任何特定测试以在模拟器上运行,同时让其他测试在真实设备上运行吗?

谢谢

【问题讨论】:

    标签: android junit android-espresso android-instrumentation


    【解决方案1】:

    是的,您可以使用 @ConditionalIgnore 注释,如 http://www.codeaffine.com/2013/11/18/a-junit-rule-to-conditionally-ignore-tests/ 中所述。

    你会有类似的东西

    public class SomeTest {
      @Rule
      public ConditionalIgnoreRule rule = new ConditionalIgnoreRule();
    
      @Test
      @ConditionalIgnore( condition = NotRunningOnEmulator.class )
      public void testSomething() {
        // ...
      }
    }
    
    public class NotRunningOnEmulator implements IgnoreCondition {
      public boolean isSatisfied() {
        return !Build.PRODUCT.startsWith("sdk_google");
      }
    }
    

    编辑

    对于这种检测设备或模拟器的特定情况,您还可以使用@RequiresDevice

    【讨论】:

    • 这条规则和 Apache 的规则有什么区别:issues.apache.org/jira/browse/GEODE-167
    【解决方案2】:

    我找到的最直接的解决方案是使用 JUnit Assume API:http://junit.org/junit4/javadoc/4.12/org/junit/Assume.html

    所以,在只能在模拟器上运行的测试方法中,我放了这段代码:

    Assume.assumeTrue("This test must be run in an emulator!", Build.PRODUCT.startsWith("sdk_google"));
    

    这会导致上述测试在不在模拟器上运行时被忽略,并在运行窗口中显示一个方便的错误消息:


    如您所见,其他两个测试顺利通过(绿色),整个测试套件能够运行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-15
      • 2012-11-09
      • 1970-01-01
      相关资源
      最近更新 更多