转的:http://www.blogjava.net/qileilove/archive/2014/05/19/413824.html

Android中如何使用JUnit进行单元测试

在我们日常开发android app的时候,需要不断地进行测试,所以使用JUnit测试框架显得格外重要,学会JUnit可以加快应用的开发周期。
实际开发中,开发android软件的过程需要不断的进行测试。而是用Junit测试框架,则是正规android开发的必用技术,在Junit中可以得到组件,可以模拟发送事件和检测程序处理的正确性。
比如,若想验证一个自定义类中的某个方法时,则可以在单元测试中创建这个类对象,并给定适合参数调用该类方法。
  Android中建立JUnit测试环境有以下两种方法。
 
Android单元测试具体方法如下:
(1).创建一个类继承AndroidTestCase,该类为一个单元测试类。
(2).在AndroidMainfest中声明instrumentation分支。(把单元测试库引进到此项目中)
(3).在<application >中声明<uses-library />分支。
(4).双击AndroidTestCase中的方法名,右键选择Runas--Android JUnit Test,运行该方法。
 
 
  一、直接在需要被测试的工程中新建测试类
  集成步骤:
  1.在androidManifest.xml文件中添加以下代码:
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.junittest" android:label="@string/app_name"
></instrumentation>
  <uses-library android:name="android.test.runner"/>
  以上代码配置是添加测试指令和引入测试环境,完整的清单文件如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.junittest"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.example.junittest" android:label="@string/app_name"
></instrumentation>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-library android:name="android.test.runner"/>
<activity
android:name="com.example.junittest.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>
</application>
</manifest>
 2.新建一个测试测试类并继承AndroidTestCase类,编写测试方法,在测试方法内使用断言assert来测试要测试的方法。
  3.点击右面的大纲视图,选择要测试的方法,右键,run as --->Android JUnit test
  下面通过一个简单的示例来演示一下如何使用JUnit单元测试
  1、先创建简单的待测试类Calculator.java
package com.example.junittest;
public class Calculator {
public int add(int x,int y){
return x+y;
}
public int sub(int x,int y){
return x-y;
}
public int divide(int x,int y){
return x/y;
}
public int multiply(int x,int y){
return x*y;
}
}
  2、创建一个测试类,此类需要继承自AndroidTestCase
  示例代码如下:
package com.example.test;
import junit.framework.Assert;
import com.example.junittest.Calculator;
import android.test.AndroidTestCase;
import android.util.Log;
public class CalculatorTester extends AndroidTestCase {
private static final String TAG = CalculatorTester.class.getSimpleName();
private Calculator calculator;
/**
*  This method is invoked before any of the test methods in the class.
*  Use it to set up the environment for the test (the test fixture. You can use setUp() to instantiate a new Intent with the action ACTION_MAIN. You can then use this intent to start the Activity under test.
*/
@Override
protected void setUp() throws Exception {
Log.e(TAG, "setUp");
calculator = new Calculator();
super.setUp();
}
/**
* 测试Calculator的add(int x, int y)方法
* 把异常抛给测试框架
* @throws Exception
*/
public void testAdd() throws Exception{
int result = calculator.add(3, 5);
Assert.assertEquals(8, result);
}
/**
* 测试Calculator的divide(int x, int y)方法
* 把异常抛给测试框架
* @throws Exception
*/
public void testDivide() throws Exception{
int result = calculator.divide(10, 0);
Assert.assertEquals(10, result);
}
/**
* This method is invoked after all the test methods in the class.
* Use it to do garbage collection and to reset the test fixture.
*/
@Override
protected void tearDown() throws Exception {
Log.e(TAG, "tearDown");
calculator = null;
super.tearDown();
}
}
  一个好的习惯是每个测试方法都抛出异常:throws Exception,然后通过Assert对结果进行断言。
  3、通过大纲视图运行测试方法
  绿条表示测试通过,在代码中我们测试的时3+5是否等于8,所以结果肯定是通过的,如果我们把assertEquals()中的8改为5的话,会出现以下结果:
  红条表示测试没通过,点击右边的错误信息可以定位到出错的代码行。  二、创建一个专门用于测试的工程
  推荐创建专门的测试工程,因为这样可以降低代码的耦合度。
  集成步骤:
  1.新建工程,选择new ---- >   other  ---->android Test Project
  2.选择要测试的工程
  3.接着和第一种建立测试类的方法是一样的,这里比较简单就略过了。
  使用这种方法的话,androidManifest.xml中已经自动配置好相关的参数,无需在进行配置,比较方便。

