Android中如何使用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中已經自動配置好相關的參數,無需在進行配置,比較方便。