Android Test

Unit Test

  • 使用場景
    只是java,或者簡單的依賴Android
  • 優(yōu)點
    不用每次都啟動真機或模擬器。
  • 使用說明
    1. 項目結(jié)構(gòu)(app/src/main/java)
      測試路徑(app/src/test/java)
    2. 依賴
dependencies {    
        // Unit testing dependencies    
        testCompile 'junit:junit:4.12'    
        // Set this dependency if you want to use Mockito
        testCompile 'org.mockito:mockito-core:1.10.19'    
        // Set this dependency if you want to use Hamcrest matching  
        testCompile 'org.hamcrest:hamcrest-library:1.1'
}
  1. 官方參考文檔

Instrumented Unit Tests

  • 使用場景
    需要使用Android特性,如ContextSharedPreferences
    每次都要運行在真機或模擬器上。
  • 使用說明
    1. 添加測試的庫
defaultConfig {
        testInstrumentationRunner 'android.support.test.runner.AndroidJUnitRunner'
}
  1. 項目結(jié)構(gòu)(app/src/main/java)
    測試路徑(app/src/androidTest/java)
  2. 添加依賴
dependencies {    
        androidTestCompile 'com.android.support:support-annotations:23.0.1'    
        androidTestCompile 'com.android.support.test:runner:0.4.1'   
        androidTestCompile 'com.android.support.test:rules:0.4.1'    
        // Set this dependency if you want to use Hamcrest matching  
        androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
}
  1. 官方參考文檔

參考

  1. Android Test官方文檔,Github
  2. 測試模版GitHub
  3. Hamcrest,Mockito
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內(nèi)容