Gradle JUnit Espresso on Emulator connectedAndroid

2019-04-11 08:52发布

I have a simple unit test for a static method and running them on Emulator. (Goal is to run on cloud CI so I am testing on emulator.)

Gradle 2.2.1 Emulator Android 5.0

I use these steps on console.

  1. android create avd --force -n test -t "android-21"
  2. emulator -avd test -no-skin -no-audio -no-window &
  3. adb wait-for-device
  4. adb shell input keyevent 82 &
  5. gradle clean installDebug
  6. gradle connectedAndroidTest

build.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')
    }
}

The error log is

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.

What could produce this error ?

1条回答
Melony?
2楼-- · 2019-04-11 09:29

Brief response:

This seems a good link about java.lang.IncompatibleClassChangeError. And If you try it on CI:

Emulator is not fully booted after wait-for-device, it's not ready for your tests and your app is not installed due a timeout, so there are no tests performed and the build fails as a new behavior added to alert you about it.

You can replace adb wait-for-device by a loop waiting for stopped state (fully booted) checking adb -e shell getprop init.svc.bootanim as this script in public domain does. Further info here.

Possible CI issue, Double Espresso was deprecated and Espresso 2.0 released:

About CI and no tests found:

I answered another question but specific for Travis-ci here. Similar error but due the script used was bugged. If you run gradle installDebug --debug you'll know more about the error (share the log here).

But running the same steps on a CI server, if I'm right, you'll see an InstallException caused by ShellCommandUnresponsiveException due a two minutes INSTALL_TIMEOUT. You can increase this value using an environment variable ADB_INSTALL_TIMEOUT=6 #minutes, but this is not your problem now.

If you are running it locally first, try it without -no-window (so you see it) or add -no-boot-anim (speed up it but is incompatible with wait-for-emulator script) or use adb wait-for-device && sleep 300 (to be sure emulator is fully booted).

About Espresso:

Double Espresso is deprecated because Espresso 2.0 is now available. Double Espresso is a pure Gradle port of Espresso 1.1 and Jake Wharton deprecated it when version 2.0 was published two weeks ago.

They updated the wiki and JavaDoc (they will move it to android.com).

You can now use the Android Support Repository to download the latest version.

And Google published new samples:

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.

About Gradle tasks:

From Android tasks and Running tests (Gradle Plugin User Guide):

  • assemble The task to assemble the output(s) of the project
  • connectedCheck Runs checks that requires a connected device or emulator.

Checks requiring a connected device are launched with the anchor task called connectedCheck. This depends on the task androidTest and therefore will run it. This task does the following:

  • Ensure the app and the test app are built (depending on assembleDebug and assembleTest)
  • Install both apps
  • Run the tests
  • Uninstall both apps.

So I think:

  • It's a good moment to migrate to Espresso 2.0 (and avoid the dependencies? issue).
  • You need to wait for emulator stopped state and I recommend this link to understand it.
  • You don't need specific install* tasks and can replace assemble by build (includes lint) and use connectedCheck (includes connectedAndroidTest).
  • If there is more than one ABI installed, you'll need to choose one (and answer no): - echo no | android create avd -f -n test -t $ANDROID_TARGET -b $ANDROID_ABI
  • I would try their samples on the CI server you choose as a second step following your goal.
查看更多
登录 后发表回答