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组件,并重写该组件的事件处理方法.除了一些特定 ...
随机推荐
- MySQL注释中的sql也可能执行
MySql支持三种注释形式:# 和–属于单行注释,注释范围为该行的结尾:/* */注释属于多行注释,此外该种注释还可以实现行内注释.具体的使用情况如下图中所示(四种使用情形): 除此之外,/* */这 ...
- MVP框架 – Ted Mosby的软件架构
作者:Hannes Dorfmann 原文链接 : Ted Mosby – Software Architect 文章出自 : Android开发技术前线 译者 : Mr.Simple 我给这篇关于A ...
- Android简易实战教程--第四十二话《Spinner下拉级联效果》
本篇承接第四十话第四十话<Spinner> 参考博客:http://blog.csdn.net/yayun0516 进入正题: Strings加入第一级数据: <string-arr ...
- Swift基础之两指拉动图片变大变小
我们在使用APP的时候,有时会发现有些图片可以通过两指进行放大.缩小,今天就实现这样的一种效果,比较简单,不喜勿喷.... var imageVi:UIImageView! = nil var ...
- python 3 黑色魔法元类初探
最近读django源码,发现必须了解元类才能理解一些很神奇的行为. 发现元类实际上是控制class的创建过程.比如类B继承某个看似平淡无奇的类A之后,你在类B中定义的属性或方法可能会遭到彻底改变. 假 ...
- 获取imageView的图和背景图
img1和img2都是ImageView,要把img1中的图片显示到img2中 前景(对应src属性) img2.setImageDrawable(img1.getDrawable()); 背景(对应 ...
- Google Dremel数据模型详解(下)
"神秘"的r和d 单从数据结构来看的话,我们可以这样解释r和d的含义.r代表着当前字段与前一字段的关系,是在哪一层合并的,即公共的父结点在哪?举例来说,假如我们重建到了Code=' ...
- Android Studio基本配置
主题设置 File→Settings- 添加第三方主题 网址:http://www.ideacolorthemes.org/home/ File→Import Settings- 设置控制台字体大小 ...
- 指令汇C电子市场开发(一) ActionBar的使用
前话: 在学习开发谷歌电子市场的的时候,我换了一款比较高大上的模拟器--genymotion,首先去genymotion的官网注册下载,然后安装.感觉这款模拟器运行挺快的,哈哈,而且可以直接把应用拖进 ...
- 剑指Offer——Trie树(字典树)
剑指Offer--Trie树(字典树) Trie树 Trie树,即字典树,又称单词查找树或键树,是一种树形结构,是一种的单词.对于每一个单词,我们要判断他出没出现过,如果出现了,求第一次出现在第几个位 ...