android自动化测试Uiautomator源码分析之四

来源:互联网 发布:wine mac下载 编辑:程序博客网 时间:2024/05/16 14:48

5 测试

Start方法中关于测试的代码如下

List<TestCase> testCases = collector.getTestCases();
for (TestCase testCase : testCases) {                prepareTestCase(testCase);                testCase.run(testRunResult);            }

getTestCases方法返回的是4.1 中根据测试方法创建的测试用例列表。

prepareTestCase为每个测试用例设置测试参数

protected void prepareTestCase(TestCase testCase) {        ((UiAutomatorTestCase)testCase).setAutomationSupport(mAutomationSupport);        ((UiAutomatorTestCase)testCase).setUiDevice(mUiDevice);        ((UiAutomatorTestCase)testCase).setParams(mParams);    }

TestCase的带参数的run方法如下

protected void run(final TestCase test) {startTest(test);Protectable p= new Protectable() {public void protect() throws Throwable {test.runBare();}};runProtected(test, p);endTest(test);}

分为三个步骤,依照顺序依次论述。

5.1 startTest

startTest方法如下,

public void startTest(Test test) {final int count= test.countTestCases();synchronized(this) {fRunTests+= count;}for (TestListener each : cloneListeners())each.startTest(test);}

cloneListeners方法,

private synchronized List<TestListener> cloneListeners() {List<TestListener> result= new ArrayList<TestListener>();result.addAll(fListeners);return result;}

其实是一样的。

然后回调WatcherResultPrinter的startTest方法。

5.2 runProtected

runProtected方法如下,

public void runProtected(final Test test, Protectable p) {try {p.protect();} catch (AssertionFailedError e) {addFailure(test, e);}catch (ThreadDeath e) { // don't catch ThreadDeath by accidentthrow e;}catch (Throwable e) {addError(test, e);}}

回调然后进一步调用TestCase的runBare方法,如果抛出异常,就调用addFailure或者addError方法。

public synchronized void addFailure(Test test, AssertionFailedError t) {fFailures.add(new TestFailure(test, t));for (TestListener each : cloneListeners())each.addFailure(test, t);}

addFailure首先新建一个TestFailure对象,然后添加到fFailures中。

最后回调WatcherResultPrinter的addFailure方法。

同理 addError方法新建一个TestFailure对象,然后添加到fErrors中。

最后回调WatcherResultPrinter的addError方法。

public synchronized void addError(Test test, Throwable t) {fErrors.add(new TestFailure(test, t));for (TestListener each : cloneListeners())each.addError(test, t);}

TestCase的runBare方法如下,

public void runBare() throws Throwable {Throwable exception= null;setUp();try {runTest();} catch (Throwable running) {exception= running;}finally {try {tearDown();} catch (Throwable tearingDown) {if (exception == null) exception= tearingDown;}}if (exception != null) throw exception;}

首先调用runTest方法进行测试,测试完成之后回调子类的tearDown方法。

5.2.1 runTest

runTest方法如下,

protected void runTest() throws Throwable {assertNotNull("TestCase.fName cannot be null", fName); // Some VMs crash when calling getMethod(null,null);Method runMethod= null;try {// use getMethod to get all public inherited// methods. getDeclaredMethods returns all// methods of this class but excludes the// inherited ones.runMethod= getClass().getMethod(fName, (Class[])null);} catch (NoSuchMethodException e) {fail("Method \""+fName+"\" not found");}if (!Modifier.isPublic(runMethod.getModifiers())) {fail("Method \""+fName+"\" should be public");}try {runMethod.invoke(this);}catch (InvocationTargetException e) {e.fillInStackTrace();throw e.getTargetException();}catch (IllegalAccessException e) {e.fillInStackTrace();throw e;}}

首先进行各种检查,检查无误后利用反射调用TestCase子类的对应的方法。

5.2.2 tearDown

tearDown方法是TestCase的抽象方法,在此,会调用UiAutomatorTestCase的tearDown方法,该方法如下,

protected void tearDown() throws Exception {        if (mShouldDisableIme) {            restoreActiveIme();        }        super.tearDown();    }

更进一步地,会调用具体测试类的restoreActiveIme方法。

5.3 endTest

TestResult的endTest方法如下,

public void endTest(Test test) {for (TestListener each : cloneListeners())each.endTest(test);}

和startTest方法相似, 回调WatcherResultPrinter的endTest方法。

0 0