Learn about Android testing methods to learn about Android JunitRunner and JUnit
AndroidTest and Test directories
Each time we create a module, we will find two folders, androidTest and Test, under the directory. AndroidTest and Test are to Test the internal of the program, rather than whether it meets the expected function requirements. These Test codes will not be packaged into the final APK
- AndroidTest: Tests that require the device environment
- Test: Independent of the device environment, tests can be performed on a PC
JUnit
JUnit is an annotation-based unit testing framework that implements most tests in the Test directory, independent of the device environment, and can run unit tests in the local JVM
- Add the dependent
TestImplementation junit: junit: '4.12' androidTestImplementation androidx. Test. Ext: junit: 1.1.1 ' AndroidTestImplementation 'androidx. Test. Espresso: espresso - core: 3.2.0'Copy the code
- annotations
- @test: indicate that the method is a Test method
public void
, i.e.,kotlin
Use only in theFun + functionJust define the method - @before: Called once Before each test method executes
- @after: Called once After each test method executes
- @beforeClass: called once before all methods are executed, only once will be executed, the annotated method must be
public static void
, i.e.,kotlin
add@JvmStatic
Annotate and useFun + functionDefine methods - AfterClass: Conditional correspondence
@BeforeClass
, all methods are called once after execution - @ignore: Ignore the test method and add the annotation when you don’t want to use it
- RunWith: Indicates the test runner for the class
- @test: indicate that the method is a Test method
Note: in JUnit5, you can declare that each method is using an instance of a test class by using the @testinstance (Lifecycle.PER_CLASS) annotation, that is, you don’t need to use @jvmstatic once for every method
- assertions
Void assertEquals(Boolean expected, Boolean actual) : Check whether two variables or equations are balanced. If the check condition is true void assertFalse(Boolean condition) : If the check condition is false void assertNotNull(Object Object) : If the check condition is not empty…
- use
JUnit uses annotations and assertions to write logic to test a method. To test a method, right-click =>run” XXX ()”, and to test all methods in a class, perform the same operation on the class
Espresso
- An overview of the
The core API is small, predictable, and easy to learn, but still customizable. Espresso tests clearly state expectations, interactions, and assertions, unencumbered by boilerplate content, custom infrastructure, or messy implementation details. The Espresso test ran extremely fast! There is no waiting, synchronization, hibernation, or polling when it manipulates and asserts the application interface at rest.
- Add the dependent
AndroidTestImplementation 'androidx. Test. Espresso: espresso - core: 3.1.0' androidTestImplementation 'androidx. Test: runner: 1.1.0' androidTestImplementation 'androidx. Test: rules: 1.1.0'Copy the code
- Set up the piling test run program
Add the following to Android.defaultConfig
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Copy the code
- OnView sample
The onView method in Espresso allows you to access and interact with components in your target application
onView(withId(R.id.my_button))
Copy the code
OnView looks for a button with id R.d.My_button and, if a match is found, returns a reference for the user to perform actions such as assertions based on the view
- Run the test
There are two ways to do this. The first way is to create a test configuration and run it through Android Studio, which can be found in the official documentation. The second way is to run the tests by running Gradle commands
./gradlew connectedAndroidTest
Copy the code
AndroidJUnitRunner
In Android development, methods often use Android SYSTEM apis such as Context, Parcelable, SharedPreferences, and so on. The local JVM (JUnit4) cannot call these interfaces, so we need to use AndroidJUnitRunner to test these methods. To do so, we need to create a test class in the androidTest directory. Add a comment @runwith (Androidjunit4.class)
- The official sample
@RunWith(AndroidJUnit4::class) @LargeTest class ChangeTextBehaviorTest { val stringToBeTyped = "Espresso" @get:Rule val ActivityRule = ActivityTestRule(MainActivity::class.java) @test fun changeText_sameActivity() {// Enter text and click the button onView(withId(R.id.editTextUserInput)) .perform(typeText(stringToBeTyped), CloseSoftKeyboard ()) onView(withId(r.i.C.hangetextbt)).perform(click()) // Check if text has changed onView(withId(R.id.textToBeChanged)) .check(matches(withText(stringToBeTyped))) } }Copy the code
- Run the test
- As with JUnit, right-click the corresponding function or class of run (device environment needs to be configured)
reference
Android Unit Testing (1) is the JUnit foundation