原址 http://www.yeetrack.com/?p=987android
Robotium是个开源的android功能测试自动化框架,比较流行。我平时使用robotium+maven+spoon,这样的组合作androd自动化。作界面自动化,尤为是移动端,case常常失败,网络问题、等待机制不合理、手机问题等等。一个case失败后,再次运行可能又正常了,这种状况不少。其实咱们在使用Robotium时,能够手动实现case失败后,重跑几回,若是几回都失败了,那么case才断定为失败。git
方法github
使用Robotium要继承Robotium的ActivityInstrumentationTestCase2这个抽象类,而且要实现其中的setup、tearDown方法。其中还有一个方法runTest就是控制执行咱们的测试用例的,咱们能够重载这个方法,实现case失败后重跑。代码以下:网络
@Override protected void runTest() throws Throwable { int retryTimes = 3; while(retryTimes > 0) { try{ Log.d("Robotium", "super"); super.runTest(); break; } catch (Throwable e) { if(retryTimes > 1) { Log.d("Robotium", "fail,重跑--"+retryTimes); retryTimes--; tearDown(); setUp(); continue; } else throw e; //记得抛出异常,不然case永远不会失败 } } }
Over,就这么简单。框架
Spoon截图重复maven
由于我使用spoon插件,重跑会致使截图重复出现。我如今时修改下Spoon-client的源码。在Spoon-client的Spoon这个final类中,有截图的实现方法。部分代码以下:ide
public static File screenshot(Activity activity, String tag) { if (!TAG_VALIDATION.matcher(tag).matches()) { throw new IllegalArgumentException("Tag must match " + TAG_VALIDATION.pattern() + "."); } try { File screenshotDirectory = obtainScreenshotDirectory(activity); String screenshotName = System.currentTimeMillis() + NAME_SEPARATOR + tag + EXTENSION; File screenshotFile = new File(screenshotDirectory, screenshotName); takeScreenshot(screenshotFile, activity); Log.d(TAG, "Captured screenshot '" + tag + "'."); return screenshotFile; } catch (Exception e) { throw new RuntimeException("Unable to capture screenshot.", e); } }
能够看到做者为了防止截图重复使用了时间戳方法System.currentTimeMillis(),这里咱们就把时间戳去掉,让重复的截图直接覆盖。测试
代码改完,打到本地maven仓库,或者私服,使用便可。插件
附上android-spoon插件地址:https://github.com/square/spooncode