1.在AndroidManifest.xml添加权限问题:
a. <instrumentation android:targetPackage="com.example.hz15" android:name="android.test.InstrumentationTestRunner"></instrumentation>
b.在activity之外添加<uses-library android:name="android.test.runner"/>
2.创建测试类:
继承AndroidTestCase类
@Override public void setUp() throws Exception { super.setUp();//写测试方法之前执行的条件 } @Override public void tearDown() throws Exception { super.tearDown();//写测试方法之后执行关闭功能 }3.随便看下例子:
package com.example.test;
import java.util.List;
import com.example.domain.Person;
import com.example.service.DBservice;
import com.example.service.PersonService;
import android.test.AndroidTestCase;
import android.util.Log;
public class PersonTest extends AndroidTestCase {
private static final String TAG = "PersonTest";
public void testCreateDB() throws Exception{
DBservice dbservice = new DBservice(getContext());
dbservice.getWritableDatabase();
}
public void testAddData() throws Exception{
PersonService ps = new PersonService(getContext());
for(int i = 10; i < 20; i++){
Person person = new Person(i, "hhzz"+i, "159123456"+i,0);
ps.addDate(person);
}
}
public void testDeleteDate() throws Exception{
PersonService ps = new PersonService(getContext());
Integer id = 9;
ps.deleteData(id);
}
public void testUpdateData() throws Exception{
PersonService ps = new PersonService(getContext());
//
// Person person = new Person();
//
// person.setId(2);
// person.setName("xia");
// person.setPhone("123456789");
//
// ps.updateData(person);
ps.updateData(new Person(1, "HZ1", "6456546", 100));
ps.updateData(new Person(2, "HZ2", "123778456789", 90));
}
public void testFind() throws Exception{
PersonService ps = new PersonService(getContext());
Person result = ps.findData(5);
Log.i(TAG, result.toString());
}
public void testGetCount() throws Exception{
PersonService ps = new PersonService(getContext());
long count = ps.getCount();
Log.i(TAG, String.valueOf(count));
}
public void testGetScrollData()throws Exception{
PersonService ps = new PersonService(getContext());
List<Person> persons = ps.getScrollDate(0, 9);
for(Person person:persons){
Log.i(TAG, person.toString());
}
}
public void testPayment() throws Exception{
PersonService ps = new PersonService(getContext());
ps.payment();
}
}