uiautomator 自定义testrunner使用和启动原理

继承testrunner实现runner

public class TestRunner extends UiAutomatorTestRunner
{
    private final List<String> mTestClasses = new ArrayList<String>();
 
    @Override
    public void run(List<String> testClasses, Bundle params, boolean debug, boolean monkey)
    {
        try
        {
            testClasses.removeAll(testClasses);  //removeAll
            addTestClasses();
            testClasses.addAll(mTestClasses); //addAll
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        super.run(testClasses, params, debug, monkey);
}

运行testrunner

-e runner com.meizu.browser.test.TestRunner

应用

可以根据外部传入的值,来确定需要执行什么用例

例如-e priority p1  取出priority值,在addtestcase的时候只加入priority是P1的testcase

 

获取runner源码

/*     */   protected UiAutomatorTestRunner getRunner() {
/* 140 */     if (this.mRunner != null) {
/* 141 */       return this.mRunner;
/*     */     }
/*     */     
/* 144 */     if (this.mRunnerClassName == null) {
/* 145 */       this.mRunner = new UiAutomatorTestRunner();
/* 146 */       return this.mRunner;
/*     */     }
/*     */     
/* 149 */     Object o = null;
/*     */     try {
/* 151 */       Class<?> clazz = Class.forName(this.mRunnerClassName);
/* 152 */       o = clazz.newInstance();
/*     */     } catch (ClassNotFoundException cnfe) {
/* 154 */       System.err.println("Cannot find runner: " + this.mRunnerClassName);
/* 155 */       System.exit(-4);
/*     */     } catch (InstantiationException ie) {
/* 157 */       System.err.println("Cannot instantiate runner: " + this.mRunnerClassName);
/* 158 */       System.exit(-4);
/*     */     } catch (IllegalAccessException iae) {
/* 160 */       System.err.println("Constructor of runner " + this.mRunnerClassName + " is not accessibile");
/* 161 */       System.exit(-4);
/*     */     }
/*     */     try {
/* 164 */       UiAutomatorTestRunner runner = (UiAutomatorTestRunner)o;
/* 165 */       this.mRunner = runner;
/* 166 */       return runner;
/*     */     } catch (ClassCastException cce) {
/* 168 */       System.err.println("Specified runner is not subclass of " + UiAutomatorTestRunner.class.getSimpleName());
/*     */       
/* 170 */       System.exit(-4);
/*     */     }
/*     */     
/* 173 */     return null;
/*     */   }

 

posted @ 2017-01-21 21:05  q55091  阅读(206)  评论(0编辑  收藏  举报