Android--JUnit单元测试
Android--JUnit单元测试
前言
本篇博客说明一下在Android开发中,如何使用JUnit进行单元测试。首先来了解一下什么是JUnit,JUnit测试是白盒测试,即主要是程序员自己对开发的方法进行功能性测试。JUnit是一套框架,Android中也沿用了这一套框架。
JUnit
在Android中使用JUnit测试大致分如下几个步骤:
- 在AndroidManifest.xml中增加对JUnit的支持,并制定测试项目包。
- 在AndroidManifest.xml中<application.../>节点中增加一个<uses-library...>节点,name属性为android.test.runner。
- 在编写待测试方法后,新建一个类,继承AndroidTestCase,在其中编写测试用例代码。
- 鼠标左键在测试用例方法上,Run As→Android JUnit Test。
下面就上面几个步骤,详细讲解一下,新建一个Android项目,在AndroidManifest.xml中,添加一个Instrumentation:
指定Instrumentation的name与TargetPackage:
在<application.../>节点中增加<uses-library android:name="android.test.runner" />
完成后AndroidManifest.xml代码如下:

1 <?xml version="1.0" encoding="utf-8"?>
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="com.example.junittestdemo"
4 android:versionCode="1"
5 android:versionName="1.0" >
6
7 <uses-sdk
8 android:minSdkVersion="8"
9 android:targetSdkVersion="17" />
10
11 <instrumentation
12 android:name="android.test.InstrumentationTestRunner"
13 android:targetPackage="com.example.junittestdemo" >
14 </instrumentation>
15
16 <application
17 android:allowBackup="true"
18 android:icon="@drawable/ic_launcher"
19 android:label="@string/app_name"
20 android:theme="@style/AppTheme" >
21 <uses-library android:name="android.test.runner" />
22
23 <activity
24 android:name="com.example.junittestdemo.MainActivity"
25 android:label="@string/app_name" >
26 <intent-filter>
27 <action android:name="android.intent.action.MAIN" />
28
29 <category android:name="android.intent.category.LAUNCHER" />
30 </intent-filter>
31 </activity>
32 </application>
33
34 </manifest>

编写一个简单的进度百分比计算方法:

1 package com.example.service;
2
3 public class ProgressService {
4 public ProgressService() {
5
6 }
7 public Integer getCurrentProgerss(double current, double max) {
8 Integer i=(int)((current / max) * 100) ;
9 return i;
10 }
11 }

编写一个测试类,这个类需要继承AndroidTestCase,针对百分比方法进行测试:

1 package com.example.junit;
2
3 import android.test.AndroidTestCase;
4 import android.util.Log;
5
6
7 import com.example.service.ProgressService;
8
9 public class ProgressServiceJUnit extends AndroidTestCase {
10 private final String TAG="main";
11
12 public ProgressServiceJUnit() {
13 // TODO Auto-generated constructor stub
14 }
15
16 public void getCurrentProgerssTest(){
17 ProgressService progressService=new ProgressService();
18 Integer pro=progressService.getCurrentProgerss(20, 70);
19 Log.i(TAG, pro.toString());
20 }
21 }

左键getCurrentProgerssTest()方法,选中Android JUnit Test,如果需要调试,可以选择Debug As下的Android JUnit Test:
当执行成功后,会显示绿色,如果是其他颜色,则为出错:
可以在LogCat日志中看到测试结果:
Android--JUnit单元测试的更多相关文章
- Android+Junit单元测试1
学习参考: http://my.oschina.net/liux/blog/52469 http://mobile.51cto.com/android-229614.htm 一,权限配置 <ap ...
- 在Android Studio进行“简单配置”单元测试(Android Junit)
起因 在Android studio 刚出.本人就想弄单元测试,可惜当时Android studio不知道抽什么风(准确来说,应该是我不会弄而已).无法执行到相应的代码.后来今天突然自己又抽风.又想去 ...
- Android系列----JUnit单元测试的使用
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
- Android学习笔记-junit单元测试
我们都知道测试对于程序员来说是必不可少的,所以,做Android程序,也要学会使用junit,这里比着java的junit测试,要稍微复杂一点,需要一些配置,下面饿哦就介绍一下怎样使用junit的测试 ...
- Android Junit测试框架
对应用进行单元测试: 使用Junit测试框架,是正规Android开发的必用技术.在Junit中可以得到组件,可以模拟发送事件和检测程序处理的正确性. 1.配置指令集和函数库: (1)配置指令集,指定 ...
- Android Studio单元测试入门
Android Studio单元测试入门 通常在开发Android app的时候经常会写一些小函数并验证它是否运行正确,通常做法我们是把这个函数放到某个界面(Activity上)执行一下,运行整个工程 ...
- Android:单元测试
通过单元测试的方法可以轻松判断BUG 第一步:首先在AndroidManifest.xml中加入下面红色代码: 打开AndroidManifest.xml,选择instrumentation ,选择N ...
- JUnit单元测试框架的使用
http://blog.csdn.net/mao520741111/article/details/51462215 原文地址 http://www.open-open.com/lib/view/op ...
- Android JUnit 入门指南
自动化单元测试可以做许多的事,并帮你节省时间.它也可以被用作快速检验新建工程或进行冒烟测试.始终,单元测试是作为一种有效的.系统的检验应用程序各功能执行的方式.Android SDK支持JUnit的自 ...
- junit单元测试(keeps the bar green to keeps the code clean)
error是程序错误,failure是测试错误. junit概要: JUnit是由 Erich Gamma (设计模式的创始人)和 Kent Beck (敏捷开发的创始人之一)编写的一个回归测试框架( ...
随机推荐
- Create and Use Custom Attributes
http://www.codeproject.com/Articles/1811/Creating-and-Using-Attributes-in-your-NET-applicat Create a ...
- Centos6.7安装docker1.7.1
Docker当前发布的最新版本已经到了1.11,其官网上针对Centos的的安装需求如下: Docker requires a -bit installation regardless of your ...
- Network-POJ3694并查集+LCA
Network Time Limit: 5000MS Memory Limit: 65536K Description A network administrator manages ...
- Jenkins RCE2(CVE-2016-0788)EXP
国内应该有这个EXP的不多,需要点JAVA基础,安装好maven之后,配置下就可以了,官网也给出了样例,乌云也给了分析.
- 事务码 ListSchema:查看Cube星型结构Schema
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- HTML 方法
姓名输入框:<input type="text" value="默认有值"/> 密码输入框:<input type="text&qu ...
- JVM学习——编译OpenJDK
最近在学习<深入理解java虚拟机 第二版>这本书.书中第一部分建议大家自己编译OpenJDK.抱着学习态度也来编译个玩一玩.下面进入正题. 1.编译环境介绍 操作系统 CentOS Li ...
- UiAutomator环境搭建及详细操作
一.环境搭建 1.1 必备条件 JDK SDK(API高于15) Eclipse(安装ADT插件) ANT(用于编译生成的jar) 安装JDK并添加环境变量 1.2 详细步骤 1.安装JDK并添加环境 ...
- Spring + Mybatis 使用 PageHelper 插件分页
原文:http://www.cnblogs.com/yucongblog/p/5330886.html 先增加maven依赖: <dependency> <groupId>co ...
- 百度地图 判断marker是否在多边形内
昨天画了圆形,判marker是否存在圆形内.今天来画多边形,判断marker在多边形内. 需要引入一个js <script type="text/javascript&quo ...