android-在测试用例后保持有活动的活动
作者:互联网
假设您正在使用带有如下签名的类为Android平台构建测试用例:
public class AccountTest extends ActivityInstrumentationTestCase2<MainActivity> { ... }
假设我们有一个使用Robotium框架的测试方法,如下所示:
public void testCreateAccount() {
solo.clickOnButton("Add Account");
solo.clickOnButton("Next");
...
}
测试用例完成后,检测到的活动将被迅速终止.是否有任何方法可以使它在终止时保持活动状态,至少是为了确定下一步要做什么并对接下来的几行进行编码?
解决方法:
Android JUnit Test是在JUnit之上构建的,因此,它遵循JUnit测试生命周期的基本规则:
>调用测试用例的setUp()方法
>调用测试方法
>调用测试用例的tearDown()方法
When the test case completes, the instrumented activity is swiftly killed.
请参阅ActivityInstrumentationTestCase2的源代码,原因如下:
@Override
protected void tearDown() throws Exception {
// Finish the Activity off (unless was never launched anyway)
Activity a = super.getActivity();
if (a != null) {
a.finish();
setActivity(null);
}
// Scrub out members - protects against memory leaks in the case where someone
// creates a non-static inner class (thus referencing the test case) and gives it to
// someone else to hold onto
scrubClass(ActivityInstrumentationTestCase2.class);
super.tearDown();
}
Is there any way to keep it alive upon termination, at least for the purpose of determining what you want to do next and coding the next few lines?
可能的解决方法是:
>完全覆盖AccountTest类(扩展ActivityInstrumentationTestCase2类)中的teardown()方法,而无需调用super.tearDown();
>创建自定义的ActivityInstrumentationTestCase3类(扩展了ActivityTestCase类),以避免在teardown()方法中完成活动.
在这两种方法中,您都应仔细实现teardown()方法,以避免内存泄漏.
希望这可以帮助.
标签:robotium,android 来源: https://codeday.me/bug/20191201/2080421.html