十五年测试老手,长期负责WEB\APP 项目测试,目前主要负责团队管理工作。
Android JUnit单元测试基础实例
上一篇 /
下一篇 2011-09-04 16:07:52
/ 个人分类:其它
1. 创建Android项目:AndroidJUnitDemo。51Testing软件测试网4C'cq\5xO
2. 创建被测试的类:Apps。包含两个被测试的方法:methodA()、methodB(),代码如下:
6N,Ij?n,X9P051Testing软件测试网-u~I+zM
z
package com.android.junit;51Testing软件测试网Yy#?-y]b8l:T%{ /** G(TP
N@0 * 被测试的类 7?{&?oX"P^ P0*/51Testing软件测试网&~q
\w6Kb]
]f public class Apps {51Testing软件测试网6{2a8g
k5O&A&K
b;F| public void methodA() { vH!uy-A}(Tu0 System.out.println("---Hello!");51Testing软件测试网(R
|
Dp7v$[:g/U+` Fd } e-@jh rrB)_'XM0 51Testing软件测试网oV(_au&Z public void methodB(String name) {51Testing软件测试网r,OSL0huTs System.out.println("---Hello " + name + "!");51Testing软件测试网 |
to1P1q,k }51Testing软件测试网#\[QD\5k } ;SL@6b!v'H0 |
51Testing软件测试网Hk3mRW jNp)c9z
3. 创建测试类:AndroidJUnitTester,51Testing软件测试网Y4} Mf,W}6K
Android测试类需要继承android.test.AndroidTestCase,代码如下51Testing软件测试网#^2kn+h
QQ
51Testing软件测试网k`P:SCr^S
package com.android.junit;51Testing软件测试网C4N)v\8U&qXxM import com.android.junit.Apps; W{:F4Jh9Q0import android.test.AndroidTestCase; N4wj6l^
v|0/**51Testing软件测试网1](rf'i8n d7P'X:I * Android JUnit测试类 ma
Q
NFZ9V]/f0*/51Testing软件测试网 w"TJK?[%Q public class AppsTester extends AndroidTestCase {51Testing软件测试网Wo!zG)FBi"A Apps a = null;51Testing软件测试网O l{+d"M@c;\&~#{ protected void setUp() throws Exception {51Testing软件测试网5R!nH
]Jsm a = new Apps(); BidD's
TM0 } b S1]R-Lti0 // methodA()测试方法
m9FL.Ie]e d0 public void testMethodA() {51Testing软件测试网q o:|V
Y ce#R a.methodA(); |6t4ZS(H'D5I5Xyf.|]0 }51Testing软件测试网fzh9_:v;y w;[iL&Ru,^ Zw)F0 // methodB()测试方法 2VF^Q|Zl0 public void testMethodB() {51Testing软件测试网P
lt nLkX.j a.methodB("Android"); 4sO]0bQ1a.IIU0 } "I
Qx1`#Te"U4RgOR0}51Testing软件测试网NuvA|h,M!~ |
'H gJrS$E2xs04. 配置AndroidManifest.xml文件51Testing软件测试网S GY@Zu"b)l-J
1) <uses-libraryandroid:name="android.test.runner" />51Testing软件测试网-o2b4gZA!K
说明:与<activity>位置同级
)A~/U{Zy|0 2) <instrumentationandroid:targetPackage="com.android.junit" 51Testing软件测试网 otMB7f
android:name="android.test.InstrumentationTestRunner"51Testing软件测试网
Lm6Xn S}bJ"j-wP
android:label="AndroidJUnit测试" />51Testing软件测试网3t^1k4Yk*s
说明:与<application>标签同级,targetPackage属性与上面mainfest的package属性内容相同即可 -->
p C%H~0a7P0 5. 测试方法:打开Eclipse->Outline试图,右键测试方法->Run as -> Android JUnit Test
1^*N*L)I!s7~U(^,[0 6. 实例代码下载地址:http://download.csdn.net/source/3504290
|7C/]j3f]$d0
收藏
举报
TAG:
Android
android
JUnit
Junit
JUNIT
junit