(转自http://www.williamhua.com/2009/05/07/using-junit-in-android-app-development/稍作修改。我自己使用的是SDK1.6的例子,在SDK1.6上运行成功)51Testing软件测试网;|)v*_cw*M
&s~GE[V0'Tk:~aKY-Z&J0Android 1.5_r1的release notes中专门提到了ADT0.9对于JUnit支持的改进,对于崇尚TDD(测试驱动开发)的人来说这无疑是一个好消息,今天就抽点时间说说Android 1.5中JUnit集成相关的要点吧。
51Testing软件测试网{C_J` x ?
51Testing软件测试网Jp!w[-BQ
P*S W配置完1.5的SDKADT0.9,运行第一个1.5的sample的时候我就看到Run as中多出了一个“Android JUnit Test”的选项(以前是在Debug中)。51Testing软件测试网 r2y3e/h J
B
t
51Testing软件测试网k1y^&O4U'e,qE*G
可是文档里并没有提到如何在Android project中集成JUnit测试,好在我从Android Developers讨论组里找到了TDD with Android 1.5这么一个帖子。
` Ii.G+`F TA0 bM R4{rQ,r0就先来看一看如何把Sample中的test case跑起来吧。
7E0`,B4oL{"sm0(多谢Zhao的blog上关于在Android 1.5 pre中运行APIDemo测试的总结)51Testing软件测试网"n2Q
j:f7^%S"N7p:A.j
第1步,新建一个Android项目,选择“create project from existing source”,并把路径指向android-sdk-1.5/platforms/android-1.5/samples/ApiDemos51Testing软件测试网u3p]"^:Eh7_
第2步,再新建一个Android项目,依然通过“create project from existing source”的方式,这次把路径指向android-sdk-1.5/platforms/android-1.5/samples/ApiDemos/tests
RDCUR0这时候ADT会报错,因为它无法找到APIDemo项目。右键,选择Properties,在Java Build Path –> Projects中添加APIDemo项目即可。
,UU2n)h$kVst_-i&j0第3步,以“Android Application”方式运行第一个项目(注意正确设置AVD,第一次运行程序时,选中项目单击右键àRun AsàOpen Run DialogàTarget,选中所用的AVD),APIDemo将被安装到模拟器。51Testing软件测试网 [C8`0^'^?yi
}
第4步,以“Android Application”方式运行第二个项目(注意正确设置AVD,第一次运行程序时,选中项目单击右键àRun AsàOpen Run DialogàTarget,选中所用的AVD),APIDemo Test将被安装到模拟器。51Testing软件测试网&g|`Ks)d8I#l&F
第5步,现在,我们可以通过Dev Tools中的Instrumentation来执行APIDemo Test了。找到“Dev tools”à“Instrumentation”中的”Tests for API Demos.”,点击即可开始测试。
5n
XZA uhv Vn0
p*[7V7AF/S[0s Q0这时,通过LogCat即可看到测试结果。
-f(X&~LJ
K051Testing软件测试网g
K1WF8~s!q(Z0W3SI/instrumentation( 773): INSTRUMENTATION_STATUS_CODE: 151Testing软件测试网hQ[j8D8Ob3y
51Testing软件测试网zq6u5_;XRY0x8F1~SI/TestRunner( 780): finished: testAndroidTestCaseSetupProperly(com.example.andr
*G Ahj,TZ g;u0"YWFv?)p9W0oid.apis.view.Focus2AndroidTest)
n5K\e6[051Testing软件测试网(s
{t ~c
jI/TestRunner( 780): passed: testAndroidTestCaseSetupProperly(com.example.androi
6\g)XU y:DK/[0GDo(a'c.A#X0d.apis.view.Focus2AndroidTest)51Testing软件测试网*D @Y.cZ8Ky
)^r;y
Y R"@&u@0I/instrumentation( 773): INSTRUMENTATION_STATUS_RESULT: stream=.
2r&?$jl$b/qm1f05\1Y5C@}!n8V s[0I/instrumentation( 773): INSTRUMENTATION_STATUS_RESULT: test=testAndroidTestCas51Testing软件测试网`|!s#g2@3}#M
51Testing软件测试网.?i}h5{&uQYeSetupProperly
0p&]6a$h-W
~){mO&d0"ck5|7N'yy%G`0I/instrumentation( 773): INSTRUMENTATION_STATUS_RESULT: class=com.example.andro
W?X
XQNc0.}h0?e$A1Nj8vx1X0id.apis.view.Focus2AndroidTest
0s3}Q0A$O$u$b0;EK)f:gF@-m0I/instrumentation( 773): INSTRUMENTATION_STATUS_RESULT: current=2251Testing软件测试网r!Nabc
51Testing软件测试网Llz1V-r d}3z.HI/instrumentation( 773): INSTRUMENTATION_STATUS_RESULT: numtests=22
tA'H7jE8A5}Lc.s0cVf:qT0I/instrumentation( 773): INSTRUMENTATION_STATUS_RESULT: id=InstrumentationTestR51Testing软件测试网\K
bm#R2~;C`
51Testing软件测试网R~;H"g7PJRunner
0m7nnPm
rz
}?051Testing软件测试网Yoo/_ z
}1@I/instrumentation( 773): INSTRUMENTATION_STATUS_CODE: 051Testing软件测试网`7Kk'A5R_%E%\
51Testing软件测试网^M'E'as;OJRw;xI/instrumentation( 773): INSTRUMENTATION_RESULT: stream=51Testing软件测试网'P6g%Cw~6H+LBZbt
xr^nW2y!J)Eubn0I/instrumentation( 773): Test results for InstrumentationTestRunner=...........51Testing软件测试网!u2E
}
c+X:Em'e
51Testing软件测试网%~eyumDF5oW...........
/}|1j(CR051Testing软件测试网}^#S:TM,g+xI/instrumentation( 773): Time: 12.21251Testing软件测试网y,||6q/X
51Testing软件测试网z p$t#e sb0c'FI/instrumentation( 773):
2rA5\^$})y$B1}051Testing软件测试网9{I$r m&U.]4d7gI/instrumentation( 773): OK (22 tests)51Testing软件测试网EI;e:N@2l*u;h%lq3{-K
51Testing软件测试网
N,On$iDNVy1C 51Testing软件测试网1bdvq']-@W
51Testing软件测试网#h2f#~'wW ^p?;Z:y除了通过Dev Tools来执行单元测试,我们还有另外两种方法:
'HgP:Af01、通过ADT,在eclipse中执行测试
/ZQb'aO
EG;T-~0在eclipse中选中test项目,直接Run As “Android JUnit Test”既可以,测试结果会以图形化的方式返回。
51Testing软件测试网0c6},R{'h$H@
%k0F&fv PBs1D02、通过sdb shell命令执行测试
&q4O/C'|&s#s+b \0在命令行中执行adb shell am instrument -w com.example.android.apis.tests/android.test.InstrumentationTestRunner命令51Testing软件测试网)P-DG"JWT-QT
5Tx9h9c9Y3X:f0其中,com.example.android.apis.tests是APIDemo Test所在的package。
}`-o`!yUa&['q051Testing软件测试网T+mY7\t GFW那么,我们如何创建自己的test项目呢?
y]+];bi:~,LL0大致的步骤如下:
yKr7Jc G:\(c01、新建一个普通的Android项目,比如项目名为Foo,Package为com.foo.bar
t+_{!?
\V/w]02、新建一个Test项目,注意把Package填成com.foo.bar.tests,项目名任意,比如FooTest,Application name任意
$KujN4RG03、在FooTest项目的Build Path中添加Foo项目
s
n7cZM/m04、参照APIDemo Test项目的manifext.xml来修改FooTest项目的manifest.xml
2[5h n$NjR8m05、编写TestCase(至于测试代码的编写,还在学习中)51Testing软件测试网&q.A N9FT&v!f
tlK \Pt Iw0