簡體   English   中英

告訴Espresso在模擬器上運行特定的測試

[英]Tell Espresso to run specific tests on an emulator

我在Espresso上進行了Android工具測試。 由於使用了LinkedIn的TestButler( https://github.com/linkedin/test-butler )庫,我的某些測試必須在模擬器上運行。 該庫將wifi / gsm切換為特定的測試運行,這就是為什么這些測試必須在模擬器上運行的原因。

我的問題是-我可以注釋要在模擬器上運行的任何特定測試,而讓其他測試在真實設備上運行嗎?

謝謝

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

你會有類似的東西

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 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"));

當不在模擬器上運行該測試時,將導致上述測試被忽略,並且在運行窗口中出現方便的錯誤消息: 在此處輸入圖片說明


如您所見,其他兩個測試通過了(綠色),並且整個測試套件都可以運行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM