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组件,并重写该组件的事件处理方法.除了一些特定 ...
随机推荐
- 【转载】给想要入门渗透的人的忠告——schiz0wcingU
最近发现很多拥有黑客梦想的年轻人在群里或者论坛里,找"师傅"或者学一些所谓的"社工" 这些找师傅的人当中,有极大一部分人是还在上学的学生,自然也就没有收入来源, ...
- python socket网络编程之粘包问题详解
一,粘包问题详情 1,只有TCP有粘包现象,UDP永远不会粘包 你的程序实际上无权直接操作网卡的,你操作网卡都是通过操作系统给用户程序暴露出来的接口,那每次你的程序要给远程发数据时,其实是先把数据从用 ...
- Centos 7安装MYSQL
1.下载RPM源 直接使用yum命令下载mysql来进行安装是不能成功的,安装过程会有问题,这里需要使用rpm命令来先进下载.下载路径为: http://dev.mysql.com/get/mysql ...
- 在ubuntu上安装最新稳定版本的node及npm
背景 通过ubuntu官方apt安装工具安装的node是最新LTS版本的,而本人是个有点强迫症的人,喜欢追求新的东西,也就是想方设法想要去安装最新版本的node,所以本文也就产生了,附上ubuntu安 ...
- IntelliJ IDEA下Git的配置与使用(命令行下)
1. 安装Git并配置好Git 安装与配置参见Git与码云(Git@OSC)入门-如何在实验室和宿舍同步你的代码(1)中的2.在本机安装Git与3.1 配置git. 2. 创建远程仓库 在gitee. ...
- postgresql添加字段
ALTER TABLE jiangan_config ADD COLUMN article text NOT NULL DEFAULT ''; pg_dump -s database_name -t ...
- Spring Security安全框架入门篇
一.Spring Security相关概念 1.1..Spring Security介绍: Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安 ...
- [boost] build boost with intel compiler 16.0.XXX
Introduction There are few information about how to compile boost with Intel compiler. This article ...
- 改进版getpass库
编程伊始 正式实施 改进版 源码 以数字显示 以自定义分隔符delimiter显示 如何使用 下载及安装 在您的代码中使用 源码下载 总结 用过Linux的都知道,尤其是进行使用包管理软件类似于apt ...
- linux中probe函数中传递的参数来源(上)
点击打开链接 上一篇中,我们追踪了probe函数在何时调用,知道了满足什么条件会调用probe函数,但probe函数中传递的参数我们并不知道在何时定义,到底是谁定义的,反正不是我们在驱动中定义的(当然 ...