一、编写一个需要被测试的类
//需要测试的类
package com.example.helloworld;
public class Person {
public int add(int i, int j){
return i+j;
}
}
二、编写一个测试类
package junit.test;
import com.example.helloworld.Person;
import android.test.AndroidTestCase;
import android.util.Log;
//测试类需要继承AndroidTestCase
public class TestPerson extends AndroidTestCase {
//测试方法
public void test1(){
Person p = new Person();
int res = p.add(1, 3);
System.out.println(res);
}
}
三、修改AndroidMainFest.xml文件
在标签中添加如下内容
targetPackage里的为测试的包名
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.helloworld"></instrumentation>
在应用程序标签中添加如下内容
<uses-library android:name="android.test.runner"/>
四、打开模拟器,运行测试类中的方法
选中测试类中的方法,右击选择Run as ->Android JUnit Test,即可运行测试方法。