Pricing
Espresso
Help Documents menu

Espresso

Updated on: 2024-09-03 15:19

Espresso, one of the most popular test automation frameworks for Android App testing.

Developers could use Espresso to write concise, beautiful, and reliable Android UI tests.

For more information, see https://developer.android.com/training/testing/espresso

Environment Setup

For more information about Espresso setup, see https://developer.android.com/training/testing/espresso/setup

  1. In Android Studio, go to Tools > Android > SDK Manager.
  2. Make sure that Android SDK is installed.
  3. Open your app’s build.gradle file. This is usually not the top-level build.gradle file but app/build.gradle. Add the following lines inside dependencies
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation 'androidx.test:runner:1.4.0'
  1. Add to the same build.gradle file the following line in android.defaultConfig :
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

The AndroidJUnitRunner class is a JUnit test runner that lets you run instrumented JUnit 4 tests on Android devices, the test runner handles loading your test package and the app under test to a device, running your tests, and reporting test results.

  1. Gradle build file code sample
plugins {
    id 'com.android.application'
}
	
android {
    compileSdkVersion 30
    buildToolsVersion "30.0.3"
	
    defaultConfig {
        applicationId "com.tencent.wetestdemo"
        minSdkVersion 16
        targetSdkVersion 30
        versionCode 3
        versionName "1.2"
        
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
	   }
}
	
dependencies {
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
    androidTestImplementation 'androidx.test:runner:1.4.0'
}

Create Test

Create tests in project, Android Studio creates tests by default in src/androidTest/java/com.example.package/

Sample Code using JUnit4

@RunWith(AndroidJUnit4.class)
@LargeTest
public class HelloWorldEspressoTest {

    @Rule
    public ActivityScenarioRule<MainActivity> activityRule =
            new ActivityScenarioRule<>(MainActivity.class);

    @Test
    public void listGoesOverTheFold() {
        onView(withText("Hello world!")).check(matches(isDisplayed()));
    }
}

Run Test in Android Studio

Before building your project, we recommend running your test in local IDE.

After running your test, Test Suite(.apk) will be generated in app/build/outputs/apk/androidTest/debug/

Option 1: Add test configuration and run

To create a test configuration in Android Studio, complete the following steps:

  1. Open Run > Edit Configurations.
  2. Add a new Android Tests configuration.
  3. Choose a module.
  4. Add a specific instrumentation runner: androidx.test.runner.AndroidJUnitRunner
  5. Run the newly created configuration.

Option 2: Click Run button to execute test directly
Execute Test

Now, the environment should be properly configured to build.

Execute Espresso Test on WeTest

  1. Build your App and Test Suite(Android Studio)

    Click Build > Build Bundle(s)/APK(s) > Build APK(s)

    Default path of App is app/build/outputs/apk/debug/

    Default path of Test Suite(.apk) is app/build/outputs/apk/androidTest/debug/

  2. Go to Console and choose your project.

  3. Select Automated Testing > Automation > Create Test on the left sidebar.

  4. Upload and choose App

    Upload target App file using App Upload. Support .apk and .aab. You can also use WeTest sample App.Then click Next step to continue.

  5. Upload and choose Test Suite

    Upload and choose your Espresso test suite, support .apk. You can also use WeTest sample test suite.

  6. Choose Devices

    You have three options to choose devices

    • Option1:Use random available devices
      you can choose random devices no matter what model or feature, we will pick available devices to run your test automatically. The maximum device quantity is 50.
    • Option2: Use existing device group
      • If you alreay have a device group, you can select it to run your test.
      • You can manage your device groups here.
    • Option3: Use chosen devices
      • You are able to choose devices from the list of available devices manually.
  7. Click Submit to run your test.

  8. View Test Report in Test Analysis