Roboitum中加入失败重跑机制

来源:互联网 发布:凸优化 matlab 编辑:程序博客网 时间:2024/05/17 01:37

http://www.yeetrack.com/?p=987


Roboitum中加入失败重跑机制

Robotium是个开源的android功能测试自动化框架,比较流行。我平时使用robotium+maven+spoon,这样的组合做androd自动化。做界面自动化,尤其是移动端,case经常失败,网络问题、等待机制不合理、手机问题等等。一个case失败后,再次运行可能又正常了,这种情况很多。其实我们在使用Robotium时,可以手动实现case失败后,重跑几次,如果几次都失败了,那么case才判定为失败。

方法

使用Robotium要继承Robotium的ActivityInstrumentationTestCase2这个抽象类,并且要实现其中的setuptearDown方法。其中还有一个方法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截图重复

因为我使用spoon插件,重跑会导致截图重复出现。我现在时修改下Spoon-client的源码。在Spoon-client的Spoon这个final类中,有截图的实现方法。部分代码如下:

    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/spoon


0 0
原创粉丝点击