Android中如何使用JUnit進(jìn)行單元測試
在我們?nèi)粘i_發(fā)android app的時(shí)候,需要不斷地進(jìn)行測試,所以使用JUnit測試框架顯得格外重要,學(xué)會JUnit可以加快應(yīng)用的開發(fā)周期。
Android中建立JUnit測試環(huán)境有以下兩種方法。
一、直接在需要被測試的工程中新建測試類
集成步驟:
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"/>
以上代碼配置是添加測試指令和引入測試環(huán)境,完整的清單文件如下:
<?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.新建一個(gè)測試測試類并繼承AndroidTestCase類,編寫測試方法,在測試方法內(nèi)使用斷言assert來測試要測試的方法。
3.點(diǎn)擊右面的大綱視圖,選擇要測試的方法,右鍵,run as --->Android JUnit test
下面通過一個(gè)簡單的示例來演示一下如何使用JUnit單元測試
1、先創(chuàng)建簡單的待測試類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、創(chuàng)建一個(gè)測試類,此類需要繼承自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(); } } |
一個(gè)好的習(xí)慣是每個(gè)測試方法都拋出異常:throws Exception,然后通過Assert對結(jié)果進(jìn)行斷言。
3、通過大綱視圖運(yùn)行測試方法
綠條表示測試通過,在代碼中我們測試的時(shí)3+5是否等于8,所以結(jié)果肯定是通過的,如果我們把a(bǔ)ssertEquals()中的8改為5的話,會出現(xiàn)以下結(jié)果:
紅條表示測試沒通過,點(diǎn)擊右邊的錯(cuò)誤信息可以定位到出錯(cuò)的代碼行。 二、創(chuàng)建一個(gè)專門用于測試的工程
推薦創(chuàng)建專門的測試工程,因?yàn)檫@樣可以降低代碼的耦合度。
集成步驟:
1.新建工程,選擇new ---- > other ---->android Test Project
2.選擇要測試的工程
3.接著和第一種建立測試類的方法是一樣的,這里比較簡單就略過了。
使用這種方法的話,androidManifest.xml中已經(jīng)自動(dòng)配置好相關(guān)的參數(shù),無需在進(jìn)行配置,比較方便。
posted on 2014-05-19 10:10 順其自然EVO 閱讀(6938) 評論(1) 編輯 收藏 所屬分類: android