Android系統單元測試方法(16)
發表于:2011-06-30來源:未知作者:領測軟件測試網采編點擊數:
標簽:
如果用Eclipse的ADT插件(0.8版本以上),也可以用圖形界面來添加,如下圖: 編輯好manifest,就可以打包(build,可以用Eclipse ADT來做,也可以用aapt命令手工
如果用Eclipse的ADT插件(0.8版本以上),也可以用圖形界面來添加,如下圖:
編輯好manifest,就可以打包(build,可以用Eclipse ADT來做,也可以用aapt命令手工完成),然后安裝到虛擬機上(用adb install命令)。之后就可以利用命令行的方式來加載你的單元測試了。在Android Shell中加載一個Instrumentation的方法是利用以下命令:
-
adb shell am instrument –w XXXXXX
adb shell am instrument –w XXXXXX
其中-w是指定Instrumentation類的參數標志。一個簡單的例子是:
-
adb shell am instrument -w com.android.foo/android.test.InstrumentationTestRunner
adb shell am instrument -w com.android.foo/android.test.InstrumentationTestRunner
當然,也可以利用adb shell先進入android命令行模式,再直接寫am instrument –w XXXXXXX。下面將具體介紹如何將根據需要加載一組單元測試。 如何在Android中利用Instrumentation來進行測試?
在介紹具體的命令之前,我們先理解一下單元測試的層次。一組單元測試可以被組織成若干個TestSuite。每個TestSuite包含若干TestCase(某個繼承android.jar的junit.framework.TestCase的類)。每個TestCase又包含若干個 Test(具體的test方法)。
如果假設com.android.foo是你的測試代碼的包的根。當執行以下命令時,會執行所有的TestCase的所有Test。測試的對象就是在Target Package中指定的包中的代碼:
-
adb shell am instrument -w com.android.foo/android.test.InstrumentationTestRunner
adb shell am instrument -w com.android.foo/android.test.InstrumentationTestRunner
如果你想運行一個TestSuite,首先繼承android.jar的junit.framework.TestSuite類,實現一個TestSuite(比如叫com.android.foo.MyTestSuite),然后執行以下命令執行此TestSuite
-
adb shell am instrument -e class com.android.foo.MyTestSuite -w com.android.foo/android.test.InstrumentationTestRunner
adb shell am instrument -e class com.android.foo.MyTestSuite -w com.android.foo/android.test.InstrumentationTestRunner
其中的-e表示額外的參數,語法為-e [arg1] [value1] [arg2] [value2] …這里用到了class參數。
如果僅僅想運行一個TestCase(比如叫com.android.foo.MyTestCase),則用以下命令:
原文轉自:http://www.kjueaiud.com