我有一个静态方法的简单单元测试并在模拟器上运行它们。 (目标是在云 CI 上运行,所以我在模拟器上测试。)
Gradle 2.2.1 模拟器 Android 5.0
我在控制台上使用这些步骤。
构建.gradle
dependencies {
...
androidTestCompile('com.jakewharton.espresso:espresso:1.1-r3') {
exclude group: 'com.squareup.dagger'
exclude group: 'com.squareup.dagger:dagger:1.2.1'
}
}
android {
compileSdkVersion 21
buildToolsVersion "21.1"
defaultConfig {
minSdkVersion 16
targetSdkVersion 21
testInstrumentationRunner "com.google.android.apps.common.testing.testrunner.GoogleInstrumentationTestRunner"
}
sourceSets {
androidTest.setRoot('src/androidTest')
}
}
错误日志是
Tests on test(AVD) - 5.0 failed: Instrumentation run failed due to 'java.lang.IncompatibleClassChangeError'
com.android.builder.testing.ConnectedDevice > hasTests[test(AVD) - 5.0] FAILED
No tests found.
什么会产生这个错误?
最佳答案
简要回复:
This似乎是关于 java.lang.IncompatibleClassChangeError 的一个很好的链接。如果你在 CI 上尝试:
模拟器在等待设备后没有完全启动,它还没有为您的测试做好准备,并且您的应用程序由于超时而没有安装,所以没有执行任何测试并且构建失败,因为添加了一个新的行为来提醒您
您可以将 adb wait-for-device 替换为等待 stopped 状态(完全启动)检查 adb -e shell getprop init.svc 的循环。 bootanim 作为 this script在公共(public)领域确实如此。 Further info here .
可能的 CI 问题,Double Espresso 已被弃用并发布了 Espresso 2.0:
关于 CI 且未找到测试:
我回答了另一个问题but specific for Travis-ci here .类似的错误,但由于使用的脚本被窃听。如果您运行 gradle installDebug --debug,您将了解有关该错误的更多信息(在此处共享日志)。
但是在 CI 服务器上运行相同的步骤,如果我是对的,您将看到一个由 ShellCommandUnresponsiveException 引起的 InstallException,因为 INSTALL_TIMEOUT 两分钟。您可以使用环境变量 ADB_INSTALL_TIMEOUT=6 #minutes 增加此值,但这不是您现在的问题。
如果您首先在本地运行它,请尝试不使用 -no-window(这样您就可以看到它)或添加 -no-boot-anim(加快速度)但与等待模拟器脚本不兼容)或使用 adb wait-for-device && sleep 300(确保模拟器已完全启动)。
关于 Espresso :
Double Espresso is deprecated因为Espresso 2.0 is now available . Double Espresso 是 Espresso 1.1 的纯 Gradle 端口,两周前发布 2.0 版时 Jake Wharton 弃用了它。
他们 updated the wiki和 JavaDoc (they will move it to android.com) .
You can now use the Android Support Repository to download the latest version.
Samples prerequisites: Android SDK v21, Android Build Tools v21.1.2, Android Support Repository.
These samples use the Gradle build system. To build a project, enter the project directory and use the ./gradlew assemble command or use "Import Project" in Android Studio. Use ./gradlew connectedCheck to run the tests on a connected emulator or device.
关于 Gradle 任务:
来自 Android tasks和 Running tests (Gradle 插件用户指南):
assemble 组装项目输出的任务connectedCheck 运行需要连接设备或模拟器的检查。需要连接设备的检查是通过名为 connectedCheck 的 anchor 任务启动的。这取决于任务 androidTest,因此将运行它。此任务执行以下操作:
所以我认为:
install* 任务,可以将assemble 替换为build(包括lint)并使用 connectedCheck(包括 connectedAndroidTest)。- echo no | android 创建 avd -f -n 测试 -t $ANDROID_TARGET -b $ANDROID_ABI关于android - 模拟器上的 Gradle JUnit Espresso connectedAndroidTest java.lang.IncompatibleClassChangeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27695071/