Espresso and Android Studio

To have installed Android Studio 1.0.1 or up.

Installing Espresso in Android Studio

1. Over your project created open the build.gradle file and add

To dependencies:
// Apps dependencies, including test
compile ''
// Testing-only dependencies
androidTestCompile ''
androidTestCompile ''

To defaultConfig:
testInstrumentationRunner ""

To packagingOpptions:
Fig 1. Example build.gradle file.

2. Create the Class test having in mind the next features
The class extend that ActivityInstrumentationTestCase2 class.
The package that contain these classes test is the package defined in AndroidManifiest.xml.
The class uses the static references i.e. import static

Fig 2. Example Test file.

In this case I have defined one operation test that validate to show the text "Hello World!" on the view of the activity. That is a simple test, remember that you can visit Espresso site to define sophisticated  test.

3. To define and setting up of the Test case
Sets up your test using "Edit configuration".
In Android test add the new test case (+).
Define the name for example Espresso test.
Define the class test that in this case is FunctionalInstrumentationTest.
Specific the instrumentation runner like as AndroidJUnitRunner.

Fig 3. Example setting up the test case.

I recommend  enable "Show chooser dialog" and always to use a device real. To be honest, I prefer always work with a device real and don't use the emulator.

4. Running the Test case created.
Choose your test case crated and click over Run option, then choose your device connected and automatically the test is going to run.

Fig 4. Example running the test case.

Finally, you will see the results and also export it using Android Studio options. You can also checking the logs from the test case.

Fig 5. Export the results form your test case.

And that's it. If you have any question, please let me know. You can download the example project in GitHub.
Good travel.
Next Post »
Thanks for your comment