Espresso and Android Studio


Required
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 'com.android.support:support-annotations:21.0.3'
// Testing-only dependencies
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'

To defaultConfig:
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

To packagingOpptions:
exclude'LICENSE.txt'
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 android.support.test.espresso.Espresso.onView.

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.
Previous
Next Post »
Thanks for your comment