Python知识分享网 - 专业的Python学习网站 学Python,上Python222
Android Programming The Big Nerd Ranch Guide 第五版 PDF 下载
发布于:2023-12-24 10:37:18

Android Programming The Big Nerd Ranch Guide 第五版 PDF 下载  图1






Instrumented Tests with Espresso and
Let’s move on to instrumented tests. Begin by checking out the example
that Android Studio created. In (androidTest), find
and open ExampleInstrumentedTest.kt:
class ExampleInstrumentedTest {
fun useAppContext() {
// Context of the app under test.
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
assertEquals("", 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