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
For more information about Espresso setup, see https://developer.android.com/training/testing/espresso/setup
build.gradle
file. This is usually not the top-level build.gradle
file but app/build.gradle
. Add the following lines inside dependenciesandroidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
androidTestImplementation 'androidx.test:runner:1.4.0'
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.
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 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()));
}
}
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:
androidx.test.runner.AndroidJUnitRunner
Option 2: Click Run button to execute test directly
Now, the environment should be properly configured to build.
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/
Go to Console and choose your project.
Select Automated Testing > Automation > Create Test on the left sidebar.
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.
Upload and choose Test Suite
Upload and choose your Espresso test suite, support .apk. You can also use WeTest sample test suite.
Choose Devices
You have three options to choose devices
Click Submit to run your test.
View Test Report in Test Analysis