Android学习笔记:对Android应用进行单元测试
2015-06-13 15:24
465 查看
第一步:在AndroidManifest.xml中加入如下两段代码:
[java] view plaincopyprint?<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pccw"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!—添加代码1-->
<uses-library android:name="android.test.runner"/>
</application>
<!—添加代码2-->
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.pccw" android:label="aaa"/>
</manifest>
1. < uses-library android:name="android.test.runner"/>代表把单元测试框架中的一些依赖库引入进来
2. < instrumentation android:name="android.test.InstrumentationTestRunner"android:targetPackage="com.pccw" android:label="aaa"/>代表配置单元测试框架的启动装置,启动装置有好几个类,可以选择,一般情况下我们使用上面这个。
3. targetPackage与上面的package相同,代表单元测试框架和当前应用是处于同一个进程中
第二步:编写业务逻辑,即需要被测试的模块
[java] view plaincopyprint?public class PersonService {
public void save(String name){
String sub = name.substring(6);
}
public int add(int a, int b){
return a+b;
}
}
第三步:编写单元测试代码
[java] view plaincopyprint?public class PersonServiceTest extends AndroidTestCase {
public void testSave() throws Exception {
PersonService service = new PersonService();
service.save(null);
}
public void testAdd() throws Exception {
PersonService service = new PersonService();
int result = service.add(1, 2);
Assert.assertEquals(3, result);
}
}
第四步:打开eclipse中的outline窗口,其中会显示单元测试类的所有的方法
然后想要测试哪个方法,则在哪个测试方法上右键鼠标,选择Run As,然后再选择Android JUnit Test即可,如果有异常或者错误,则会出现如下情况:
如果是正常的,则会如下:
[java] view plaincopyprint?<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.pccw"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!—添加代码1-->
<uses-library android:name="android.test.runner"/>
</application>
<!—添加代码2-->
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.pccw" android:label="aaa"/>
</manifest>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.pccw" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!—添加代码1--> <uses-library android:name="android.test.runner"/> </application> <!—添加代码2--> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.pccw" android:label="aaa"/> </manifest>
1. < uses-library android:name="android.test.runner"/>代表把单元测试框架中的一些依赖库引入进来
2. < instrumentation android:name="android.test.InstrumentationTestRunner"android:targetPackage="com.pccw" android:label="aaa"/>代表配置单元测试框架的启动装置,启动装置有好几个类,可以选择,一般情况下我们使用上面这个。
3. targetPackage与上面的package相同,代表单元测试框架和当前应用是处于同一个进程中
第二步:编写业务逻辑,即需要被测试的模块
[java] view plaincopyprint?public class PersonService {
public void save(String name){
String sub = name.substring(6);
}
public int add(int a, int b){
return a+b;
}
}
public class PersonService { public void save(String name){ String sub = name.substring(6); } public int add(int a, int b){ return a+b; } }
第三步:编写单元测试代码
[java] view plaincopyprint?public class PersonServiceTest extends AndroidTestCase {
public void testSave() throws Exception {
PersonService service = new PersonService();
service.save(null);
}
public void testAdd() throws Exception {
PersonService service = new PersonService();
int result = service.add(1, 2);
Assert.assertEquals(3, result);
}
}
public class PersonServiceTest extends AndroidTestCase { public void testSave() throws Exception { PersonService service = new PersonService(); service.save(null); } public void testAdd() throws Exception { PersonService service = new PersonService(); int result = service.add(1, 2); Assert.assertEquals(3, result); } }
第四步:打开eclipse中的outline窗口,其中会显示单元测试类的所有的方法
然后想要测试哪个方法,则在哪个测试方法上右键鼠标,选择Run As,然后再选择Android JUnit Test即可,如果有异常或者错误,则会出现如下情况:
如果是正常的,则会如下:
相关文章推荐
- Android学习笔记:对Android应用进行单元测试
- android dtmf
- 【Ubuntu】Android Studio配置
- Android使ScrollView显示到底部或任意位置
- Android学习记录<五>
- Android实战简易教程-第十六枪(LineChart实现数据趋势展示)
- 实现记住用户名和密码(加了一个引导页)
- android studio 安装启动出现 The environment variable Java_home does not point to JVm。。
- android 最下面菜单框的拉动效果
- Android学习心得(7) --- 迭代器Iterator反编译smali分析
- android-adb通用
- 如何让android的service一直在后台运行?
- Android Studio开发入门-引用jar及so文件
- Android StateMachine解析( 1 )
- AndroidStudio_local path doesn't exist解决办法
- android5.0(Lollipop) BLE Central(Scanner)牛刀小试
- mac下androidstudio下ndk开发简单操作流程
- android5.0(Lollipop) BLE Central牛刀小试
- 编译Android2.3.5源码,提供源码
- Android程序的入口点及深入生命周期及fragment的使用及viewpager