(转自http://www.williamhua.com/2009/05/07/using-junit-in-android-app-development/稍作修改。我自己使用的是SDK1.6的例子,在SDK1.6上运行成功)51Testing软件测试网M/MfB!O#T
b GAL.`qY7Z051Testing软件测试网h2L+hv}Android 1.5_r1的release notes中专门提到了ADT0.9对于JUnit支持的改进,对于崇尚TDD(测试驱动开发)的人来说这无疑是一个好消息,今天就抽点时间说说Android 1.5中JUnit集成相关的要点吧。
*Od2U7X6C,I1yJs(ea/l051Testing软件测试网"v+`bz_yWiU配置完1.5的SDKADT0.9,运行第一个1.5的sample的时候我就看到Run as中多出了一个“Android JUnit Test”的选项(以前是在Debug中)。
L K|)UD0
C,Ku}*Pu0可是文档里并没有提到如何在Android project中集成JUnit测试,好在我从Android Developers讨论组里找到了TDD with Android 1.5这么一个帖子。
_5U-F:x1Va/s|0r&O1VJ,j!hv#~
m0就先来看一看如何把Sample中的test case跑起来吧。51Testing软件测试网og"a-_P3{:bt
(多谢Zhao的blog上关于在Android 1.5 pre中运行APIDemo测试的总结)51Testing软件测试网4vIS)~I:U
第1步,新建一个Android项目,选择“create project from existing source”,并把路径指向android-sdk-1.5/platforms/android-1.5/samples/ApiDemos51Testing软件测试网ud"_0C0h+Xs%\
第2步,再新建一个Android项目,依然通过“create project from existing source”的方式,这次把路径指向android-sdk-1.5/platforms/android-1.5/samples/ApiDemos/tests
3v#h+GQw)x0这时候ADT会报错,因为它无法找到APIDemo项目。右键,选择Properties,在Java Build Path –> Projects中添加APIDemo项目即可。51Testing软件测试网4}kn4M)d2{(Uw.m
第3步,以“Android Application”方式运行第一个项目(注意正确设置AVD,第一次运行程序时,选中项目单击右键àRun AsàOpen Run DialogàTarget,选中所用的AVD),APIDemo将被安装到模拟器。
9eZ4B]sxe4E0第4步,以“Android Application”方式运行第二个项目(注意正确设置AVD,第一次运行程序时,选中项目单击右键àRun AsàOpen Run DialogàTarget,选中所用的AVD),APIDemo Test将被安装到模拟器。51Testing软件测试网z0\Q9^0m
第5步,现在,我们可以通过Dev Tools中的Instrumentation来执行APIDemo Test了。找到“Dev tools”à“Instrumentation”中的”Tests for API Demos.”,点击即可开始测试。
BL8j.E `!}"_0
@~i6F/R0这时,通过LogCat即可看到测试结果。
F,t*j*{J'o,}*{09{'G)^'b~]
on\-j0I/instrumentation( 773): INSTRUMENTATION_STATUS_CODE: 1
/i(] jO$p[\7nQO"i051Testing软件测试网f6]d;cqI/TestRunner( 780): finished: testAndroidTestCaseSetupProperly(com.example.andr51Testing软件测试网` Nqtc
j
51Testing软件测试网#up1D3FVUd[;goid.apis.view.Focus2AndroidTest)51Testing软件测试网iLM ]5kPe
51Testing软件测试网4U1k"t;y5\7~i3RI/TestRunner( 780): passed: testAndroidTestCaseSetupProperly(com.example.androi
i9[hT)x].I_0R;fm|9I.et;i l9{!A$m0d.apis.view.Focus2AndroidTest)
b!Uf;z}!V051Testing软件测试网$`&U(J+`OI/instrumentation( 773): INSTRUMENTATION_STATUS_RESULT: stream=.
qP@ZBv051Testing软件测试网$o`x_/|| qL*vU0HoVI/instrumentation( 773): INSTRUMENTATION_STATUS_RESULT: test=testAndroidTestCas51Testing软件测试网+oj7M%i\\c1`?[
.j#E7J l1QJ4K!d0eSetupProperly
c
|V)j+SH0+p%`6H8A
ki9@0I/instrumentation( 773): INSTRUMENTATION_STATUS_RESULT: class=com.example.andro
`iLB'aT0h*[#_;t
l]eTp0id.apis.view.Focus2AndroidTest51Testing软件测试网2@{;v)f u
KIp7xri0I/instrumentation( 773): INSTRUMENTATION_STATUS_RESULT: current=2251Testing软件测试网jno_-Ha R
51Testing软件测试网%{.a9vzB_,j"]~I/instrumentation( 773): INSTRUMENTATION_STATUS_RESULT: numtests=2251Testing软件测试网${/O2ErB2@2t2NU
51Testing软件测试网
U$i*D5L KkP!ZI/instrumentation( 773): INSTRUMENTATION_STATUS_RESULT: id=InstrumentationTestR
5}"A0j0\2qcY.I"\051Testing软件测试网gP N Ee]-F'L4Junner
$| S~\-Nr!C{0&BA+X{0Ah0I/instrumentation( 773): INSTRUMENTATION_STATUS_CODE: 051Testing软件测试网MOD xW
0cxJ$Um5Ks2B0I/instrumentation( 773): INSTRUMENTATION_RESULT: stream=
3U]0A1or1xs0.w0UN"U7kkw0I/instrumentation( 773): Test results for InstrumentationTestRunner=...........51Testing软件测试网clBv'b5F3a
51Testing软件测试网,g(_/e])z:f...........
p
L G:}&`?0A|F#a3ylm0I/instrumentation( 773): Time: 12.21251Testing软件测试网]n+@s5^!e"K
J+h
YJOp.E KW4CE0I/instrumentation( 773):
5T%F!b*N$Q!A0J(Wc"~!Bm3sb,_"@c0I/instrumentation( 773): OK (22 tests)
G(O-a \%o`
[0,K]VbJ0
z4^fpZ6gy051Testing软件测试网x
ZU,TT2l!lu除了通过Dev Tools来执行单元测试,我们还有另外两种方法:
)H6]B
t2ZY+L%I01、通过ADT,在eclipse中执行测试
)N)q+r/T&T9y[J }7@@0在eclipse中选中test项目,直接Run As “Android JUnit Test”既可以,测试结果会以图形化的方式返回。
@Dk
~'a0S^qI5S02、通过sdb shell命令执行测试
.f!Q:il-C#f8@hmqg0在命令行中执行adb shell am instrument -w com.example.android.apis.tests/android.test.InstrumentationTestRunner命令51Testing软件测试网XQ+h/z:Q[@M2c
R#c-Ii2[;JBzr |0其中,com.example.android.apis.tests是APIDemo Test所在的package。51Testing软件测试网af*QDaT`[Cz
6y9az!t:^6b0那么,我们如何创建自己的test项目呢?
3TP\Aner{
[0大致的步骤如下:51Testing软件测试网VG8qPj+T+K
1、新建一个普通的Android项目,比如项目名为Foo,Package为com.foo.bar51Testing软件测试网8uRd(eu6Er
2、新建一个Test项目,注意把Package填成com.foo.bar.tests,项目名任意,比如FooTest,Application name任意
kCr!g,qj03、在FooTest项目的Build Path中添加Foo项目
.X3}u
NO
Zt04、参照APIDemo Test项目的manifext.xml来修改FooTest项目的manifest.xml51Testing软件测试网"vP3bR"~"}
5、编写TestCase(至于测试代码的编写,还在学习中)
#P(V'tj+_&a~\_.F051Testing软件测试网3@p%O:Uho