qileilove

          blog已經轉移至github,大家請訪問 http://qaseven.github.io/

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

          posted on 2014-05-19 10:10 順其自然EVO 閱讀(6938) 評論(1)  編輯  收藏 所屬分類: android

          評論

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

          學生學習是城市調查  回復  更多評論   

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

          導航

          統計

          常用鏈接

          留言簿(55)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 景宁| 都江堰市| 宝鸡市| 什邡市| 平舆县| 弥渡县| 郯城县| 乌海市| 芦山县| 奎屯市| 乌拉特前旗| 泸水县| 军事| 抚宁县| 竹山县| 团风县| 德阳市| 那坡县| 惠州市| 平江县| 慈利县| 汤原县| 西乌珠穆沁旗| 扬中市| 宁波市| 娄烦县| 余庆县| 招远市| 平凉市| 建平县| 马龙县| 凉城县| 洛扎县| 浠水县| 中超| 浏阳市| 澄城县| 平和县| 淮阳县| 吐鲁番市| 巴彦淖尔市|