Android--JUnit单元测试

前言

  本篇博客说明一下在Android开发中,如何使用JUnit进行单元测试。首先来了解一下什么是JUnit,JUnit测试是白盒测试,即主要是程序员自己对开发的方法进行功能性测试。JUnit是一套框架,Android中也沿用了这一套框架。

JUnit

  在Android中使用JUnit测试大致分如下几个步骤:

  1. 在AndroidManifest.xml中增加对JUnit的支持,并制定测试项目包。
  2. 在AndroidManifest.xml中<application.../>节点中增加一个<uses-library...>节点,name属性为android.test.runner。
  3. 在编写待测试方法后,新建一个类,继承AndroidTestCase,在其中编写测试用例代码。
  4. 鼠标左键在测试用例方法上,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单元测试的更多相关文章

  1. Android+Junit单元测试1

    学习参考: http://my.oschina.net/liux/blog/52469 http://mobile.51cto.com/android-229614.htm 一,权限配置 <ap ...

  2. 在Android Studio进行“简单配置”单元测试(Android Junit)

    起因 在Android studio 刚出.本人就想弄单元测试,可惜当时Android studio不知道抽什么风(准确来说,应该是我不会弄而已).无法执行到相应的代码.后来今天突然自己又抽风.又想去 ...

  3. Android系列----JUnit单元测试的使用

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  4. Android学习笔记-junit单元测试

    我们都知道测试对于程序员来说是必不可少的,所以,做Android程序,也要学会使用junit,这里比着java的junit测试,要稍微复杂一点,需要一些配置,下面饿哦就介绍一下怎样使用junit的测试 ...

  5. Android Junit测试框架

    对应用进行单元测试: 使用Junit测试框架,是正规Android开发的必用技术.在Junit中可以得到组件,可以模拟发送事件和检测程序处理的正确性. 1.配置指令集和函数库: (1)配置指令集,指定 ...

  6. Android Studio单元测试入门

    Android Studio单元测试入门 通常在开发Android app的时候经常会写一些小函数并验证它是否运行正确,通常做法我们是把这个函数放到某个界面(Activity上)执行一下,运行整个工程 ...

  7. Android:单元测试

    通过单元测试的方法可以轻松判断BUG 第一步:首先在AndroidManifest.xml中加入下面红色代码: 打开AndroidManifest.xml,选择instrumentation ,选择N ...

  8. JUnit单元测试框架的使用

    http://blog.csdn.net/mao520741111/article/details/51462215 原文地址 http://www.open-open.com/lib/view/op ...

  9. Android JUnit 入门指南

    自动化单元测试可以做许多的事,并帮你节省时间.它也可以被用作快速检验新建工程或进行冒烟测试.始终,单元测试是作为一种有效的.系统的检验应用程序各功能执行的方式.Android SDK支持JUnit的自 ...

  10. junit单元测试(keeps the bar green to keeps the code clean)

    error是程序错误,failure是测试错误. junit概要: JUnit是由 Erich Gamma (设计模式的创始人)和 Kent Beck (敏捷开发的创始人之一)编写的一个回归测试框架( ...

随机推荐

  1. SpringMVC集成AOP错误:java lang classnotfoundexception org aspectj lang joinpoint

    记录自己出现的问题,Spring AOP 使用测试类测试没问题,在SpringMVC启动服务器时出现java lang classnotfoundexception org aspectj lang ...

  2. WPF:设置MenuItem多种不同状态图标

    需求描述: 给MenuItem内部的子Image设置默认图标(鼠标leave).鼠标hover图标.和选中时的图标. 注:是给Menu内个别MenuItem修改,并且是弹出子菜单. 问题描述: 1)前 ...

  3. Iptables防火墙

    1 位置 使用vim /usr/sysconfig/iptables 2 启动.关闭.保存 service iptables stop service iptables start service i ...

  4. python学习之认识字符串

    1.打印字符串 >>> print ("hello world") hello world 2.使用“/"进行符号转义 >>> pri ...

  5. [poj3378] Crazy Thairs (DP + 树状数组维护 + 高精度)

    树状数组维护DP + 高精度 Description These days, Sempr is crazed on one problem named Crazy Thair. Given N (1 ...

  6. Json不知道key值情况下获取第一个键值对

    JObject jsonData = new JObject(); jsonData.Add("1", "1"); jsonData.Add("2&q ...

  7. jquery总结04-DOM节点操作

    一般js操作节点 ①创建节点(元素文本)document.createElement  innerHTML ②添加属性 setAttribute ③加入文档 appendChild 操作繁琐还有兼容性 ...

  8. CSS3之尖角标签

    如图所示,Tag标签的制作通常使用背景图片,现在用CSS3代码就能实现尖角效果(需浏览器支持CSS3属性). 运用CSS3样式实现尖角标签,只需要写简单的HTML结构和CSS样式. <p> ...

  9. 从webRoot中下载Excel

    @RequestMapping("downLoad") public void downLoad(Offsupervise off1,HttpServletRequest requ ...

  10. (转) C++中基类和派生类之间的同名函数的重载问题

    下面有关派生类与基类中存在同名函数 fn: class A { public: void fn() {} void fn(int a) {} }; class B : public A { publi ...