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

  在我们日常开发 androidapp的时候,需要不断地进行 测试,所以使用 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 Android中如何使用JUnit进行单元测试_第1张图片   示例代码如下:
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、通过大纲视图运行测试方法 Android中如何使用JUnit进行单元测试_第2张图片   绿条表示测试通过,在代码中我们测试的时3+5是否等于8,所以结果肯定是通过的,如果我们把assertEquals()中的8改为5的话,会出现以下结果: Android中如何使用JUnit进行单元测试_第3张图片   红条表示测试没通过,点击右边的错误信息可以定位到出错的代码行。    二、创建一个专门用于测试的工程   推荐创建专门的测试工程,因为这样可以降低代码的耦合度。   集成步骤:   1.新建工程,选择new ---- > other ---->android Test Project Android中如何使用JUnit进行单元测试_第4张图片   2.选择要测试的工程 Android中如何使用JUnit进行单元测试_第5张图片   3.接着和第一种建立测试类的方法是一样的,这里比较简单就略过了。   使用这种方法的话,androidManifest.xml中已经自动配置好相关的参数,无需在进行配置,比较方便。

更多相关文章

  1. Android的反编译和代码混淆
  2. Android使用自定义字体的方法
  3. Android 环信官方Demo3.3.2详细配置方法
  4. 在Android上实现HttpServer的示例代码
  5. Android WebView 调用React Js 代码
  6. android recover 系统代码分析 -- 选择进入
  7. Android中经常用到的方法--SDcard下文件的操作
  8. Android Intent调用方法总结

随机推荐

  1. 什么是Activity
  2. Android(安卓)N Ethernet新IP获取机制—I
  3. (4.1.14)Android使用Activity用作弹出式对
  4. DPBaseAdapter-Android
  5. Android(安卓)JNI 开发简单介绍
  6. Android(安卓)在OnCreate()中获取控件高
  7. Okhttp的简单介绍和使用(一)
  8. 高通平台短信里面含有中文和ftp开头的网
  9. 手机上的HTML5框架 Sencha Touch
  10. SurfaceFlinger启动过程分析(一)