1.进行单元测试前,需要在manifest文件中添加测试的权限以及使用的测试类
配置文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zizhu.junit"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".JunitTestActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<uses-library android:name="android.test.runner" />
</application>
<uses-permission android:name="android.permission.RUN_INSTRUMENTATION" />
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:label="junit test my app"
android:targetPackage="com.zizhu.junit" />
</manifest>
2.继承自AndroidTestCase即可进行测试
package com.zizhu.test;
import junit.framework.Assert;
import com.zizhu.service.PersonService;
import android.test.AndroidTestCase;
public class PersonServiceTest extends AndroidTestCase {
public void testSave(){
PersonService ps = new PersonService();
ps.save("zizhu");
}
public void testAdd(){
PersonService ps = new PersonService();
Assert.assertEquals(33, ps.add(11, 22));
}
}