Android中的自动测试(2)
1,401 views| 2010-01-14| 李先静| Android| | 发表评论转载时请注明出处和作者联系方式
文章出处:http://www.limodev.cn/blog
作者联系方式:李先静 <xianjimli@gmail.com>
这里结合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
Android Share
Tags
Recent Posts
Most Viewed
- 系统程序员成长计划写作提纲 - 19,646 views
- Android IPC机制详解 - 6,350 views
- 系统程序员成长计划-走近专业程序员(上) - 6,262 views
- 系统程序员成长计划-写得又快又好的秘诀(一) - 5,419 views
- 系统程序员成长计划-背景知识 - 5,075 views
- i++循环与i–循环的执行效率 - 4,738 views
- 系统程序员成长计划-Write once, run anywhere(WORA)(上) - 4,708 views
- 系统程序员成长计划-走近专业程序员(下) - 4,262 views
- Linux下的调试工具 - 4,033 views
- Advanced Linux Sound Architecture (ALSA) 研究笔记 - 4,032 views
- 系统程序员成长计划-序 - 3,998 views
- 系统程序员成长计划-写得又快又好的秘诀(三) - 3,937 views
- 中国人与自由软件文化研究(搞笑版) - 3,743 views
- Android中的MessageQueue,Handler,Looper和Thread - 3,715 views
- 答复:我不会OOO,仍然可以XXX - 3,665 views
Categories
- Android (28)
- Broncho-A1-Hack (6)
- DirectFB (7)
- FTK(嵌入式GUI) (24)
- GTK+ (29)
- KVM hack notes (8)
- Linux Mobile (65)
- Management (5)
- Mozilla (9)
- Open Source (5)
- Programming (34)
- Tools (9)
- Uncategorized (23)
- Win32 (3)
- X Windows (31)
- 沉思录 (29)
- 系统程序员成长计划 (67)
Blogroll
gallery
Linux guru
推荐网站
Recent Comments
- Dig on 嵌入式GUI FTK设计与实现-事件源(FtkSource)
- 用心生活每一天 » GNU gprof: linux profiling tools 使用 on gcc profiling的工作原理
- JavaScript for: i++ vs i–-传播、沟通、分享-一直“有你” on i++循环与i–循环的执行效率
- Frankly Law on 嵌入式GUI FTK介绍(11)-交叉编译
- tracing on Linux下的调试工具
- ndljsn on FTK移植指南(初稿)
- tracing on 爬塘朗山
- tracing on GTK+(基于DirectFB)的字体处理
- Kely on 系统程序员成长计划写作提纲
- tracing on 爬塘朗山



January 14th, 2010