1)是一個測試運行器,用於運行Junit3和Junit4的Android測試包
2)替換Instrumentation Test Runner(一個比較舊的測試運行器)
3)支持Instrumentation Test Runner全部特性,但又進行了擴展
4)保持了全部Instrumentation Test Runner的命令格式java
java.lang.Object
android.app.Instrumentation
android.support.test.runner.MonitoringInstrumentation
android.support.test.runner.AndroidJUnitRunner
am instrument [-r] [-e <NAME> <VALUE>] [-p <FILE>] [-w] [--user <USER_ID>|CURRENT] [--no-window-animation] [--abi <ABI>] <COMPONENT> //COMPONENT:包名/運行Runner
adb shell am instrument
-w com.testDemo.test/android.support.test.runner.AndroidJUnitRunner
參數android |
說明git |
-rgithub |
打印原始結果(若是不加測試結果信息會不多) 使用[-e perf true]生成原始輸出性能度量 |
-e web |
設置參數 爲測試運行器常見的形式是: [-e [,…]] |
將profiling數據寫入到文件中 注:須要先創建一個trace文件 |
|
-w shell |
等待測試完成(這是一個必需參數) |
–user | |
指定用戶運行,若是不指定則使用當前用戶運行 |
–no-window-animation | 關掉窗口動畫(至關於開場動畫)再運行 |
–abi 架構 |
經過所選的ABI啓動進程,選擇支持的CPU架構 可使用(adb shell getprop |
1)在github中搜索android-testing選中googlesamples/android-testing點進去,而後下載.zip壓縮包
2)下載完成後進行解壓,全部的依賴都在android-testing-master\ui\espresso\BasicSampleBundled\libs路徑下
3)file–new–new project(等待工程建立完成)–app–java–[packageName右鍵]–new–java class
注:點開java後會有兩個選項,上面的一個是apk代碼,下面是測試代碼,咱們寫測試用例通常都寫在測試代碼包內
4)複製android-testing-master\ui\espresso\BasicSampleBundled\libs目錄下除android-support-v4.jar之外的所有文件到建立的工程的libs文件夾中
5)選中copy進去的全部文件右鍵—add as library—在彈出的提示窗口中選擇app—OK
6)增長完成後能夠切換到Android視圖,在Gradle Scripts/build.gradle(Module:app)中進行查看,而且註釋掉第一行*(註釋的那一行表示它會把libs下面全部的jar包編譯進去,由於咱們已經手動把它添加進去了因此就不須要了),代碼顯示以下:app
dependencies { //compile fileTree(include: ['*.jar'], dir: 'libs') testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:design:23.1.1' compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/espresso-contrib-2.2-release-no-dep.jar') compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/espresso-core-2.2-release-no-dep.jar') compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/espresso-idling-resource-2.2-release-no-dep.jar') compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/espresso-intents-2.2-release-no-dep.jar') compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/espresso-web-2.2-release-no-dep.jar') compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/exposed-instrumentation-api-publish-0.3-release-no-dep.jar') compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/guava-18.0.jar') compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/hamcrest-core-1.3.jar') compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/hamcrest-integration-1.3.jar') compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/hamcrest-library-1.3.jar') compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/javawriter-2.1.1.jar') compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/javax.annotation-api-1.2.jar') compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/javax.inject-1.jar') compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/jsr305-2.0.1.jar') compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/junit-4.12.jar') compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/rules-0.3-release-no-dep.jar') compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/runner-0.3-release-no-dep.jar') compile files('C:/Users/xuzhonghui/AndroidStudioProjects/RunnerDemo/libs/uiautomator-v18-2.1.1-release-no-dep.jar') }
5)點擊導航欄Run—Edit Configurations…—點擊彈出界面左上角的+號—Android Tests,配置參考下圖性能
6)在Gradle Scripts/build.gradle(Module:app)文件的defalutConfig中增長調試器,增長後的代碼顯示以下:
android { compileSdkVersion 23 buildToolsVersion "23.0.2" defaultConfig { applicationId "com.example.xuzhonghui.runnerdemo" minSdkVersion 15 targetSdkVersion 23 versionCode 1 versionName "1.0" //引號中的內容爲剛剛建立的運行器 testInstrumentationRunner"android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } }
7)至此,基本的配置完成了,而後在剛剛建立的類中寫測試代碼了,代碼以下:
package com.example.xuzhonghui.runnerdemo; import android.app.Instrumentation; import android.support.test.InstrumentationRegistry; import android.support.test.runner.AndroidJUnit4; import android.support.test.uiautomator.UiDevice; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; //下面一行必定要的,就是指定哪一個運行器運行 @RunWith(AndroidJUnit4.class) //測試用例 public class RunnerTest { //初始化設備 UiDevice device; @Before public void setip{ device=UiDevice.getInstance(InstrumentationRegistry.getInstrumentation()) } //用例1 @Test public void test1(){ device.pressMenu(); } //用例2 @Test public void test2(){ device.pressBack(); } //用例3 @Test public void test3(){ device.pressMenu(); } //用例4 @Test public void test4(){ device.pressBack(); } }
8)代碼寫好後須要從新配置一下運行器,配置圖以下:
9)而後運行便可
我第一次按照上述步驟運行的時候就出現了問題,具體原理暫時還沒搞清楚,之後明白了補上,問題和解決方案請參考這裏:http://blog.csdn.net/qq_26967883/article/details/50477071的第一條:Excution faild for task ‘:app:transformResourcesWithMergeJavaResForDebug’
#列出手機中全部已安裝的包,界面中能夠看到剛剛使用Android Studio安裝的包 adb shell pm list instrumentation #該命令中不打印詳細日誌,-e後面的參數是剛剛Android Studio中建立用例的<packageName>.<className> adb shell am instrument -w -e class com.example.xuzhonghui.runnerdemo.RunnerTest com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner #跟上一條命令相比,只是多了一個<-r>參數,是讓他輸出詳細的運行日誌,至關於eclipes調試uiautomator的時候輸出的日誌 adb shell am instrument -w -r -e class com.example.xuzhonghui.runnerdemo.RunnerTest com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner #跟上一條相比多了一個-p+file的參數,目的是爲了將運行日誌保存到.trace文件中。使用該命令要先在adb shell 模式下在設備中使用命令"touch runner.trace"建立一個自定義名稱的.trace文件 adb shell am instrument -w -r -p /data/local/tmp/runner.trace -e class com.example.xuzhonghui.runnerdemo.RunnerTest com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner
過濾參數 |
說明 |
-e testFile | 運行文件中指定的用例 |
-e package | 運行這個包中的全部用例 |
-e size [small|medium|large] | 運行註釋Small/MediumTest/largeTest的用例 |
-e annotation | 運行指定註釋的用例 |
-e notAnnotation | 運行不包含指定註釋的用例 |
-e numShards | 將用例分割成不一樣的切片 |
-e shardIndex | 運行指定切片id的用例 |
1)首先修改一下上面使用Android Studio建立的用例代碼,多加幾個測試方法,我這裏加了10個,都是按鍵信息,加完後記得先跑一次,把包push到手機中
#列出手機中全部已安裝的包找到com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner就是咱們使用Android建立並安裝到手機中的包 adb shell pm list instrumentation #運行所有用例,沒有日誌信息 C:\Users\xuzhonghui>adb shell am instrument -w com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner #運行所有用例,有日誌信息 C:\Users\xuzhonghui>adb shell am instrument -w -r com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner #運行指定類的用例(指定類=包名.類名) C:\Users\xuzhonghui>adb shell am instrument -w -r -r class com.example.xuzhonghui.runnerdemo.RunnerTest com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner #運行指定類中的某一條用例(在指定類後面加一個"#用例名") C:\Users\xuzhonghui>adb shell am instrument -w -r -r class com.example.xuzhonghui.runnerdemo.RunnerTest#test3 com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner #運行多個類的用例,運行前先在以前先在Android Studio中多建立一個類並複製幾條用例進去方便(這裏只演示兩個類,多個類同理,加個逗號便可) C:\Users\xuzhonghui>adb shell am instrument -w -r -r class com.example.xuzhonghui.runnerdemo.RunnerTest,com.example.xuzhonghui.runnerdemo.RunnerTest2 com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner
建立一個.txt文檔,文檔內容以下圖,而後使用命令」adb push F:\testFile.txt /data/local/tmp」將文檔push到手機中
#運行剛剛.txt文件中指定的用例 adb shell am instrument -w -r -e testFile /data/local/tmp/testFile.txt com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner #運行com.example.xuzhonghui.runnerdemo包裏的全部用例 adb shell am instrument -w -r -e package com.example.xuzhonghui.runnerdemo com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner
修改一下以前在Android Studio中建立的用例,增長几個註釋,結果以下圖(記得修改完後先在手機上運行一下):
#運行剛剛註釋爲small的用例 adb shell am instrument -w -r -e size small com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner #運行剛剛註釋爲medium的用例 adb shell am instrument -w -r -e size medium com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner #運行剛剛註釋爲large的用例 adb shell am instrument -w -r -e size large com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner
修改AndroidStudio中的用例,上一步中的smallText註釋的Import找到註釋方法的編寫,而後根據這個本身來編寫註釋方法,編寫好的方法以下圖:
編寫好後,按照上一步的方式將註釋加入到用例中,修改好用例後記得先運行一次
#運行指定自定義註釋SmokeAnnotation的用例(這裏須要注意的是annotation後面的跟的是本身編寫的註釋的<包名>.<類名>) adb shell am instrument -w -r -e annotation com.example.xuzhonghui.runnerdemo.SmokeAnnotation com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner #運行除指定自定義註釋SmokeAnnotation的全部其餘用例 adb shell am instrument -w -r -e notAnnotation com.example.xuzhonghui.runnerdemo.SmokeAnnotation com.example.xuzhonghui.runnerdemo.test/android.support.test.runner.AndroidJUnitRunner
主要是用在白盒測試上的,而不是UiAutomator,有興趣能夠深刻研究下
參數 | 說明 |
-e log true | 只在日誌模式下運行 這個選項將加載和遍歷全部測試的類和方法,但會繞過實際測試執行。用於快速獲取信息要執行的測試的儀器命令 |
-e coverage true | 生成代碼覆蓋率 這須要一個EMMA的構建。默認狀況下,代碼覆蓋率結果文件將被保存在/data//coverage.ec文件,除非有coverageFile標誌 |
-e coverageFile | 指定代碼覆蓋率文件保存路徑 -e coverageFile /sdcard/myFile.ec |
-e listener | 指定一個或多個RunListeners觀察測試運行 -e listener com.foo.Listener,com.foo.Listener2 |
-e timeout_mse | 設置超時時間(單位:毫秒)將被應用到每個測試 -e timeout_mec 5000 |
-e disableAnlytics true | 禁用谷歌分析 |
如何使用監聽器:
實際使用過程當中,只需在AndroidManifect清單文件中加入上面代碼便可,實際使用參考下圖