ndroid中的自动测试这里结合Music的测试程序,分析一下Android测试程序的结构和执行流程。

来源:互联网 发布:淘宝直通车怎么玩 编辑:程序博客网 时间:2024/05/22 02:30

这里结合Music的测试程序,分析一下Android测试程序的结构和执行流程。

o TestRunner

1.Instrumentation是一个基本的可执行单元,有点类似于Activity和Service,所有测试程序都必须从它继承过来。

2.TestSuiteProvider是一个接口,有一个接口函数getTestSuite,用来获取测试用例。

3.InstrumentationTestRunner主要是重载了Instrumentation的onCreate和onStart,把执行线程和测试用例关联起来。

4.MusicPlayerStressTestRunner重载getAllTests了,框架通过getAllTests获取测试用例。

o 测试程序的执行过程

1.InstrumentationTestRunner::onCreate
调用getTestSuite获取测试用例,创建AndroidTestRunner对象mTestRunner,把TestRunner和TestCase关联起来。

2.Instrumentation::start
这里创建一个线程InstrumentationThread,然后执行线程的start函数。

3.InstrumentationThread::start
这里反过来调用Instrumentation::onStart, 这个函数实际上是在InstrumentationTestRunner里实现的。

4.InstrumentationTestRunner::onStart
这里真正去调用mTestRunner.runTest。

5.AndroidTestRunner::runTest
这里在一个循环中,去调用每一个测试用例的run函数。

o TestCase

1.Runnable是个接口,里面只有一个run函数。

2.TestCase也个接口,提供了setUp和tearDown两个函数,用来做测试前的初始化和测试后的清除工作。

3.InstrumentationTestCase 对Instrumentation进行包装,提供了sendKeys之类的函数,方便Testcase使用。里面还提供了runTest和runTestOnUiThread两个函数,但我还不知道这里怎么调用过来的。

4.ActivityInstrumentationTestCase是个模板,它在setUp时创建指定的Activity。

5.TestPlaylist则是实际的测试程序,这里实现了testDeletePlaylist之类测试函数。

o 测试程序的分类

1.Suppress
Use this annotation on test classes or test methods that should not be included in a test
suite. If the annotation appears on the class then no tests in that class will be included. If
the annotation appears only on a test method then only that method will be excluded.

2.LargeTest
Marks a test that should run as part of the large tests.

3.MediumTest
Marks a test that should run as part of the medium tests.

4.SmallTest
Marks a test that should run as part of the small tests.

5.Smoke
Marks a test that should run as part of the smoke tests.
The android.test.suitebuilder.SmokeTestSuiteBuilder
will run all tests with this annotation.

在函数或类的前面加相应的annotation,即可对其进行分类。如:

    @LargeTest
public void testDeletePlaylist() throws Exception{
boolean isEmptyPlaylist = true;
addNewPlaylist(MusicPlayerNames.DELETE_PLAYLIST_NAME);
deletePlaylist(MusicPlayerNames.DELETE_PLAYLIST_NAME);
isEmptyPlaylist = verifyPlaylist(MusicPlayerNames.DELETE_PLAYLIST_NAME);
assertFalse("testDeletePlaylist", isEmptyPlaylist);
}

在InstrumentationTestRunner里,会根据命令行参数来决定运行哪一类测试:

    private Predicate<TestMethod> getSizePredicateFromArg(String sizeArg) {
 
if (SMALL_SUITE.equals(sizeArg)) {
return TestPredicates.SELECT_SMALL;
} else if (MEDIUM_SUITE.equals(sizeArg)) {
return TestPredicates.SELECT_MEDIUM;
} else if (LARGE_SUITE.equals(sizeArg)) {
return TestPredicates.SELECT_LARGE;
} else {
return null;
}
}
 
testSuiteBuilder.addRequirements(testSizePredicate);

在TestSuiteBuilder中对测试程序进行筛选:

     */
public final TestSuite build() {
rootSuite = new TestSuite(getSuiteName());
 
// Keep track of current class so we know when to create a new sub-suite.
currentClassname = null;
try {
for (TestMethod test : testGrouping.getTests()) {
if (satisfiesAllPredicates(test)) {
addTest(test);
}
}
if (testCases.size() > 0) {
for (TestCase testCase : testCases) {
if (satisfiesAllPredicates(new TestMethod(testCase))) {
addTest(testCase);
}
}
}
} catch (Exception exception) {
Log.i("TestSuiteBuilder", "Failed to create test.", exception);
TestSuite suite = new TestSuite(getSuiteName());
suite.addTest(new FailedToCreateTests(exception));
return suite;
}
return rootSuite;
}

o AndroidManifest.xml

AndroidManifest.xml用来描述各个测试程序的入口:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.music.tests">
 
<application>
<uses-library android:name="android.test.runner" />
</application>
 
<instrumentation android:name=".MusicPlayerLaunchPerformance"
android:targetPackage="com.android.music"
android:label="Music Launch Performance">
</instrumentation>
 
<instrumentation android:name=".MusicPlayerFunctionalTestRunner"
android:targetPackage="com.android.music"
android:label="Music Player Functional Test">
</instrumentation>
 
<instrumentation android:name=".MusicPlayerStressTestRunner"
android:targetPackage="com.android.music"
android:label="Music Player Stress Test">
</instrumentation>
 
</manifest>

o 运行测试程序
1.编译

cd packages/apps/Music/tests
mm

2.安装

adb install ../../../../out/target/product/littleton/data/app/MusicTests.apk

3.运行

adb shell am instrument -w com.android.music.tests/.MusicPlayerFunctionalTestRunner
原创粉丝点击