2016-05-10 39 views
0

我正在使用Robotium在Android上执行自动化测试。 问题是,当执行一堆测试时,如果任何测试导致应用程序崩溃 - 将不会执行进一步的测试。 “只有这条信息会出现”测试未能运行到完成,原因:'由于'进程崩溃'导致测试运行失败。'。查看设备logcat以获取详细信息“。使用Robotium处理应用程序崩溃

我试着在runTest()上使用try \ catch,但应用程序崩溃时不会引发异常。

public void runTest() throws Throwable { 
     try { 
      super.runTest(); 
     } catch (Throwable t) { 
      String testCaseName = String.format("%s.%s", getClass().getName(), getName()); 
      solo.takeScreenshot(testCaseName); 
      Logging.w(Logging.TAG.Test, String.format("Captured screenshot for failed test: %s", testCaseName)); 
      t.printStackTrace(); 
      String out = ""; 
      for (StackTraceElement s : t.getStackTrace()) { 
       out = out + s.toString() + "\n"; 
      } 
      fail(t.getMessage() + "\n" + out); 
     } 
    } 

但仍然存在“测试未能运行至完成...”消息,并且所有测试执行都停止。

当前测试失败是可以的,问题在于未执行下一个测试。有人解决了这个问题吗?有没有办法从测试中处理应用程序异常?

回答

0

的问题是在

System.exit(0) 

的代码内。当主应用程序以这种方式退出时,它的进程被杀死,并且随着机器人的测试被杀死(它们生活在相同的进程中)。 对我来说,解决方案是从主应用程序代码中删除System.exit(0)。

+0

如果应用程序依赖于出口,它有时可以帮助用finish()替换System.exit(0)。 –