Android 单元测试

这篇博客主要介绍了在Android项目中进行单元测试的两个关键步骤:首先,需要在AndroidManifest.xml文件中添加必要的权限和<uses-library>标签;其次,创建测试类,确保其继承自AndroidTestCase,以便进行应用的逻辑测试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值