android-在测试用例后保持有活动的活动

android-在测试用例后保持有活动的活动,第1张

概述假设您正在使用带有如下签名的类为Android平台构建测试用例:publicclassAccountTestextendsActivityInstrumentationTestCase2<MainActivity>{...}假设我们有一个使用Robotium框架的测试方法,如下所示:publicvoidtestCreateAccount(){solo.clickOnButton("AddA

假设您正在使用带有如下签名的类为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的源代码,原因如下:

@OverrIDeprotected 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()方法,以避免内存泄漏.

希望这可以帮助.

总结

以上是内存溢出为你收集整理的android-在测试用例后保持有活动的活动全部内容,希望文章能够帮你解决android-在测试用例后保持有活动的活动所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1074010.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存