在写完DAO类进行增删改查操作后,可以对其进行测试,具体步骤如下:
如果出现AndroidTestCase无法继承,可查看: link.
1.在系统自带的Android test包下新建test类,也可以直接使用test类
- Android Test: 整合测试,可以运行在设备或虚拟机上
- Test:单元测试,运行在本地开发机上,可以脱离Android运行环境,速度快
本次直接在Android test下的test类中进行测试了:
编写测试类:
@RunWith(AndroidJUnit4.class)
public class ExampleInstrumentedTest {
@Test
public void testInsert(){
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
Dao dao=new Dao(appContext);
dao.insert();
}
@Test
public void testDelete(){
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
Dao dao=new Dao(appContext);
dao.delete();
}
@Test
public void testpdate(){
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
Dao dao=new Dao(appContext);
dao.update();
}
@Test
public void testQuery(){
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
Dao dao=new Dao(appContext);
dao.query();
}
@Test
public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
assertEquals("com.example.datebasedemo", appContext.getPackageName());
}
}
2.进行方法测试
此步骤需要在run-edit configuration中进行设置
进行方法测试:
3.导出数据表查看记录
导出数据表: