Espresso 2.0
Recently Espresso 2.0 was released making it now part of the Android Support Library. This was announced on the android dev blog.
Setup Guide
With that they also linked an updated setup guide. There you can find instructions to configure from scratch or to update your existing espresso config for 2.0.
Other Tips
Changes are the above 2 links contain all information you need. If not I have listed some common mistakes below
Upgrade Android Studio to 1.0.*
Start by upgrading your android Studio build. You should be able to get at least 1.0 from the stable builds channels (=default). So just use the menu option Android Studio > Check for updates... .
To get the latest from the latest you can also go into Preferences, search for updates and change the channel to canary channel.
Update Android Support Library to v 11+
Espresso was included in the Support library from version 11 so you have to get at least that version. Check for updates using the Android SDK manager. The Support Library is within the Extras tree at the bottom.
New dependencies and namespace
If upgading from an older espresso release you'll have to update the dependencies and the namespace. For new projects just add these to the dependencies
in your build.gradle
file.
dependencies {
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1'
}
And since the namespace changed you'll have to update all imports:
android.support.test.espresso
Note that it's easier to use static imports. Some commonly used imports as an example:
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
import static android.support.test.espresso.matcher.ViewMatchers.withContentDescription;
For asserts use hamcrest, again some examples:
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.startsWith;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.equalToIgnoringCase;
import static org.hamcrest.Matchers.equalToIgnoringWhiteSpace;
Instrumentation runner
The test runner needs to be configured in both your build.gradle
file within defaultConfig and the run configuration used to launch your tests from Android Studio.
defaultConfig {
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
And in your run configuration use this as instrumentation runner (full class name only):
android.support.test.runner.AndroidJUnitRunner
Example test case
And an example test case to finish with. Note that MainActivity
is your actvitiy you want to test. The tests themselves are public methods that start with test, like testListGoesOverTheFold
in the below example.
@LargeTest
public class HelloWorldEspressoTest extends ActivityInstrumentationTestCase2<MainActivity> {
public HelloWorldEspressoTest() {
super(MainActivity.class);
}
@Override
public void setUp() throws Exception {
super.setUp();
getActivity();
}
public void testListGoesOverTheFold() {
onView(withText("Hello world")).check(isDisplayed());
}
}
For more information on writing tests visit the espresso start guide.