Android--JUnit單元測試


前言

  本篇博客說明一下在Android開發中,如何使用JUnit進行單元測試。首先來了解一下什么是JUnit,JUnit測試是白盒測試,即主要是程序員自己對開發的方法進行功能性測試。JUnit是一套框架,Android中也沿用了這一套框架。

 

JUnit

  在Android中使用JUnit測試大致分如下幾個步驟:

  1. 在AndroidManifest.xml中增加對JUnit的支持,並制定測試項目包。
  2. 在AndroidManifest.xml中<application.../>節點中增加一個<uses-library...>節點,name屬性為android.test.runner。
  3. 在編寫待測試方法后,新建一個類,繼承AndroidTestCase,在其中編寫測試用例代碼。
  4. 鼠標左鍵在測試用例方法上,Run As→Android JUnit Test。

  下面就上面幾個步驟,詳細講解一下,新建一個Android項目,在AndroidManifest.xml中,添加一個Instrumentation:

  指定Instrumentation的name與TargetPackage:

  在<application.../>節點中增加<uses-library android:name="android.test.runner" />

  完成后AndroidManifest.xml代碼如下:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="com.example.junittestdemo"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-sdk
 8         android:minSdkVersion="8"
 9         android:targetSdkVersion="17" />
10 
11     <instrumentation
12         android:name="android.test.InstrumentationTestRunner"
13         android:targetPackage="com.example.junittestdemo" >
14     </instrumentation>
15 
16     <application
17         android:allowBackup="true"
18         android:icon="@drawable/ic_launcher"
19         android:label="@string/app_name"
20         android:theme="@style/AppTheme" >
21         <uses-library android:name="android.test.runner" />
22 
23         <activity
24             android:name="com.example.junittestdemo.MainActivity"
25             android:label="@string/app_name" >
26             <intent-filter>
27                 <action android:name="android.intent.action.MAIN" />
28 
29                 <category android:name="android.intent.category.LAUNCHER" />
30             </intent-filter>
31         </activity>
32     </application>
33 
34 </manifest>

  編寫一個簡單的進度百分比計算方法:

 1 package com.example.service;
 2 
 3 public class ProgressService {
 4     public ProgressService() {
 5 
 6     }
 7     public Integer getCurrentProgerss(double current, double max) {
 8         Integer i=(int)((current / max) * 100) ;
 9         return i;
10     }
11 }

 編寫一個測試類,這個類需要繼承AndroidTestCase,針對百分比方法進行測試:

 1 package com.example.junit;
 2 
 3 import android.test.AndroidTestCase;
 4 import android.util.Log;
 5 
 6 
 7 import com.example.service.ProgressService;
 8 
 9 public class ProgressServiceJUnit extends AndroidTestCase {
10     private final String TAG="main";
11     
12     public ProgressServiceJUnit() {
13         // TODO Auto-generated constructor stub
14     }
15     
16     public void getCurrentProgerssTest(){
17         ProgressService progressService=new ProgressService();
18         Integer pro=progressService.getCurrentProgerss(20, 70);
19         Log.i(TAG, pro.toString());
20     }
21 }

  左鍵getCurrentProgerssTest()方法,選中Android JUnit Test,如果需要調試,可以選擇Debug As下的Android JUnit Test:

  當執行成功后,會顯示綠色,如果是其他顏色,則為出錯:

  可以在LogCat日志中看到測試結果:

  源碼下載

  請支持原創,尊重原創,轉載請注明出處。謝謝。

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM