(转自http://www.williamhua.com/2009/05/07/using-junit-in-android-app-development/稍作修改。我自己使用的是SDK1.6的例子,在SDK1.6上运行成功)51Testing软件测试网-}I8m S i;L'q?,R
51Testing软件测试网
S.`"?3NtZ5t#?
L E`I ?:v'p j%Y'{0Android 1.5_r1的release notes中专门提到了ADT0.9对于JUnit支持的改进,对于崇尚TDD(测试驱动开发)的人来说这无疑是一个好消息,今天就抽点时间说说Android 1.5中JUnit集成相关的要点吧。
51Testing软件测试网B y
D)z;t#`p*n
51Testing软件测试网3azFa&w!b配置完1.5的SDKADT0.9,运行第一个1.5的sample的时候我就看到Run as中多出了一个“Android JUnit Test”的选项(以前是在Debug中)。51Testing软件测试网$L7B[z_t2yG"p
51Testing软件测试网6O1z
B9xjH cr
可是文档里并没有提到如何在Android project中集成JUnit测试,好在我从Android Developers讨论组里找到了TDD with Android 1.5这么一个帖子。51Testing软件测试网
?ZX;@_Yp/IC;t
51Testing软件测试网psc#I@7GI就先来看一看如何把Sample中的test case跑起来吧。51Testing软件测试网,{FSA9k
(多谢Zhao的blog上关于在Android 1.5 pre中运行APIDemo测试的总结)
1{3_X;oQ C;IPg!XHV0第1步,新建一个Android项目,选择“create project from existing source”,并把路径指向android-sdk-1.5/platforms/android-1.5/samples/ApiDemos
[J5}H-{'n0第2步,再新建一个Android项目,依然通过“create project from existing source”的方式,这次把路径指向android-sdk-1.5/platforms/android-1.5/samples/ApiDemos/tests
kt5}cST0这时候ADT会报错,因为它无法找到APIDemo项目。右键,选择Properties,在Java Build Path –> Projects中添加APIDemo项目即可。
x&`.qb-?n0第3步,以“Android Application”方式运行第一个项目(注意正确设置AVD,第一次运行程序时,选中项目单击右键àRun AsàOpen Run DialogàTarget,选中所用的AVD),APIDemo将被安装到模拟器。
.Q4a!A3HmVJ&f0第4步,以“Android Application”方式运行第二个项目(注意正确设置AVD,第一次运行程序时,选中项目单击右键àRun AsàOpen Run DialogàTarget,选中所用的AVD),APIDemo Test将被安装到模拟器。
[s8e ZJzr}&Q7H0第5步,现在,我们可以通过Dev Tools中的Instrumentation来执行APIDemo Test了。找到“Dev tools”à“Instrumentation”中的”Tests for API Demos.”,点击即可开始测试。
%g4j@yj/t0
*@)] YY6CK+A
e3W#s!r0这时,通过LogCat即可看到测试结果。
_ B!u-OM0|rIB%f"q
W:X2J0I/instrumentation( 773): INSTRUMENTATION_STATUS_CODE: 151Testing软件测试网J/Wa Y3m.E4J2Tt
n^LZ D;^F0I/TestRunner( 780): finished: testAndroidTestCaseSetupProperly(com.example.andr
C n*R-^6X0R\5hd1P Z8\nuO0oid.apis.view.Focus2AndroidTest)
n1J(C4Sox051Testing软件测试网'bpq&DE%[!NQ)sI/TestRunner( 780): passed: testAndroidTestCaseSetupProperly(com.example.androi
#|
b}-L'f `-b,?06?p'lI,V%G7R4w0d.apis.view.Focus2AndroidTest)
'Si
G}4Gw&i~FR09k.iUuK0I/instrumentation( 773): INSTRUMENTATION_STATUS_RESULT: stream=.
2yz)y6Wn%t0B0t7TRcy,_0I/instrumentation( 773): INSTRUMENTATION_STATUS_RESULT: test=testAndroidTestCas
h(_#xVr.E^m sX0_&[
S["A0M0eSetupProperly51Testing软件测试网*]Z!p
GJ
{;n+\%}0K&K{WP0I/instrumentation( 773): INSTRUMENTATION_STATUS_RESULT: class=com.example.andro
-na0Aok s Y0?*_L!^S0id.apis.view.Focus2AndroidTest
P:@
G&|[(b(y051Testing软件测试网4t1|rH
o2z!M;I|I/instrumentation( 773): INSTRUMENTATION_STATUS_RESULT: current=2251Testing软件测试网Q0AYx u:v8}#_
51Testing软件测试网sAU%sm}2T&lI/instrumentation( 773): INSTRUMENTATION_STATUS_RESULT: numtests=22
_"qjZ3}0
~A
Xe@S Nz}s0I/instrumentation( 773): INSTRUMENTATION_STATUS_RESULT: id=InstrumentationTestR51Testing软件测试网Bx#{:LU i+qM#W
u'd Y {(l|g0unner
Iv4OG5`+J0
Q'tl QK!JA0I/instrumentation( 773): INSTRUMENTATION_STATUS_CODE: 0
,i6lM*|-B/Q7?5S0)k:~ dY$l+v|o0I/instrumentation( 773): INSTRUMENTATION_RESULT: stream=51Testing软件测试网Brpc)`wa
6[(p Ld$~v/R0I/instrumentation( 773): Test results for InstrumentationTestRunner=...........
*_\Z+S
_n051Testing软件测试网/Y!pGKR$][...........
/o ZP)N#f051Testing软件测试网8[&U-O_/qOp J6`xI/instrumentation( 773): Time: 12.21251Testing软件测试网2~6gaG]VD}@
{G,M5F![0I/instrumentation( 773):51Testing软件测试网5u4J,B@!ena
-ok"@1dF0I/instrumentation( 773): OK (22 tests)51Testing软件测试网RHz.M-E9A\ K9v$P{
#Mml+Hz/@R0
%I4X Y0X-T S:L6aDP09AWl"G9e%T+s @0除了通过Dev Tools来执行单元测试,我们还有另外两种方法:51Testing软件测试网}i'?qL'FZw
1、通过ADT,在eclipse中执行测试51Testing软件测试网 W,wWmy b3]+uFY
在eclipse中选中test项目,直接Run As “Android JUnit Test”既可以,测试结果会以图形化的方式返回。
51Testing软件测试网]-B9b4LSF VY
51Testing软件测试网sSY5[ db2、通过sdb shell命令执行测试51Testing软件测试网IA:Zw"O/KO!M
在命令行中执行adb shell am instrument -w com.example.android.apis.tests/android.test.InstrumentationTestRunner命令51Testing软件测试网6i'WJ1P/NS!j-AP
/r:M,aY2YqW$g0其中,com.example.android.apis.tests是APIDemo Test所在的package。51Testing软件测试网?{v]PDK}
U E6p-p6u OMN0那么,我们如何创建自己的test项目呢?
8}$H!Z!lU0大致的步骤如下:51Testing软件测试网6{as#ir{/Yax
1、新建一个普通的Android项目,比如项目名为Foo,Package为com.foo.bar
3@7HbRbi+lYb02、新建一个Test项目,注意把Package填成com.foo.bar.tests,项目名任意,比如FooTest,Application name任意
3v^u&kM:D:]03、在FooTest项目的Build Path中添加Foo项目
6At;`Y
z*Ej9J OA$T M04、参照APIDemo Test项目的manifext.xml来修改FooTest项目的manifest.xml51Testing软件测试网C j5RAo&Q,@
5、编写TestCase(至于测试代码的编写,还在学习中)
`Q;z
Gk
zQ E051Testing软件测试网L~ a7h |:gI