Running Espresso tests

Prerequisite

Espresso tests can be run via the command line either with Gradle or with an Android Debug Bridge (adb) shell.

For these tests to execute, an emulator/physical device must be running on the host machine.

To list the active devices, one runs the below command

~/ adb devices

The command should list all the available devices

Run tests via command line/terminal

To run the instrumented tests, navigate to the root folder of the project. Then run the following command for each of the scenario

  1. To run all the instrumentation tests for all the flavours

./gradlew :<project_name>:clean :<project_name>:connectedAndroidTest coveralls --stacktrace

 

2. To run all the instrumentation tests but for a particular flavor

./gradlew :<project_name>:clean :<project_name>:connected<flavour>AndroidTest coveralls --stacktrace

3. To run a given class of the instrumentation tests for all flavours

./gradlew -Pandroid.testInstrumentationRunnerArguments.class=<classpath> connectedAndroidTest

4. To run a given class of the instrumentation tests for a given flavour. This has one advantage in that the tests can be organized into a TestSuite and run from a single class in a well organized format to maximize the formatted use of test data and a good end-user experience

./gradlew -Pandroid.testInstrumentationRunnerArguments.class=<classpath> connected<flavour>AndroidTest

Running tests via terminal has a major advantage of presenting the tester with a nice .html formatted report of the tests run, providing extra details per class and on failed tests. This summary can be found on the ../<project_folder>/../build/reports/androidTests/connected/ folder for a project without flavours and ../<project_folder>/../build/reports/androidTests/connected/flavors/<flavour>/ for a given flavour

Sample reports

A test summary report



A summary report per class


Run an Espresso test locally

To run an Espresso test via the Android Studio IDE, use the Project window on the left side of the IDE :

  1. Open the desired app module folder and navigate to the test you want to run. The test’s location depends on the location of your instrumentation test root i.e module-name/src/androidTest/java/ and the package name of the launched activity. The following examples demonstrate where a test would save for the Notes testing app:

    • If you are using the Android view within the window, navigate to java > com.example.username.appname (androidTest).

    • If you are using the Project view inside the window, navigate to src > androidTest > java > com.example.username.appname within the module folder.

  2. Right-click on the test and click Run ‘testName.’

    • Alternatively, you can open the test file and right-click on the generated test class or method. Read more about how to run tests on the Test Your App page.

  3. In the Select Deployment Target window, choose the device on which you want to run the test. If necessary, create a new Android Virtual Device. Click OK.

Monitor the progress of your test in the Run window at the bottom of the IDE. Android Studio runs a full build of your project and opens a tab with the name of your test in the Run window, as shown in figure 3. You can check whether your test passes or fails in this tab, as well as how long the test took to run. When the test finishes, the tab will log "Tests ran to completion."