让 Espresso 在模拟器上进行 运行 特定测试

Tell Espresso to run specific tests on an emulator

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

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

谢谢

是的,您可以使用 @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

我找到的最直接的解决方案是使用 JUnit 假设 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"));

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


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