引用于:http://www.51testing.com/?17144/viewspace-197415.html

(转自http://www.williamhua.com/2009/05/07/using-junit-in-android-app-development/稍作修改。我自己使用的是SDK1.6的例子,在SDK1.6上运行成功)51Testing软件测试网0KS5`n"z#U~

51Testing软件测试网)\]u$F7x/J1cM

_;W;Lj(x m;e/V E0Android 1.5_r1release notes中专门提到了ADT0.9对于JUnit支持的改进,对于崇尚TDD测试驱动开发)的人来说这无疑是一个好消息,今天就抽点时间说说Android 1.5JUnit集成相关的要点吧。

51Testing软件测试网 hP,UM9?#W}3yZ)G

51Testing软件测试网'f*[uS%k

配置完1.5SDKADT0.9,运行第一个1.5sample的时候我就看到Run as中多出了一个“Android JUnit Test的选项(以前是在Debug中)。51Testing软件测试网N/vW'Em(LK

c.B*H [/_u&^og0可是文档里并没有提到如何在Android project中集成JUnit测试,好在我从Android Developers讨论组里找到了
TDD with Android 1.5这么一个帖子。51Testing软件测试网-x+kipn2`Yt

1U/S[email protected]"iu1|1x7t s0就先来看一看如何把Sample中的test case跑起来吧。
u$o3P4~0{0(多谢Zhaoblog上关于
Android 1.5 pre中运行APIDemo测试的总结)51Testing软件测试网SKF\e,\
1,新建一个Android项目,选择“create project from existing source”,并把路径指向
android-sdk-1.5/platforms/android-1.5/samples/ApiDemos
(@&|(P \B(}Bf ^K0
2,再新建一个Android项目,依然通过“create project from existing source”的方式,这次把路径指向android-sdk-1.5/platforms/android-1.5/samples/ApiDemos/tests51Testing软件测试网 gQ$lc]N!k,q
这时候ADT会报错,因为它无法找到APIDemo项目。右键,选择Properties,在Java Build Path –> Projects中添加APIDemo项目即可。51Testing软件测试网W e@x:]'@
3,以“Android Application”方式运行第一个项目(注意正确设置AVD,第一次运行程序时,选中项目单击右键àRun AsàOpen Run DialogàTarget,选中所用的AVD),APIDemo将被安装到模拟器。51Testing软件测试网g;Ok2eW9}Kt
4,以“Android Application”方式运行第二个项目(注意正确设置AVD,第一次运行程序时,选中项目单击右键àRun AsàOpen Run DialogàTarget,选中所用的AVD),APIDemo Test将被安装到模拟器。51Testing软件测试网&jx [djx2e0h4`"V
5,现在,我们可以通过Dev Tools中的Instrumentation来执行APIDemo Test了。找到“Dev tools”à“Instrumentation”中的”Tests for API Demos.”,点击即可开始测试。
*wUs{R051Testing软件测试网;]EDSn X|z
这时,通过LogCat即可看到测试结果。

:n3OG0x4m(WWlXj051Testing软件测试网F/nj_n wf"a

I/instrumentation(773): INSTRUMENTATION_STATUS_CODE: 1

#W:|[aL2x0

'qF.SF*F0I/TestRunner(780): finished: testAndroidTestCaseSetupProperly(com.example.andr51Testing软件测试网0H!c&|%RpZ L

8E#BC\&L0oid.apis.view.Focus2AndroidTest)51Testing软件测试网 FgO P \knE,J

51Testing软件测试网%u L zTF/B^:F

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

(UDgU\k051Testing软件测试网@|:Lc.i h2e)g

d.apis.view.Focus2AndroidTest)51Testing软件测试网f,~!{1bek

51Testing软件测试网*L4{ P)iv"@

I/instrumentation(773): INSTRUMENTATION_STATUS_RESULT: stream=.51Testing软件测试网rGpW;tcU/iLS|%U

I]@(J%|B0I/instrumentation(773): INSTRUMENTATION_STATUS_RESULT: test=testAndroidTestCas

w:A"vI\051Testing软件测试网+Il1qhe2M1d"u

eSetupProperly

.? N7]Y}8m/k i0

]!NM h)K$O5{A Co0I/instrumentation(773): INSTRUMENTATION_STATUS_RESULT: class=com.example.andro51Testing软件测试网2P LaH K(GrdJ D7~^

o?-moj2Z*r0id.apis.view.Focus2AndroidTest

#V C&t:{4D%A8h051Testing软件测试网f%@Y"ZZ~

I/instrumentation(773): INSTRUMENTATION_STATUS_RESULT: current=2251Testing软件测试网? O%\,m,Vb:^3U

8h_&[wa(Y3~!L3h0I/instrumentation(773): INSTRUMENTATION_STATUS_RESULT: numtests=2251Testing软件测试网!rEu@mgJhH&D

N `8SsgcB x$nx}0I/instrumentation(773): INSTRUMENTATION_STATUS_RESULT: id=InstrumentationTestR

,L/e#Dy_051Testing软件测试网j)`Wo X4X-m-bbm

unner51Testing软件测试网%YQ}-J1v/M

uR^g2U:}0I/instrumentation(773): INSTRUMENTATION_STATUS_CODE: 0

V$Gx6I|t051Testing软件测试网FV3`NEZUd8e

I/instrumentation(773): INSTRUMENTATION_RESULT: stream=

+Ku;i JM;I051Testing软件测试网#kS_ ~ `

I/instrumentation(773): Test results for InstrumentationTestRunner=...........

gn$Eq`0

%y{*E}H/fU0...........51Testing软件测试网(W2d%y,~$D

l'w&e3FSb0I/instrumentation(773): Time: 12.212

+e$z`'Q"ZW!`"U0

8[(f ar/uKo9i0I/instrumentation(773):51Testing软件测试网%VRXV$ZoZ9xh;q

+d#D&N^@'N]k7F[0I/instrumentation(773): OK (22 tests)51Testing软件测试网4iGp l9y+c rA,E p

"Y T,s%Y&L"}051Testing软件测试网_L!A/ii&it|~

51Testing软件测试网&l+TCh&^ZX;zw[

除了通过Dev Tools来执行单元测试,我们还有另外两种方法:51Testing软件测试网z([ j%LGJ
1、通过ADT,在eclipse中执行测试51Testing软件测试网2zc$zs6x
eclipse中选中test项目,直接Run As “Android JUnit Test”既可以,测试结果会以图形化的方式返回。

+QT7A"a3Ne t:m0

C+RF(^)vxMz02、通过sdb shell命令执行测试
A*g)Q-z/D0C_$\4r0在命令行中执行adb shell am instrument -w com.example.android.apis.tests/android.test.InstrumentationTestRunner命令
3D WQ)~2K hIa0
v1mC3m7B0其中,com.example.android.apis.testsAPIDemo Test所在的package
51Testing软件测试网 CG(q^,uuR B

w\,m'ary3E0那么,我们如何创建自己的test项目呢?51Testing软件测试网pF"a7f [0c(As
大致的步骤如下:51Testing软件测试网Q'C%qD4m)xS5Q'_
1、新建一个普通的Android项目,比如项目名为FooPackage
com.foo.bar51Testing软件测试网 T5]$m(I f-X Z
2
、新建一个Test项目,注意把Package填成com.foo.bar.tests,项目名任意,比如FooTestApplication name任意
b |7`YV03、在FooTest项目的Build Path中添加Foo项目
-qoMB3K04、参照APIDemo Test项目的manifext.xml来修改FooTest项目的manifest.xml
l9R*o"E/b6W4Y+o05
、编写TestCase(至于测试代码的编写,还在学习中)

}0X W+Og&Y6eH0

KxO})?|0

VF-}t;R X0

f7e}2H*wLI3l0以前还有一个早期的介绍该操作的文档,现在可以不用这么麻烦了。 :)

}u,]"~*z|7}0

(http://groups.google.com/group/android-beginners/browse_thread/thread/92cae3fad38643a4)

U+l(RBN0

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.

更多相关文章

  1. Android(安卓)源码下利用jni编译自己的项目(参考系统development/
  2. Android(安卓)application单例模式
  3. Android(安卓)package名要区分大小写
  4. Android发布项目到JCenter
  5. Android(安卓)如何连真机测试
  6. 一些优秀的开源项目
  7. Android(安卓)android-common 常用功能和工具集合
  8. 超级助手公告
  9. 把android db文件导入到项目中

随机推荐

  1. Android--高效地加载大图片
  2. 编程总结:Android(安卓)读写文件
  3. Android 日历开发教程[五]
  4. 2020版本Android 开发者学习路线(热门技
  5. Android Binder机制浅析及AIDL的使用
  6. Android消息通知-Notification
  7. Android Studio的使用
  8. Activity状态保存
  9. Android root权限获取原理详解
  10. Android下常见的内存泄露 经典