安卓下的Junit测试主要分三个步骤:
- 导入测试类库:
在项目的AndriodManiFest.xml配置文件的application中导入测试类库
<!-- 1.导入测试类库 -->
<uses-library android:name="android.test.runner"/>
2.配置测试启动类:
在项目的AndriodManiFest.xml配置文件的manifest中配置测试启动类
<!-- 2.配置测试启动类 -->
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.test.junit"></instrumentation>
其中targetPackage为manifest所在的package在AndriodManiFest.xml可以查看
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.junit"
android:versionCode="1"
android:versionName="1.0" >
3.创建一个测试类:
创建一个测试类,让这个类继承自AndroidTestCase
package com.test.junit.Service.Test;
import com.test.junit.Service.PersonService;
import android.test.AndroidTestCase;
/**
*
* 3..在Test 包底下创建一个测试类,让这个类继承自AndroidTestCase
* @author yuxin
*
*/
public class PersonServiseTest extends AndroidTestCase {
public void testGetFirstName() throws Exception {
PersonService ps=new PersonService();
ps.getFirstName();
}
public void testAdd() throws Exception {
PersonService ps=new PersonService();
int actual=ps.Add(2,1);
assertEquals(3, actual);
}
}
具体需要测试哪一个方法可以在Outline中选中该方法右键Debug As>Andriod Junit Test
我的博客网站:http://huyuxin.top/欢迎大家访问!评论!
本文详细介绍在安卓环境下使用JUnit进行单元测试的方法,包括导入测试类库、配置测试启动类及创建测试类等步骤,并通过示例代码展示如何进行具体的测试。
4万+

被折叠的 条评论
为什么被折叠?



