spresso – 每个测试方法后活动都会关闭.有没有办法我们不能停止关闭活动和运行其他测试方法 最佳答案 如果我明白你的意思,你可以做以下解决方案之一:
>写下一个大测试来测试所有想要的视图,而不是任何
中断:
@Test
public void checkIfMainActivityViewsAreProperlyDisplayed() {
//Check if activity name is visible on Toolbar
onView(withText(R.string.action_main)).check(matches(withParent(withId(R.id.toolbar))));
//Check if ListView is visible at least 50 percent
onView(withId(R.id.mListView)).check(matches(isDisplayingAtLeast(50)));
//Check if order Checkbox is checked. Perform check.
onView(withId(R.id.mCheckBox))
.check(matches(isNotChecked()))
.perform(ViewActions.click())
.check(matches(isChecked()));
}
使用注释可以让您的代码更清晰
>记下测试套件并将其添加到主方法,如下所示:
private void checkifActivityNameIsVisibleOnToolbar() {
onView(withText(R.string.action_main)).check(matches(withParent(withId(R.id.toolbar))));
}
private void checkifListViewIsVisibleAtLeastFiftyPercent() {
onView(withId(R.id.mListView)).check(matches(isDisplayingAtLeast(50)));
}
private void performClickOnOrderCheckBoxToFindIfStateChanged() {
onView(withId(R.id.mOrder))
.check(matches(isNotChecked()))
.perform(ViewActions.click())
.check(matches(isChecked()));
}
@Test
public void checkIfMainActivityViewsAreProperlyDisplayed() throws InterruptedException {
checkifActivityNameIsVisibleOnToolbar();
checkifListViewIsVisibleAtLeastFiftyPercent();
performClickOnOrderCheckBoxToFindIfStateChanged();
}
维护测试代码可能比上一个更容易.
希望它会有所帮助