Android学习笔记:对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>
<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 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);
- }
- }
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学习笔记1 android adb启动失败问题 adb server is out of date. killing...
下面是Android的学习笔记,原文地址. 我是使用adb devices出现如下红字错误, 使用第一种方法方法,结果关掉豌豆荚就可以了. android adb启动失败问题 adb server i ...
- Android学习笔记之Android Studio添加新的Activity
1.创建Android项目工程:AndroidTest 创建过程可参考网上诸多教程. 2.添加新的Activity,步骤如下 a. 在layout文件夹上右键,New-Activity-相应Activ ...
- Android学习笔记之 android:collapseColumns ,android:shrinkColumns 和stretchColumns
摘自:http://blog.csdn.net/sjf0115/article/details/7213565/ TableLayout是一个使用复杂的布局,最简单的用法就仅仅是拖拉控件做出个界面,但 ...
- android学习笔记45——android的数据存储和IO
android的数据存储和IO SharedPreferences与Editor简介 SharedPreferences保存的数据主要是类似于配置信息格式的数据,因此其保存的数据主要是简单的类型的ke ...
- Android学习笔记之Android Studio下创建menu布局文件
1.创建menu文件夹 Android Studio项目中如果没有menu文件夹,在res文件夹右键,new-Android resource directory: 则会弹出下图对话框,在Resour ...
- Android学习笔记_79_ Android 使用 搜索框
1.在资源文件夹下创建xml文件夹,并创建一个searchable.xml: android:searchSuggestAuthorityshux属性的值跟实现SearchRecentSuggesti ...
- android学习笔记(9)android程序调试学习
相应若水老师的第十四课 一,Log日志输出 Log.v(tag,message); //verbose模式,打印最具体的日志 Log.d(tag,message); // ...
- Android学习笔记(1)—Android Studio安装
Android Studio 是一个全新的 Android 开发环境,基于IntelliJ IDEA. 类似 Eclipse ADT,Android Studio 提供了集成的 Android 开发工 ...
- Android学习笔记1——Android开发环境配置
一.JDK配置 Android是基于Java进行开发的,首先需要在电脑上配置JDK(Java Development Kit).在http://www.androiddevtools.cn/下载对应系 ...
- Android学习笔记(36):Android的两种事件处理方式
Android提供了两种事件处理的方式:基于回调的事件处理 和 基于监听的事件处理. 我们来说的easy理解一点: (1)基于回调的事件处理就是继承GUI组件,并重写该组件的事件处理方法.除了一些特定 ...
随机推荐
- Python学习---字符串处理
This world is but a canvas to our imagination. 世界只是我们的想象的画布. ----Apri 22 ''' 题目内容: "Pig Latin&q ...
- Java面试19|过于深入的问题
1.synchronized关键字的实现原理 可以参考:http://www.jianshu.com/p/c5058b6fe8e5 2.CAS是由Unsafe类的compareAndSwap()方法实 ...
- MySQL数据库优化的八种方式
引言: 关于数据库优化,网上有不少资料和方法,但是不少质量参差不齐,有些总结的不够到位,内容冗杂 偶尔发现了这篇文章,总结得很经典,文章流量也很大,所以拿到自己的总结文集中,积累优质文章,提升个人能力 ...
- PHP 字符串变量
PHP 字符串变量 字符串变量用于存储并处理文本. PHP 中的字符串变量 字符串变量用于包含有字符的值. 在创建字符串之后,我们就可以对它进行操作了.您可以直接在函数中使用字符串,或者把它存储在变量 ...
- JS基础速成(一)
.t1 { background-color: #ff8080; width: 1100px; height: 40px } 一.JS的变量 1.变量的声明 var num=1;//使用var生命的变 ...
- windows pe
下载adk https://www.microsoft.com/en-us/download/details.aspx?id=30652 安装 C:\Program Files (x86)\Windo ...
- 到底什么是集群&分布式
对于楼主这样工作一年的菜鸟,偶尔会看到一些文章标题带有"分布式""集群"关键字,然后就懵逼了.最近对这些概念进行了一定的了解,整理了一下思路,在这里分享给各位猿 ...
- 数据库的case when 使用实例
本文作者:苏生米沿 本文地址:http://blog.csdn.net/sushengmiyan/article/details/50471210 需求很简单,我有一个部门和部门的请假申请表.表数据简 ...
- 优先使用TimeUnit类中的sleep()
TimeUnit是什么? TimeUnit是java.util.concurrent包下面的一个类,TimeUnit提供了可读性更好的线程暂停操作,通常用来替换Thread.sleep(),在很长一段 ...
- Dynamics CRM2016 Web API获取实体元数据Picklist属性的Text&Value
通过组织服务中获取实体picklist字段的text和value可以通过RetrieveAttributeRequest实现,但在使用web api的今天该怎么实现,本文即来一探究竟,本篇基于SDK中 ...