手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >Android学习笔记之应用单元测试实例分析
Android学习笔记之应用单元测试实例分析
摘要:本文实例讲述了Android学习笔记之应用单元测试。分享给大家供大家参考,具体如下:第一步:在AndroidManifest.xml中加入如...

本文实例讲述了Android学习笔记之应用单元测试。分享给大家供大家参考,具体如下:

第一步:在AndroidManifest.xml中加入如下两段代码:

<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相同,代表单元测试框架和当前应用是处于同一个进程中

第二步:编写业务逻辑,即需要被测试的模块

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 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窗口,其中会显示单元测试类的所有的方法

Android学习笔记之应用单元测试实例分析1

然后想要测试哪个方法,则在哪个测试方法上右键鼠标,选择Run As,然后再选择Android JUnit Test即可,如果有异常或者错误,则会出现如下情况:

Android学习笔记之应用单元测试实例分析2

如果是正常的,则会如下:

Android学习笔记之应用单元测试实例分析3

希望本文所述对大家Android程序设计有所帮助。

【Android学习笔记之应用单元测试实例分析】相关文章:

Android开发笔记之:在ImageView上绘制圆环的实现方法

Android应用程序的调试

Android笔记之:App模块化及工程扩展的应用

android 照相功能的简单实例

Android源码学习之组合模式定义及应用

Android 动画之AlphaAnimation应用详解

Android Handler主线程和一般线程通信的应用分析

Android 图片特效处理的方法实例

Android源码学习之观察者模式应用及优点介绍

Android 实用工具之emulator介绍

精品推荐
分类导航