如何运行android sdk sample中的单元测试

上一篇 / 下一篇  2009-12-02 14:36:39 / 个人分类:Android

我的栏目
  • 栏目:Android

(转自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.`"?3N tZ5t#?

L E`I?:v'p j%Y'{0Android 1.5_r1release notes中专门提到了ADT0.9对于JUnit支持的改进,对于崇尚TDD测试驱动开发)的人来说这无疑是一个好消息,今天就抽点时间说说Android 1.5JUnit集成相关的要点吧。

51Testing软件测试网By D)z;t#`p*n

51Testing软件测试网3azFa&w!b

配置完1.5SDKADT0.9,运行第一个1.5sample的时候我就看到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;@_Y p/IC;t

51Testing软件测试网psc#I@7GI

就先来看一看如何把Sample中的test case跑起来吧。51Testing软件测试网,{FSA9k
(多谢Zhaoblog上关于
Android 1.5 pre中运行APIDemo测试的总结
1{3_X;oQ C;IPg!XHV01,新建一个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-?n03,以“Android Application”方式运行第一个项目(注意正确设置AVD,第一次运行程序时,选中项目单击右键àRun AsàOpen Run DialogàTarget选中所用的AVD),APIDemo将被安装到模拟器。
.Q4a!A3HmVJ&f04,以“Android Application”方式运行第二个项目(注意正确设置AVD,第一次运行程序时,选中项目单击右键àRun AsàOpen Run DialogàTarget选中所用的AVD),APIDemo Test将被安装到模拟器。
[s8e Z Jzr}&Q7H05,现在,我们可以通过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^LZD;^F0I/TestRunner( 780): finished: testAndroidTestCaseSetupProperly(com.example.andr

Cn*R-^6X0

R\5hd1P Z8\nuO0oid.apis.view.Focus2AndroidTest)

n1J(C4Sox051Testing软件测试网'bpq&DE%[!NQ)s

I/TestRunner( 780): passed: testAndroidTestCaseSetupProperly(com.example.androi

#| b}-L'f `-b,?0

6?p'lI,V%G7R4w0d.apis.view.Focus2AndroidTest)

'Si G}4Gw&i~FR0

9k.iUuK0I/instrumentation( 773): INSTRUMENTATION_STATUS_RESULT: stream=.

2y z)y6Wn%t0

B0t7TRcy,_0I/instrumentation( 773): INSTRUMENTATION_STATUS_RESULT: test=testAndroidTestCas

h(_#xVr.E^msX0

_&[ S["A0M0eSetupProperly51Testing软件测试网*] Z!p GJ

{;n+\%}0K&K{WP0I/instrumentation( 773): INSTRUMENTATION_STATUS_RESULT: class=com.example.andro

-na0Aoks Y0

?*_L!^S0id.apis.view.Focus2AndroidTest

P:@ G&|[(b(y051Testing软件测试网4t1|rH o2z!M;I|

I/instrumentation( 773): INSTRUMENTATION_STATUS_RESULT: current=2251Testing软件测试网Q0AYxu:v8}#_

51Testing软件测试网sA U%sm}2T&l

I/instrumentation( 773): INSTRUMENTATION_STATUS_RESULT: numtests=22

_"qjZ3}0

~A X e @ S Nz}s0I/instrumentation( 773): INSTRUMENTATION_STATUS_RESULT: id=InstrumentationTestR51Testing软件测试网Bx#{:LU i+qM#W

u'dY {(l |g0unner

Iv4OG5`+J0

Q'tlQK!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!pG K R$][

...........

/oZP)N#f051Testing软件测试网8[&U-O_/q OpJ6`x

I/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-TS:L6aDP0

9AWl"G9e%T+s@0除了通过Dev Tools来执行单元测试,我们还有另外两种方法:51Testing软件测试网}i'?qL'FZw
1、通过ADT,在eclipse中执行测试51Testing软件测试网W,wWm y b3]+uFY
eclipse中选中test项目,直接Run As “Android JUnit Test”既可以,测试结果会以图形化的方式返回。

51Testing软件测试网]-B9b4LSF VY

51Testing软件测试网sSY5[ db

2、通过sdb shell命令执行测试51Testing软件测试网IA:Z w"O/KO!M
在命令行中执行adb shell am instrument -w com.example.android.apis.tests/android.test.InstrumentationTestRunner命令51Testing软件测试网6i'W J1P/NS!j-AP

/r:M,aY2YqW$g0其中,com.example.android.apis.testsAPIDemo Test所在的package
51Testing软件测试网?{v]PDK}

U E6p-p6uOMN0那么,我们如何创建自己的test项目呢?
8}$H!Z!lU0大致的步骤如下:51Testing软件测试网6{ as#ir{/Yax
1、新建一个普通的Android项目,比如项目名为FooPackage
com.foo.bar
3@7HbRbi+lYb02
、新建一个Test项目,注意把Package填成com.foo.bar.tests,项目名任意,比如FooTestApplication name任意
3v^u&kM:D:]03、在FooTest项目的Build Path中添加Foo项目
6At;`Y z*Ej9J OA$TM04、参照APIDemo Test项目的manifext.xml来修改FooTest项目的
manifest.xml51Testing软件测试网C j5R Ao&Q,@
5
、编写TestCase(至于测试代码的编写,还在学习中)

`Q;z Gk zQ E051Testing软件测试网L~ a7h |:g I

 51Testing软件测试网 E,Cjl&Mb

51Testing软件测试网%W:RK*C%?r

以前还有一个早期的介绍该操作的文档,现在可以不用这么麻烦了。 :)51Testing软件测试网(ZIdrt.l+p I

(http://groups.google.com/group/android-beginners/browse_thread/thread/92cae3fad38643a4)51Testing软件测试网.k?7~/N \{|WVS

I will tell you more simple way. To execute the test cases of sample application provided by google please follow the following steps.
 
1. Open Eclipse and move to your desired workspace.
2. Create new Android Project and select Create Project from existing source.
3. Browse to any sample project for example Snake
4. Build and execute the application to check whether it is properly
installed.
5. In Project Explorer Drag the file SnakeTest.java to source folder.
6. Open AndroidManifest.xml which resides inside the folder test.
7. Copy following lines
 
<instrumentation android:name="android.test.InstrumentationTestRunner"
     android:targetPackage="com.example.android.snake"
     android:label="Snake sample tests">
</instrumentation>
8. Paste these lines to the file AndroidManifest.xml which resides in the application.
9. Add following line in the AndroidManifest
  <uses-library android:name="android.test.runner" />
10. Now save and Run the configuration.
11. Now close the application on emulator.
12. Open Dev Tools
13. Go to Instrumentation
14. Select Snake Sample Tests.
15. The test cases will execute. Check the Logs for the result.
16. You can also execute the following commands in the command prompt
adb shell am instrument –w com.example.android.snake/android.test.InstrumentationTestRunner

TAG: Android JUnit Junit

 

评分:0

我来说两句

Open Toolbar