Android 单元测试

我在学习Android的时候最头特的就是 测试! 虚拟机慢有不舒服,真机也是觉得很慢,因为要频繁的安装应用,所以Android 单元测试的使用就显得尤为重要了。

想实现简单的单元测试不是很难,只要几步就可以完成了:

首先要在清单文件(AndroidManifest.xml)下进行如下声明:

  <!-- 测试环境的指令 放在application节点的外面-->
     <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.tai.mylistview"/>
<!-- 测试环境的指令
放在application节点的里面与activity同级--><uses-library android:name="android.test.runner"/>
可参照如下声明:
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.tai.mylistview"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6     <uses-sdk
 7         android:minSdkVersion="8"
 8         android:targetSdkVersion="14" />
 9     <uses-permission android:name="android.permission.INTERNET"/>
10        <!-- 测试环境的指令 -->
11     <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.tai.mylistview"/>
12     
13     <application
14         android:allowBackup="true"
15         android:icon="@drawable/ic_launcher"
16         android:label="@string/app_name"
17         android:theme="@style/AppTheme" >
18     <!-- 测试环境的指令 -->
19     <uses-library android:name="android.test.runner"/>
20         <activity
21             android:name="com.tai.mylistview.MainActivity"
22             android:label="@string/app_name" >
23             <intent-filter>
24                 <action android:name="android.intent.action.MAIN" />
25 
26                 <category android:name="android.intent.category.LAUNCHER" />
27             </intent-filter>
28         </activity>
29         <activity android:name="com.tai.mylistview.SimpleAdapter_Act"></activity>
30         <activity android:name="com.tai.mylistview.CheckImage"></activity>
31         <activity android:name="com.tai.mylistview.DataBase"></activity>
32         
33         
34      <provider 
35          android:name="com.tai.provider.PersonDBProvider"
36          android:authorities="com.tai.mylistview.personProvider"/>
37     </application>
38 
39 </manifest>
View Code

再就是声明一个类,这个类要继承自AndroidTestCase,剩下的就喝Junit是一样的。

下面这就是对上篇中的数据库操作的测试方法:

 1 package com.tai.test;
 2 
 3 import com.person.dao.PersonDao;
 4 import com.person.dao.PersonDao2;
 5 import com.tai.db.PersonDBOpenHelper;
 6 
 7 import android.test.AndroidTestCase;
 8 
 9 public class Test extends AndroidTestCase {
10 
11     public void testCreateDb() throws Exception
12     {
13         //getContext() 是android 测试类 androidTestCase 提供的方便得到测试 上下文的API
14         PersonDBOpenHelper helper = new PersonDBOpenHelper(getContext());
15         helper.getReadableDatabase();
16     }
17     
18     private PersonDao2 dao;
19     //当测试框架准备好的时候才会执行  一般都坐初始化操作
20     @Override
21     protected void setUp() throws Exception {
22         super.setUp();
23         dao = new PersonDao2(getContext());
24     }
25     //当测试方法执行完之后执行的方法  一般都做些擦屁股的操作
26     @Override
27     protected void tearDown() throws Exception {
28         super.tearDown();
29     }
30     
31     public void testAdd() throws Exception
32     {
33         boolean result = dao.add("张三","999");
34         assertEquals(true, result);
35     }
36     
37     public void testFind() throws Exception
38     {
39         boolean result = dao.find("张三");
40         assertEquals(true, result);
41     }
42     
43     public void testUpdate() throws Exception
44     {
45         boolean result = dao.update("110","张三");
46         assertEquals(true, result);
47     }
48     public void testDelete() throws Exception
49     {
50         boolean result = dao.delete("张三");
51         assertEquals(true, result);
52     }
53 }
test

下面这两个方法很实用。

//当测试框架准备好的时候才会执行 一般都坐初始化操作
@Override
protected void setUp() throws Exception {
super.setUp();
dao = new PersonDao2(getContext());
}
//当测试方法执行完之后执行的方法 一般都做些擦屁股的操作
@Override
protected void tearDown() throws Exception {
super.tearDown();
}

记得再写测试方法的时候想单元框架抛出异常!!

OK简单的测试就这样可以用了。

还有最懒的一个方法  直接生成一个测试工程!

之后就可以直接写测试类  测试方法!

原文地址:https://www.cnblogs.com/mauiie/p/3734813.html