Android下junit單元測試、logCat的使用
Android下junit單元測試
軟件測試小知識:
根據(jù)測試是否知道源代碼:
黑盒測試:只關(guān)心程序執(zhí)行的過程和結(jié)果
白盒測試:根據(jù)源代碼寫測試方法或者測試用例。
根據(jù)測試的粒度:
方法測試:function test
單元測試:unit test
集成測試:intergration test
根據(jù)測試的次數(shù):
冒煙測試:smoke test(android 猴子)
壓力測試:prssure test
Android單元測試:
1.Android測試類要繼承AndroidTestCase類
2.寫測試方法,在測試方法內(nèi)使用斷言assert來測試要測試的方法
3.在AndroidManifest.xml中,要設(shè)置
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.lee.test" />
和<uses-library android:name="android.test.runner" >
4.確保adb連接正常。
MyService.java
package com.lee.test.service; public class MyService { /** |
TestMyService.java
package com.lee.test.service.test; import com.lee.test.service.MyService; import android.test.AndroidTestCase; public class TestMyService extends AndroidTestCase { /** * add方法的測試代碼 * 把異常拋給測試框架 * @throws Exception */ public void testAdd()throws Exception{ MyService myService = new MyService(); int retVal = myService.add(3, 5); //斷言,預(yù)期結(jié)果是8,實際結(jié)果是retVal assertEquals(8, retVal); } } package com.lee.test.service.test; import com.lee.test.service.MyService; import android.test.AndroidTestCase; public class TestMyService extends AndroidTestCase { /** * add方法的測試代碼 * 把異常拋給測試框架 * @throws Exception */ public void testAdd()throws Exception{ MyService myService = new MyService(); int retVal = myService.add(3, 5); //斷言,預(yù)期結(jié)果是8,實際結(jié)果是retVal assertEquals(8, retVal); } } |
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.lee.test" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <!-- 這里是進(jìn)行單元測試必須要添加,指令集必須在manifest節(jié)點下 --> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.lee.test" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.lee.test.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> <!-- 這里也是進(jìn)行單元測試必須要添加,在application節(jié)點下,使用函數(shù)庫 --> <uses-library android:name="android.test.runner" > </uses-library> </application> </manifest> <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.lee.test" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" /> <!-- 這里是進(jìn)行單元測試必須要添加,指令集必須在manifest節(jié)點下 --> <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.lee.test" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.lee.test.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> <!-- 這里也是進(jìn)行單元測試必須要添加,在application節(jié)點下,使用函數(shù)庫 --> <uses-library android:name="android.test.runner" > </uses-library> </application> </manifest> |
logCat的使用
日志信息是分等級的:
verbose:提醒 黑色
debug:調(diào)試 藍(lán)色
info:信息 綠色
warn:警告 橙色
error:錯誤 紅色
使用上面的程序,可以做一個demo
修改MyService.java
package com.lee.test.service; import android.util.Log; public class MyService { private static final String TAG = "MyService"; /** * 計算器相加的業(yè)務(wù) * @param a * @param b * @return */ public int add(int a,int b){ Log.v(TAG, ""+a); Log.d(TAG, ""+b); Log.i(TAG, ""+b); Log.w(TAG, ""+a); Log.e(TAG, ""+b); System.out.println("System.out的log"); System.err.println("System.err的log"); return a+b; } } package com.lee.test.service; import android.util.Log; public class MyService { private static final String TAG = "MyService"; /** * 計算器相加的業(yè)務(wù) * @param a * @param b * @return */ public int add(int a,int b){ Log.v(TAG, ""+a); Log.d(TAG, ""+b); Log.i(TAG, ""+b); Log.w(TAG, ""+a); Log.e(TAG, ""+b); System.out.println("System.out的log"); System.err.println("System.err的log"); return a+b; } } |
posted on 2013-10-14 10:16 順其自然EVO 閱讀(793) 評論(0) 編輯 收藏 所屬分類: android