问题
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