【发布时间】:2015-05-29 09:03:32
【问题描述】:
我今天有一个简单的测试:
@RunWith(AndroidJUnit4.class)
@LargeTest
public class WhenNavigatingToUsersView {
@Rule
public ActivityTestRule<MainActivity> mActivityRule =
new ActivityTestRule(MainActivity.class);
private MainActivity mainActivity;
@Before
public void setActivity() {
mainActivity = mActivityRule.getActivity();
onView(allOf(withId(R.id.icon), hasSibling(withText(R.string.users)))).perform(click());
}
@Test
public void thenCorrectViewTitleShouldBeShown() {
onView(withText("This is the Users Activity.")).check(matches(isDisplayed()));
}
@Test
public void thenCorrectUserShouldBeShown() {
onView(withText("Donald Duck (1331)")).check(matches(isDisplayed()));
}
}
但是对于每个测试方法,setActivity 都会运行,如果您有 10-15 个方法,最终会很耗时(如果您也有很多视图)。
@BeforeClass 似乎不起作用,因为它必须是静态的,因此也迫使 ActivityTestRule 也是静态的。
那么还有其他方法可以做到这一点吗?而不是在同一个测试方法中有多个断言?
【问题讨论】:
标签: android junit4 android-espresso