Android系統單元測試方法(17)
發表于:2011-06-30來源:未知作者:領測軟件測試網采編點擊數:
標簽:
Java代碼 adbshellaminstrument-e class com.android.foo.MyTestCase-wcom.android.foo/android.test.InstrumentationTestRunner adb shell am instrument -e class com.android.foo.MyTestCase -w com.android.fo
-
adb shell am instrument -e class com.android.foo.MyTestCase -w com.android.foo/android.test.InstrumentationTestRunner
adb shell am instrument -e class com.android.foo.MyTestCase -w com.android.foo/android.test.InstrumentationTestRunner
如果僅僅想運行一個Test(比如就是上面MyTestCase的testFoo方法),很類似的,就這樣寫:
-
adb shell am instrument -e class com.android.foo.MyTestCase#testFoo -w com.android.foo/android.test.InstrumentationTestRunner
adb shell am instrument -e class com.android.foo.MyTestCase#testFoo -w com.android.foo/android.test.InstrumentationTestRunner
然后,所有的測試結果會輸出到控制臺,并會做一系列統計,如標記為E的是Error,標記為F的是Failure,Su
clearcase/" target="_blank" >ccess的測試則會標記為一個點。這和JUnit的語義一致。如果希望斷點調試你的測試,只需要直接在代碼上加上斷點,然后將運行命令參數的-e后邊附加上debug true后運行即可。更加詳細的內容可以看InstrumentationTestRunner的Javadoc。我希望Android能盡快有正式的文檔來介紹這個內容。 如何在Android的單元測試中做標記?
在android.test.annotation包里定義了幾個annotation,包括 @LargeTest,@MediumTest,@SmallTest,@Smoke,和@Suppress。你可以根據自己的需要用這些 annotation來對自己的測試分類。在執行單元測試命令時,可以在-e參數后設置“size large”/ “size medium”/ “size small”來執行具有相應標記的測試。特別的@Supperss可以取消被標記的Test的執行。 完整的操作過程
總結以上所有的內容,編寫并運行完整的測試需要以下的步驟:
loadrunner/" target="_blank" >lr87_RS_ybLUaROG971Ml4s-K4ueu7-K24ECCe7KtCddfdN19EX1huVHzsNGkF598ULmMEZsbewOGHeDC8K8zLnFeTfpfQxqB2kUPA/clip_image008_thumb%5B1%5D.gif" />
以上步驟中,在Android自帶的例子中,我發現它有兩個manifest.xml。也就是說在步驟3中源代碼和測試代碼分別生成了兩個不同的包。然后步驟4利用adb install命令安裝到了虛擬機上。由于我沒有找到Eclipse ADT有辦法可以為一個只有Instrumentation,沒有Activity的Application打包并安裝,于是采用了略微不同的辦法完成了這個工作。下面將一一詳細介紹整個過程。 1. 編寫程序
我新建了一個項目TestApp,參數為:
-
Package Name: com.android.testapp
-
-
Activity Name: MainActivity
-
-
Application Name: TestApp
-
-
以下是MainActivity的源代碼:
-
-
package com.android.testapp;
-
-
import android.app.Activity;
-
-
import android.os.Bundle;
-
-
public class
原文轉自:http://www.kjueaiud.com