Search code examples
androidjunitandroid-espressoandroid-instrumentation

Tell Espresso to run specific tests on an emulator


I have Android instrumentation tests with Espresso. Some of my tests must be run on an emulator - due to using LinkedIn's TestButler (https://github.com/linkedin/test-butler) library. This library toggles wifi/gsm for specific test runs, and that is why these tests must be run on an emulator.

My question is - can I annotate any specific tests to run on an emulator, while having the other tests run on a real device?

Thanks


Solution

  • The most straightforward solution I found is to use JUnit Assume API: http://junit.org/junit4/javadoc/4.12/org/junit/Assume.html

    So, inside the test methods that can only be run on an emulator, I put this code:

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

    This results in the said test being ignored when it isn't run on an emulator, and a handy error msg in the run window: enter image description here


    As you can see, the other two tests passed fine (in the green), and the entire test suite was able to run.