Espresso v2.0 seems to work fine with both Android SDK on Eclipse and Android Studio, but as Google recommends Android Studio we’re providing the instructions here for Android Studio. Let’s check it out – it’s a great promising framework for your app testing! And it’s FAAAAST!
In order to get Espresso v2.0 properly configured, follow these steps:
1) Open the SDK Manager (under Tools -> Android -> SDK Manager):
2) Ensure you have the Android Support Repository installed. This can be found under Extras.
build.gradle file (under file structure
app/build.gradle) in a text editor.
4) Add the following piece of code inside dependencies:
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0' androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
In addition, if your application uses functionalities from
espresso-contrib, add the following line inside dependencies:
5) Configure the instrumentation runner with the
testInstrumentationRunner. It should look like this:
More information about Espresso Core Resource can be found in this Javadoc.
The New Test Runner – AndroidJUnitRunner
The AndroidJUnitRunner is a new unbundled test runner for Android, which is part of the Android Support Test Library and can be downloaded via the Android Support Repository. The new runner contains all improvements of GoogleInstrumentationTestRunner and adds more features, such as:
- JUnit4 support
- Instrumentation Registry for accessing Instrumentation, Context and Bundle Arguments
- Test Filters @SdkSupress and @RequiresDevice
- Test timeouts
- Sharding of tests
- RunListener support to hook into the test run lifecycle
- Activity monitoring mechanism ActivityLifecycleMonitorRegistry
The User Guide for AndroidJUnitRunner can be found here.
How to use Espresso v2.0 tests on Bitbar Testing
Let’s take a look at how to run your Espresso scripts. Follow these few steps and you should get your tests up and running
First, you have to create a test configuration in Android Studio. Open Run -> Edit Configurations. Click the ‘+’ button on the left-top corner to add new configuration as Android Tests. Give a name to test configuration (here ‘New Test Config’), select module (here ‘app’) and specify the instrumentation runner (here ‘android.support.test.runner.AndroidJUnitRunner’). Your open Run/Debug Configuration dialog should look at this:
Now, your environment should be properly configured to build instrumentation packages.
Once you have configured the test project you can build it and upload it into Bitbar Testing. Under test run creation, create a standard “Android” project, upload your application APK, upload your test APK, select device group as you wish, and under Advanced configuration (step #4 in Bitbar Testing) define the custom test runner in this view, as shown on the screenshot below:
NOTE! The above dialog doesn’t show the whole string but it should be naturally – android.support.test.runner.AndroidJUnitRunner
Start your test and the test results will be ready in a few minutes!
You can now run your tests directly from Android Studio using Bitbar’s Gradle plugin! This awesome plugin eliminates the need for any manual effort in order to build and get your APK up and running on physical devices. Check it out – it’s simply awesome!
Also, if you are interested to check out more good stuff about Espresso, how to write tests with it, how to fully utilize our Bitbar Testing devices in your testing and many other test automation specific detail, do check out our webinar and slide deck about this great-potential framework!