qileilove

          blog已經(jīng)轉(zhuǎn)移至github,大家請?jiān)L問 http://qaseven.github.io/

          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

          評論

          # re: Android中如何使用JUnit進(jìn)行單元測試 2015-08-12 14:49 去撒

          學(xué)生學(xué)習(xí)是城市調(diào)查  回復(fù)  更多評論   

          <2014年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 宜阳县| 乌拉特后旗| 诸城市| 米泉市| 龙山县| 高青县| 石楼县| 道孚县| 兰考县| 突泉县| 龙州县| 深泽县| 门源| 天祝| 扎赉特旗| 天全县| 依兰县| 五莲县| 开江县| 凤庆县| 芒康县| 江孜县| 中山市| 紫云| 太保市| 保德县| 若尔盖县| 溧阳市| 休宁县| 诏安县| 奉贤区| 吴桥县| 乌苏市| 丹寨县| 米林县| 长岭县| 贞丰县| 玛纳斯县| 万山特区| 泸定县| 景洪市|