Android之如何使用JUnit进行单元测试的更多相关文章

  1. Android中如何使用JUnit进行单元测试 eclipse

    Android中如何使用JUnit进行单元测试 在我们日常开发android app的时候,需要不断地进行测试,所以使用JUnit测试框架显得格外重要,学会JUnit可以加快应用的开发周期. Andr ...

  2. Android开发学习——SQLite数据库与单元测试

    SQLite数据库 轻量级关系型数据库 创建数据库需要使用的api:SQLiteOpenHelper  public class Myopenhelper extends SQLiteOpenHelp ...

  3. android 学习随笔三(测试与单元测试框架)

    测试 1.按岗位: 黑盒测试:测试业务逻辑 白盒测试:测试逻辑方法 2.按测试粒度 方法测试 function 单元测试 unit 集成测试 integration 系统测试 system 3.按暴力 ...

  4. java如何使用JUnit进行单元测试

    注:所有内容都是在eclipse上实现,关于eclipse的安装和jdk的安装配置,请看:http://www.cnblogs.com/fench/p/5914827.html 单元测试是什么? 百度 ...

  5. JUnit 4 单元测试

    Individual Project ——JUnit 4 单元测试 学习到JUnit单元测试,我拿来测试之前写过的一个计算器(两个依存类:Calc.java CalcFunction.java).代码 ...

  6. 使用Spring配合Junit进行单元测试的总结

    最近公司的项目和自己的项目中都用到了spring集成junit进行单元测试,总结一下几种基本的用法: 1.直接对spring中注入的bean进行测试(以DAO为例): 在测试类上添加@RunWith注 ...

  7. Spring(3)—— Junit框架单元测试

    Junit主要用于单元测试,即白盒测试.它是一个开源的由JAVA开发的一个用于测试的框架. Junit的几个基本概念:TestCase,TestSuite,TestFixtrue TestCase:代 ...

  8. JUnit + Mockito 单元测试(二)

    摘自: http://blog.csdn.net/zhangxin09/article/details/42422643 版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 入门 ...

  9. spring junit 做单元测试,报 Failed to load ApplicationContext 错误

    spring junit 做单元测试,报 Failed to load ApplicationContext 错误. 查找了好一会,最后发现.@ContextConfiguration(locatio ...

随机推荐

  1. 打包发布WinForm应用程序

    1:新建安装部署项目 打开VS,点击新建项目,选择:其他项目类型->安装与部署->安装向导(安装项目也一样),然后点击确定.(详细见下图) 此主题相关图片如下: 2:安装向导 点击下一步, ...

  2. 初学js/jquery 心得

    1.多个对象操作的时候可以放在一起,eg: $('.send_message, .friends_increment').blur(function() {}); 2.三元表达式与if else,eg ...

  3. JQuery_元素属性操作

    除了对元素内容进行设置和获取,通过jQuery 也可以对元素本身的属性进行操作,包括获取属性的属性值.设置属性的属性值,并且可以删除掉属性. <script type="text/ja ...

  4. TestNG BeforeClass BeforeMethod Test AfterClass AfterMethod

    http://topmanopensource.iteye.com/blog/1983729 1.TestNG测试注解和Junit注解的不同以及生命周期: TestNG测试的一个方法的生命周期: @B ...

  5. 配置项setOption -- title

    标题组件,包含主标题和副标题.在 ECharts 3 中可以存在任意多个标题组件,这在需要标题进行排版,或者单个实例中的多个图表都需要标题时会比较有用. title.show boolean [ de ...

  6. ftp put本地文件至ubuntu服务器报错

    起因:我想把本地下载的安装包上传至服务器. 由于Mac的ftp图形化客户端还没找着合适的,就想着用命令也是一样的. 但是又进坑了. 下载能够正常运行: ftp> get 2.jpg /Users ...

  7. mongo 相关命令

    mongo导入数据: 1. 先进入找到mongo 安装目录 执行 ./mongo 进入mongo 2. mongorestore -u 用户名 -p 密码 -d 数据库 —drop 文件存在路径 显示 ...

  8. ActiveReports中如何在后台导出运行时绑定数据源报表

    ActiveReports支持运行时绑定数据源功能,这种绑定数据源方法使用较为普及,然而很多系统中都需要在后台导出报表文件,所以用户就很困惑,ActiveReports中如何在后台导出运行时绑定数据源 ...

  9. 基于jstree的 对混乱的 命名系统进行归类的 计算机软件

    本人现在就职于一家加拿大东部餐饮连锁公司的IT部门,公司旗下有4个品牌,280多家餐厅. 所有的餐厅都使用maitred 的pos软件来处理收银结账. 公司总部使用business object 对m ...

  10. JAVA类与对象

    Employee类: public class EmployeeTest { public static void main(String[] args) { // fill the staff ar ...