天天看點

android studio 單元測試問題記錄

問題1:在Build Variants視窗中沒有Test Artifact來選擇測試器

如圖:

android studio 單元測試問題記錄

解決方案是在Menu中依次打開: File  -> Settings -> Build, Execution, Deployment -> Build Tools -> Gradle -> Experimental

将Enable all test artifacts (Unit Test and Instrumentation Test) in Android projects 取消勾選,點選OK後效果如下圖

android studio 單元測試問題記錄

問題2:Error:Conflict with dependency 'com.android.support:support-annotations'. Resolved versions for app (23.3.0) and test app (23.0.1) differ. See http://g.co/androidstudio/app-test-app-conflict for details.

在配置支援UI測試的時候首先添加

defaultConfig {
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
           

其次添加

dependencies {
    androidTestCompile 'com.android.support.test:runner:0.4'
    // Set this dependency to use JUnit 4 rules
    androidTestCompile 'com.android.support.test:rules:0.4'
    // Set this dependency to build and run Espresso tests
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
    // Set this dependency to build and run UI Automator tests
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
}
           

然後提示問題2,在dependencies中添加

androidTestCompile 'com.android.support:support-annotations:23.3.0'      

總共添加的代碼如下

dependencies {
    androidTestCompile 'com.android.support.test:runner:0.4'
    // Set this dependency to use JUnit 4 rules
    androidTestCompile 'com.android.support.test:rules:0.4'
    // Set this dependency to build and run Espresso tests
    androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.1'
    // Set this dependency to build and run UI Automator tests
    androidTestCompile 'com.android.support.test.uiautomator:uiautomator-v18:2.1.2'
    // Solves "versions for app (23.3.0) and test app (23.1.1) differ"
    androidTestCompile 'com.android.support:support-annotations:23.3.0'
}
           

問題2解決