资料内容:
Instrumented Tests with Espresso and
ActivityScenario
Let’s move on to instrumented tests. Begin by checking out the example
that Android Studio created. In
com.bignerdranch.android.geoquiz (androidTest), find
and open ExampleInstrumentedTest.kt:
@RunWith(AndroidJUnit4::class)
class ExampleInstrumentedTest {
@Test
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("com.bignerdranch.android.geoquiz", appContext.packageName)
}
}
Much of this code is similar to the tests you have seen so far: You have a
class containing a function annotated with @Test, and within that function
there is an assertion to verify some behavior. But there are also some
differences: First, the class itself has an annotation,
@RunWith(AndroidJUnit4::class), which signals to JUnit that
this test should be executed on an Android device. And the test function
relies on the Android SDK, specifically to verify that the app’s package
name is the same as the value you set when you created the app.
You are about to run ExampleInstrumentedTest, but you have some
housekeeping to take care of first. Since instrumented tests run on an
Android device, not your development machine, you need to either connect
an Android device, as you did in Chapter 2, or run an emulator. Make sure
the device dropdown at the top of the Android Studio window shows the