如果你也正在学习单元测试的相关内容,请看:
AndroidX Test 是Jetpack库的集合,可以使测试用例运行在Android APP,它同样提供了一系列工具帮忙编写测试用例。
ActivityScenarioRule
@RunWith(AndroidJUnit4::class.java)
@LargeTest
class MyClassTest {
@get:Rule
val activityRule = ActivityScenarioRule(MyClass::class.java)
@Test fun myClassMethod_ReturnsTrue() {
activityRule.scenario.onActivity { … } // Optionally, access the activity.
}
}
ActivityScenarioRule会在测试开始时启动一个activity,测试结束时关闭activity。
调用它的getScenario方法会返回ActivityScenario。
ActivityScenario
ActivityScenario用来启动和控制activity生命周期。
Before:
MyActivity activity = Robolectric.setupActivity(MyActivity.class);
assertThat(activity.getSomething()).isEqualTo("something");
After:
try(ActivityScenario<MyActivity> scenario = ActivityScenario.launch(MyActivity.class)) {
scenario.onActivity(activity -> {
assertThat(activity.getSomething()).isEqualTo("something");
});
}
Before:
ActivityController<MyActivity> controller = Robolectric.buildActivity(MyActivity.class);
controller.create().start().resume(); // Moves the activity state to State.RESUMED.
controller.pause(); // Moves the activity state to State.STARTED. (ON_PAUSE is an event).
controller.stop(); // Moves the activity state to State.CREATED. (ON_STOP is an event).
controller.destroy(); // Moves the activity state to State.DESTROYED.
After:
try(ActivityScenario<MyActivity> scenario = ActivityScenario.launch(MyActivity.class)) {
scenario.moveToState(State.RESUMED); // Moves the activity state to State.RESUMED.
scenario.moveToState(State.STARTED); // Moves the activity state to State.STARTED.
scenario.moveToState(State.CREATED); // Moves the activity state to State.CREATED.
scenario.moveToState(State.DESTROYED); // Moves the activity state to State.DESTROYED.
}
它在测试结束时不会自动关闭activity,需要出发close,建议使用ActivityScenarioRule,ActivityScenarioRule在测试结束时会自动关闭activity。
FragmentScenario
FragmentScenario用来启动和管理fragment的生命周期。
添加依赖
dependencies {
val fragment_version = "1.5.7"
debugImplementation("androidx.fragment:fragment-testing:$fragment_version")
}
可以通过launchInContainer或launch创建fragment。
@RunWith(AndroidJUnit4::class)
class MyTestSuite {
@Test fun testEventFragment() {
// The "fragmentArgs" argument is optional.
val fragmentArgs = bundleOf(“selectedListItem” to 0)
val scenario = launchFragmentInContainer<EventFragment>(fragmentArgs)
...
}
}
@RunWith(AndroidJUnit4::class)
class MyTestSuite {
@Test fun testEventFragment() {
// The "fragmentArgs" arguments are optional.
val fragmentArgs = bundleOf("numElements" to 0)
val scenario = launchFragment<EventFragment>(fragmentArgs)
...
}
}
通过触发moveToState方法,改变fragment的生命周期
@RunWith(AndroidJUnit4::class)
class MyTestSuite {
@Test fun testEventFragment() {
val scenario = launchFragmentInContainer<EventFragment>(
initialState = Lifecycle.State.INITIALIZED
)
// EventFragment has gone through onAttach(), but not onCreate().
// Verify the initial state.
scenario.moveToState(Lifecycle.State.RESUMED)
// EventFragment moves to CREATED -> STARTED -> RESUMED.
...
}
}
ServiceTestRule
ServiceTestRule用来启动或绑定service,并在测试结束时自动关闭或解绑。它不支持IntentService测试。
@RunWith(AndroidJUnit4::class.java)
@MediumTest
class MyServiceTest {
@get:Rule
val serviceRule = ServiceTestRule()
@Test fun testWithStartedService() {
serviceRule.startService(
Intent(ApplicationProvider.getApplicationContext<Context>(),
MyService::class.java))
// Add your test code here.
}
@Test fun testWithBoundService() {
val binder = serviceRule.bindService(
Intent(ApplicationProvider.getApplicationContext(),
MyService::class.java))
val service = (binder as MyService.LocalBinder).service
assertThat(service.doSomethingToReturnTrue()).isTrue()
}
}
AndroidJUnitRunner
AndroidJUnitRunner是一个Junit测试运行程序。可在Android设备上运行Junit3或Junit4型测试,包括使用 Espresso 和 UI Automator 测试框架的测试类。此测试运行程序负责将测试软件包和被测应用加载到设备上,运行测试并报告测试结果。
- End
参考: