What are the correct dependencies for Espresso tests?

独自空忆成欢 提交于 2020-01-06 07:01:41

问题


On different official Android sites there is different information on how to set up Espresso tests:

1) https://developer.android.com/training/testing/ui-testing/espresso-testing

dependencies {
    // Other dependencies ...
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

2) https://developer.android.com/studio/test/

dependencies {
    // Required for local unit tests (JUnit 4 framework)
    testCompile 'junit:junit:4.12'

    // Required for instrumented tests
    androidTestCompile 'com.android.support:support-annotations:24.0.0'
    androidTestCompile 'com.android.support.test:runner:0.5'
}

3) https://developer.android.com/training/testing/espresso/setup

androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
  • With some configurations I get library conflicts ((2) explains that this is due to dependencies requiring the same other dependency but with different versions).

    • Even excluding with

    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2', { exclude group: 'com.android.support', module: 'support-annotations' } might not help.

  • With some configurations I get an error on the import of android.support.test.rule.ActivityTestRule


回答1:


No. 3 seems to work, with and without the exclude-part:

dependencies {
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    }
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test:rules:1.0.2'
}

It seems that for older espresso versions, like 2.2.2, one did not need a separate dependency for android.support.test.rule.ActivityTestRule import.




回答2:


Espresso is an instrumentation test. These are placed in a different folder than Unit Tests. They are kept in the androidTest/java package. So keep that in mind when using dependencies. For instrumentation tests you need to use

androidTestImplementation

vs

testImplementation

So assuming that your Espresso test class is in app->src->androidTest->java location, this should work:

androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-intents:3.0.2'
androidTestImplementation 'com.android.support.test:rules:1.0.2'
androidTestImplementation 'com.android.support.test:runner:1.0.2'


来源:https://stackoverflow.com/questions/50179597/what-are-the-correct-dependencies-for-espresso-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!