Android Espresso 测试错误 activity
Android Espresso test on wrong activity
我的应用程序上有两个活动:登录 activity (loginActivity) 和第二个 activity (mainActivity)。我想用 Espresso 来测试 loginActivity 上的登录,所以我写了这个测试:
public class LoginActivityTest extends ActivityInstrumentationTestCase2<LoginActivity> {
public LoginActivityTest() {
super(LoginActivity.class);
}
@Override
public void setUp() throws Exception {
super.setUp();
getActivity();
}
public void testLogin() throws Exception {
onView(withId(R.id.button_log_in)).perform(click());
onView(withId(R.id.container)).check(matches(isDisplayed()));
}
}
问题在于,当应用程序启动时,如果用户之前已登录,则 loginActivity 会立即启动 mainActivity,并且在执行测试时会失败并显示错误:
android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: com.test.android.development:id/R.id.button_log_in
注意:如果我在 运行 测试之前启动应用程序并注销,错误就会消失。
提前致谢!
仅当用户之前未登录时使用 if
执行测试。
然后你可以添加一个 else
来测试记录的状态。
否则,您必须尝试捕获 NoMatchingViewException 以告诉您的应用在登录用户的情况下不要执行登录测试。类似于:
try {
// your test code here
} catch (NoMatchingViewException e){
Log.d(TAG_LOG, "No login test performed!");
}
让我知道这是否有帮助:)
我明白你说的了。在 UI 测试中,我们有多个注释。我认为你应该使用 @BeforeClass 方法(@BeforeClass 方法调用 Before onCreat 方法)。因此在 @BeforeClass 方法中使用数据库填满您的列表,然后删除数据库。然后在@AfterClass方法中(@AfterClass方法调用After onDestroy方法)。新建数据库like以前的数据库并在 @BeforeClass 方法中使用相同的列表。
我希望你觉得它有用。
我的应用程序上有两个活动:登录 activity (loginActivity) 和第二个 activity (mainActivity)。我想用 Espresso 来测试 loginActivity 上的登录,所以我写了这个测试:
public class LoginActivityTest extends ActivityInstrumentationTestCase2<LoginActivity> {
public LoginActivityTest() {
super(LoginActivity.class);
}
@Override
public void setUp() throws Exception {
super.setUp();
getActivity();
}
public void testLogin() throws Exception {
onView(withId(R.id.button_log_in)).perform(click());
onView(withId(R.id.container)).check(matches(isDisplayed()));
}
}
问题在于,当应用程序启动时,如果用户之前已登录,则 loginActivity 会立即启动 mainActivity,并且在执行测试时会失败并显示错误:
android.support.test.espresso.NoMatchingViewException: No views in hierarchy found matching: with id: com.test.android.development:id/R.id.button_log_in
注意:如果我在 运行 测试之前启动应用程序并注销,错误就会消失。
提前致谢!
仅当用户之前未登录时使用 if
执行测试。
然后你可以添加一个 else
来测试记录的状态。
否则,您必须尝试捕获 NoMatchingViewException 以告诉您的应用在登录用户的情况下不要执行登录测试。类似于:
try {
// your test code here
} catch (NoMatchingViewException e){
Log.d(TAG_LOG, "No login test performed!");
}
让我知道这是否有帮助:)
我明白你说的了。在 UI 测试中,我们有多个注释。我认为你应该使用 @BeforeClass 方法(@BeforeClass 方法调用 Before onCreat 方法)。因此在 @BeforeClass 方法中使用数据库填满您的列表,然后删除数据库。然后在@AfterClass方法中(@AfterClass方法调用After onDestroy方法)。新建数据库like以前的数据库并在 @BeforeClass 方法中使用相同的列表。 我希望你觉得它有用。