1.新建壹個java package,新建壹個java類
源碼來自開源項目:/p/nbandroid-utils/
源碼中生成的TEST-all.xml結果文件位於/data/data/com.example/files目錄下,要導出結果文件的話,需要手機擁有root權限,比較麻煩,因此下面修改了文件存放路徑,有SD卡則文件位於SD卡的/robotium目錄下
代碼貼不下,詳細代碼請見.example" />
修改為:
[html] view plaincopy
<instrumentation
android:name="com.example.test.instrumentation.InstrumentationTestRunner"
android:targetPackage="com.example" />
3.修改Run Configurations
右鍵測試工程>Run as >Run Configurations
在Test欄中,勾選Run all tests in the selected project,or package
這樣每次在Eclipse中運行時才會使用新的InstrumentationTestRunner
在Instrumentation runner處下拉選擇新寫的InstrumentationTestRunner
點擊Apply完成設置
4.命令行下運行測試用例
Running all tests: adb shell am instrument -w com.android.foo/com.example.test.instrumentation.InstrumentationTestRunner
Running a single testcase: adb shell am instrument -w -e class com.android.foo.FooTest com.android.foo/com.example.test.instrumentation.InstrumentationTestRunner
Running multiple tests: adb shell am instrument -w -e class com.android.foo.FooTest,com.android.foo.TooTest com.android.foo/com.example.test.instrumentation.InstrumentationTestRunner
命令行下運行測試用例與平時壹樣,只要將原來的InstrumentationTestRunner換成新的InstrumentationTestRunner就行,
需要註意的是,由於每次命令行執行完畢,都會覆蓋原有的TEST-all.xml文件,即如果采用Running a single testcase方式運行多個測試用例集,則最後結果只會記錄最後壹個用例集,因此建議采用Running multiple tests方式
5.運行完成後將手機中的文件導出
adb -s $ANDROID_AVD_DEVICE pull /mnt/sdcard/rototium/TEST-all.xml
其中$ANDROID_AVD_DEVICE為參數化的手機序列號
6.在jenkins中任務構建完成後即可使用Publish JUnit test result report插件分析得出單元測試報告