- Write Once, Run Everywhere Tests on Android
- Originally by Jonathan Gerrish
- The Nuggets translation Project
- Permanent link to this article: github.com/xitu/gold-m…
- Translator: Rickon
- Proofreader: Xiaxiayang
At this year’s Google I/O conference, we launched the AndroidX Test as part of Jetpack. Today, we are pleased to announce the release of the final VERSION of V1.0.0 alongside Robolectric V4.0. As part of version 1.0.0, all AndroidX tests are now open source.
AndroidX Test provides common testing APIs across Test environments, including instrumentation and Robolectric tests. It includes existing Android JUnit 4 support, Espresso View interaction libraries, and several new key testing APIs. These APIs can be used for instrumentation testing on real and virtual devices. Starting with Robolectric 4.0, they are also available for native JVM testing.
Considering the following usage scenario, we launch the login page, enter the correct user name and password, and make sure to go to the home screen.
@RunWith(AndroidJUnit4::class)
class LoginActivityTest {
@Test fun successfulLogin() {
// GIVEN
val scenario =
ActivityScenario.launch(LoginActivity::class.java)
// WHEN
onView(withId(R.id.user_name)).perform(typeThe Text (" test_user ")) onView (withId (R.i d.p assword)). Perform (typePerform (click()) // THEN assertThat(getIntents().first()) .hasComponentClass(HomeActivity::class.java) } }Copy the code
Let’s step through the test:
-
We use the new ActivityScenario API to start the LoginActivity. It will create an activity and enter the resumed state that the user can see and type. ActivityScenario handles all synchronization with the system and provides support for common scenarios that you should test, such as how your application will handle being destroyed and rebuilt by the system.
-
We use the Espresso View interaction library to enter text into two text fields and then click a button in the UI. Like ActivityScenario, Espresso handles multithreading and synchronization for you and provides a readable and smooth API to create tests.
-
We use the new Intents.getintents () Espresso API to return the list of captured Intents. We then verify the captured intent using IntentSubject.AssertThat (), which is part of the new Android Truth extension framework. The Android Truth Extension framework provides an expressive and readable API to verify the state of basic Android framework objects.
This test can be run on a local JVM using Robolectric or any real or virtual device.
To run it on an Android device, place it in the “androidTest” resource root along with the following dependencies:
AndroidTestImplementation (" androidx. Test: runner: 1.1.0 ") androidTestImplementation (" androidx. Test. Ext: junit: 1.0.0 ") AndroidTestImplementation (" androidx. Test. Espresso: espresso - the intents: 3.1.0 ") AndroidTestImplementation (" androidx. Test. Espresso: espresso - core: 3.1.0 ") AndroidTestImplementation (" androidx. Test. Ext: way: 1.0.0 ")Copy the code
Running on a real or virtual device gives you confidence that your code interacts correctly with the Android system. However, as the number of test cases increases, you begin to sacrifice test execution time. You might decide to run only some of the larger tests on the real machine while running lots of smaller unit tests on the emulator, such as Robolectric, which can run tests faster on the native JVM.
To run a test case on a local JVM using the Robolectric simulator, place the test case in the “test” resource root and add the following code to gradle.build:
testImplementation (" androidx. Test: runner: 1.1.0 ")testImplementation (" androidx. Test. Ext: junit: 1.0.0 ")testImplementation (" androidx. Test. Espresso: espresso - the intents: 3.1.0 ")testImplementation (" androidx. Test. Espresso: espresso - core: 3.1.0 ")testImplementation (" androidx. Test. Ext: way: 1.0.0 ")testImplementation (" org. Robolectric: robolectric: 4.0 ") android {testOptions.unitTests.includeAndroidResources = true
}
Copy the code
The unification of the test apis between emulators and instruments offers many exciting possibilities! Our Nitrogen project, released on Google I/O, will allow you to seamlessly switch tests between runtime environments. This means you’ll be able to take Test cases written for the new AndroidX Test APIs and run them on native JVMS, real or virtual devices, or even cloud-based Test platforms like Firebase Test LABS. We are excited about the opportunity to provide developers with quick, accurate, and actionable feedback on the quality of their applications.
Finally, we’re happy to announce that all AndroidX components are fully open source, and we look forward to your contributions.
To learn more
Documents: developer.android.com/testing
Version notes:
- AndroidX Test: developer.android.com/training/te…
- Robolectric: github.com/robolectric…
Robolectric: github.com/robolectric…
AndroidX Test: github.com/android/and…
If you find any mistakes in your translation or other areas that need to be improved, you are welcome to the Nuggets Translation Program to revise and PR your translation, and you can also get the corresponding reward points. The permanent link to this article at the beginning of this article is the MarkDown link to this article on GitHub.
The Nuggets Translation Project is a community that translates quality Internet technical articles from English sharing articles on nuggets. The content covers Android, iOS, front-end, back-end, blockchain, products, design, artificial intelligence and other fields. If you want to see more high-quality translation, please continue to pay attention to the Translation plan of Digging Gold, the official Weibo, Zhihu column.