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

  在我们日常开发android app的时候,需要不断地进行测试,所以使用JUnit测试框架显得格外重要,学会JUnit可以加快应用的开发周期。
  Android中建立JUnit测试环境有以下两种方法。
  一、直接在需要被测试的工程中新建测试类
  集成步骤:
  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进行单元测试 eclipse的更多相关文章

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

    转的:http://www.blogjava.net/qileilove/archive/2014/05/19/413824.html Android中如何使用JUnit进行单元测试 在我们日常开发a ...

  2. SpringBoot重点详解--使用Junit进行单元测试

    目录 添加依赖与配置 ApplicationContext测试 Environment测试 MockBean测试 Controller测试 情况一 情况二 方法一 方法二 本文将对在Springboo ...

  3. 在Android中进行单元测试遇到的问题

    问题1.Cannot connect to VM  socket closed 在使用JUnit进行测试的时候,遇到这个问题.网上的解释是:使用Eclipse对Java代码进行调试,无论是远程JVM还 ...

  4. 软件工程第二次作业(Android Studio利用Junit进行单元测试)

    一.开发工具的安装和运行 1.安装 由于我的电脑之前就安装好了Android Studio,就不再重装了.在这里就给出几条安装过程中需要注意的地方吧: 安装包最好在官网下载已经带有Android SD ...

  5. Android使用JUnit进行单元测试

    前言:为什么要进行单元测试?单元测试能快速是开发者,找到代码中的问题所在,因为是单元测试,所以代码只执行响应的测试单元,执行快解决问题的效率高,同时提高代码的质量. Android中的单元测试可简单分 ...

  6. Android中使用自身携带的Junit新建一个测试工程

    1.新建立一个Android工程 package com.shellway.junit; public class Service { public int divide(int a,int b){ ...

  7. 如何将Android默认的Camra程序导入到eclipse中

    由于工作需要将camera源码导入到Eclipse中,找了很多的方法,现将自己的整理发出来.... 由于开发的要求,需要将Android默认的Camra程序导入到eclipse中,进行修改和再开发. ...

  8. Android中的单元测试

    2015年5月19日 23:10     在Android中,已经内置了Junit所以不需要在导包.只要继承AndroidTestCase类就可以了.     首先需要修改AndroidManifes ...

  9. Android 开发之开发插件使用:Eclipse 插件 SQLiteManger eclipse中查看数据内容--翻译

    最近研究了一段时间Android开发后发现,google自带的ADT工具,缺失一些开发常用的东西,希望可以构建一个类似使用JAVA EE开发体系一样开发的工具包集合,包括前台开发,调试,到后台数据库的 ...

随机推荐

  1. python有木有哪些必须要学习的高级模块

    python有木有哪些必须要学习的高级模块 字母表

  2. Linux 用户和组的 添加/删除

    1.建用户:adduser phpq //新建phpq用户passwd phpq //给phpq用户设置密码 2.建工作组groupadd test //新建test工作组 3.新建用户同时增加工作组 ...

  3. codeforces 11 B.Jumping Jack 想法题

    B. Jumping Jack Jack is working on his jumping skills recently. Currently he's located at point zero ...

  4. Qt570_CentOS64x64_02

    1.Qt570的简单测试项目,在做"重新构建"的操作的时候,出现1个问题,Qt底下的"编译输出"窗口中的信息为: cc1plus: error: unrecog ...

  5. 豆知识扩展:HTML<meta> tag

    豆知识: HTML<meta> tag Metadata 是关于数据的信息. The <meta> tag provides metadata关于网页.Metadat不会显示在 ...

  6. 6-9 😢 5小时的debug: 从rails 命令运行超慢开始->删除rails->删除ruby->删除rvm->安装上rvm->安装上ruby

    上午,莫名其妙的rails app不能用了,rails -v一查发现不存在.ruby -v发现是2.0的版本.很着急上火,因为很少使用过rvm这个ruby版本控制器.所以照官网文档.从新安装ruby, ...

  7. oracle会自动收集统计信息-记住哦

    oracle自动收集统计信息,周一至周五  时间:22:00:00 oracle自动收集统计信息,周六.周日  时间:06:00:00

  8. Leetcode 62

    //从理解二维dp到简化成一维dp我用了一年的时间class Solution { public: int uniquePaths(int m, int n) { vector<); ;i &l ...

  9. 310. Minimum Height Trees -- 找出无向图中以哪些节点为根,树的深度最小

    For a undirected graph with tree characteristics, we can choose any node as the root. The result gra ...

  10. Struts2基本使用(一)--在项目中引入Struts2

    Struts2基本使用 在MVC开发模式中,Struts2充当控制器(Controller)的角色.其主要功能就是处理用户请求,生成响应,是连接视图层(View)和模型层(Model)的桥梁.在处理用 ...