Running Android Espresso Tests on Jenkins with Headless Emulator

I wanted to get espresso tests running under Jenkins, these instructions are for Jenkins running on Ubuntu (17.04) and you have already got Jenkins running and building Android projects. After some messing around I got it working. This is what you will need to do.

  1. Install the package “qemu-kvm”.
    >sudo apt-get install qemu-kvm
  2. Add the Jenkins user to the kvm group.
    >sudo usermod -a -G kvm jenkins
  3. Build the android-emulator plugin for Jenkins, the current version in the Jenkins plugins repository is too old to work. I found a fork on GitHub which has many of the issues fixed. The GitHub page is https://github.com/wbrunette/android-emulator-plugin.
    Clone and build, using the “upgrade” branch.
    >git clone https://github.com/wbrunette/android-emulator-plugin.git
    >cd android-emulator-plugin
    >git checkout -b origin/upgrade
    >mvn
  4. Install the android-emulator plugin into Jenkins. The plugin can be found in target/android-emulator.hpi
  5. Install the “JUnit Plugin”.
  6. Restart Jenkins.
  7. Configure the project you want to run tests under.
  8. Scroll down to “Build Environment” and check “Run an Android emulator during build”.
  9. Fill in the options to match what I have below, depending on your app and tests, you may want to change the orientation to landscape and the android version/target abi. These ones work well with the current android version 26 images.
  10. Add an “Execute Shell” build step, as the first build step. Enter the following command to stop the soft keyboard from appearing in editors and pushing items off the display.
    $ANDROID_HOME/platform-tools/adb -s $ANDROID_AVD_DEVICE wait-for-device shell settings put secure show_ime_with_hard_keyboard 0
  11. Add the “connectedAndroidTest” to the “Invoke Gradle script” “Task” section.
  12. Add the “Publish JUnit test result report” post build step. Point “Test report XMLs” to the location of the build report. This will be
    [appname]/build/outputs/androidTest-results/connected/*.xml
    Where [appname] is the path of your app under build – usually “app” in a single target project.
  13. “Save” the configuration, and run a build.

The build will either complete without any failed tests, or it will fail. I have had it fail a few times without any real reason but it seems fairly stable. When looking at the project status page you will get a graph of the “Test Result Trend” and be able to look at the test results. A logcat.txt is also saved as an artifact.

 

1 Comment


  1. Wow. That is so elegant and logical and clearly explained. Brilliantly goes through what could be a complex process and makes it obvious.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *