快速搭建Android jUnit测试环境
测试的分类:
- 源代码透明度 :
- 黑盒测试:不知道源代码,关系用户操作。
- 白盒测试:根据源代码编写测试用例。
- 测试粒度:
- 方法测试:function test
- 单元测试:jUnit test
- 集成测试:intergration test
- 测试次数:
- 冒烟测试:smoke test(Android monkey)
- 压力测试:pressure test
Android monkey:
Android monkey是Android中用来进行smoke测试的一个工具,模拟一只猴子在胡乱点击Android手机,使用方法:
cmd--> adb shell -->monkey [次数]
【前提是配置了../sdk/platform-tools/的环境变量】
配置jUnit环境:
1.编写测试类继承AndroidTestCase类,编写测试方法的时候要将异常抛出来。
public class TestArithmeticService extends AndroidTestCase { /** * 将add方法测试代码,需要将异常抛出去 * * @throws Exception */ public void testAdd() throws Exception { ArithmeticService service = new ArithmeticService(); int retsult = service.add(5, 15); assertEquals(22, retsult); }}
2.配置AndroidManifest.xml文件
<!-- 在manifest节点内配置Android的jUnit测试环境 --><!-- targetPackage指定Android应用程序包名 --><instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.meritit.dm.junittest" > </instrumentation><!-- 在application节点内创建uses-library --><uses-library android:name="android.test.runner" />
AndroidManifest.xml文件:
<? xml version ="1.0" encoding= "utf-8" ?>< manifest xmlns:android ="http://schemas.android.com/apk/res/android" package ="com.meritit.dm.junittest" android:versionCode ="1" android:versionName ="1.0" > <!-- 在manifest节点内配置Android的jUnit测试环境 --> <instrumentation android:name ="android.test.InstrumentationTestRunner" android:targetPackage ="com.meritit.dm.junittest" > </instrumentation > <uses-sdk android:minSdkVersion ="8" android:targetSdkVersion ="19" /> <application android:allowBackup ="true" android:icon ="@drawable/ic_launcher" android:label ="@string/app_name" android:theme ="@style/AppTheme" > <!-- 在application节点内创建uses-library --> < uses-library android:name ="android.test.runner" /> < activity android:name ="com.meritit.dm.junittest.MainActivity" android:label ="@string/app_name" > < intent-filter> < action android:name ="android.intent.action.MAIN" /> < category android:name ="android.intent.category.LAUNCHER" /> </ intent-filter> </ activity> </application ></ manifest>另外:可以创建Android Test Project,以上配置会自动生成。