在测试用例之后保持已检测的Activity活动

时间:2012-05-02 00:21:20

标签: android robotium

假设您正在使用具有以下签名的类构建Android平台的测试用例:

public class AccountTest extends ActivityInstrumentationTestCase2<MainActivity> { ... }

假设我们使用Robotium框架有这样的测试方法:

public void testCreateAccount() {
    solo.clickOnButton("Add Account");
    solo.clickOnButton("Next");

    ...
}

当测试用例完成时,仪表活动会迅速被杀死。是否有任何方法可以在终止时保持活着,至少为了确定下一步要做什么以及编写下几行?

3 个答案:

答案 0 :(得分:2)

Android JUnit Test构建于JUnit之上,因此,它遵循JUnit测试生命周期的基本规则:

  • 调用测试用例的setUp()方法
  • 调用测试方法
  • 调用测试用例的tearDown()方法
  

当测试用例完成时,仪表活动会迅速被杀死。

请参阅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();
}
  

有没有办法让它在终止时保持活着,至少是为了确定你下一步要做什么以及编写接下来的几行?

可能的解决方法是:

  1. 完全覆盖AccountTest类中的teardown()方法(扩展ActivityInstrumentationTestCase2类),而不调用super.tearDown();
  2. 创建自定义的ActivityInstrumentationTestCase3类(扩展ActivityTestCase类)以避免在teardown()方法中完成活动。
  3. 在任何一种方法中,都应该仔细实现teardown()方法以避免内存泄漏。

    希望这有帮助。

答案 1 :(得分:1)

事实证明,使用Nativedriver库很容易实现我想要的东西:

http://code.google.com/p/nativedriver/

在测试用例结束时省略driver.quit()可以使活动保持活动状态,这在最初开发测试用例时非常方便。

答案 2 :(得分:0)

我有同样的问题。最后我使用Sleep().

while(true)
{
   Thread.Sleep(1000);
   // do other things when the main thread wake
}

您可以将上述代码放在任意测试方法中,然后仪器Activity将不会结束。

希望它对你有所帮助。

相关问题