Preparing Espresso Tests for Upload

The steps necessary to prepare an app and its corresponding test suite for upload to App Center vary depending on the test framework being used. This document provides instructions for preparing Espresso tests for upload to Test Cloud. For guidance on authoring Espresso tests, see the Espresso documentation.

1. Changes to the build system

Add the JCenter Maven repository. Make sure you have a jcenter() entry in the build.gradle in your project root directory:

allprojects {
    repositories {
        jcenter()
    }
}

Add the following dependency in your app module's build.gradle file:

androidTestCompile('com.microsoft.appcenter:espresso-test-extension:1.3')

Starting with Gradle 3.0, androidTestCompile is deprecated, you should use androidTestImplementation instead.

This adds the Test Cloud Espresso Extensions as a dependency to your project, which ensures that the ReportHelper is available at compile time. The ReportHelper enables the label feature. See Step 3 for more detail on the label feature.

2. Changes to the tests

Step 1 - Add imports

Import these packages into your test classes:

import android.support.test.runner.AndroidJUnit4;
import com.microsoft.appcenter.espresso.Factory;
import com.microsoft.appcenter.espresso.ReportHelper;

Step 2 - Instantiate the ReportHelper

Insert this declaration in each of your test classes:

@Rule
public ReportHelper reportHelper = Factory.getReportHelper();

Step 3 - Update your test cases

Using the helper still allows you to run your tests locally without additional modifications, but enables you to label test steps in your test execution using reportHelper.label("myTestStepLabel"). The label text is used to navigate the test steps and corresponding screenshots in the test report.

A recommended practice is to have a call to label in the @After method, this includes a screenshot of the app final state in the test report. The screenshot is taken, even if a test is failing, and often provides valuable information as to why it does so. An example @After method for a test could look like this:

@After
public void TearDown(){
    reportHelper.label("Stopping App");
}

To build the project and test apk files, run each of the following commands.

./gradlew assembleDebug
./gradlew assembleDebugAndroidTest

Note

You system path variable must include access to your machine's Java installation.

Once a test suite has been instrumented with the Test Cloud extensions, upload it to Test Cloud.