Android junit單元測試,androidjunit
軟體測試的分類
* 黑箱測試
* 測試邏輯業務
* 白盒測試
* 測試邏輯方法
根據測試粒度
* 方法測試:function test
* 單元測試:unit test
* 整合測試:integration test
* 系統測試:system test
根據測試暴力程度
* 煙霧測試 (Smoke Test):smoke test
* 壓力測試:pressure test
建立android項目,建立Test.java檔案,注意定義一個類繼承一定要繼承AndroidTestCase
package com.wuyudong.juint.test;import com.wuyudong.juint.util.Utils;import android.test.AndroidTestCase;public class Test extends AndroidTestCase { public void test() { int res = Utils.add(3, 5); assertEquals(8, res); }}
建立工具包檔案Utils.java
package com.wuyudong.juint.util;public class Utils { public static int add(int a, int b) { return a - b; }}
運行項目,報錯:
[2016-05-30 06:21:13 - 單元測試] 單元測試 does not specify a android.test.InstrumentationTestRunner instrumentation or does not declare uses-library android.test.runner in its AndroidManifest.xml
在AndroidManifest.xml中添加下面的代碼:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.wuyudong.juint" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.wuyudong.juint" ></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.wuyudong.juint.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>
繼續運行單元測試test,出現下面的斷言異常
雙擊跳轉到
public class Test extends AndroidTestCase { public void test() { int res = Utils.add(3, 5); assertEquals(8, res); }}