In order to run your android unit tests automatically on external virtual server after each git commit you need a continuous integration system. There are a couple of good services e.g. jenkins and travis out there. But how do you use them? Here is one way for travis. You can start off by forking android-maven-example and adapt it to your own project. Maven’s package manager ability let’s you download and install all dependency packages. In example you also have several maven goals pre-defined. Most important ones are install – installing to a device or emulator, package – creating a jar of your libs and clean.
Now the automated tests are setup and you only need to make an account on https://travis-ci.org/ and turn on all the repositories which you want to let test.
The android maven example travis file is a bit outdated however. Android has been added as a first class citizen. So your travis.yml file could look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | # http://docs.travis-ci.com/user/languages/android/ # https://github.com/andrewhr/rxjava-android-example/blob/master/.travis.yml # http://gmariotti.blogspot.de/2014/04/integrating-android-github-repo-with.html branches: only: - master language: android jdk: - oraclejdk8 android: components: # additional packages with 'android list sdk': http://pastebin.com/ceuXUfNp - android-11 - android-12 - android-13 - android-14 - sysimg-11 - sysimg-12 - sysimg-13 - sysimg-14 #- build-tools-20.0.0 licenses: - android-sdk-license-5be876d5 env: matrix: - ANDROID_SDKS=android-10,build-tools-19.1.0 ANDROID_TARGET=android-10 ANDROID_ABI=armeabi #- ANDROID_SDKS=android-11,sysimg-11,build-tools-19.1.0 ANDROID_TARGET=android-11 ANDROID_ABI=armeabi #- ANDROID_SDKS=android-12,sysimg-12,build-tools-19.1.0 ANDROID_TARGET=android-12 ANDROID_ABI=armeabi #- ANDROID_SDKS=android-13,sysimg-13,build-tools-19.1.0 ANDROID_TARGET=android-13 ANDROID_ABI=armeabi #- ANDROID_SDKS=android-14,sysimg-14,build-tools-19.1.0 ANDROID_TARGET=android-14 ANDROID_ABI=armeabi #- ANDROID_SDKS=android-15,build-tools-19.1.0 ANDROID_TARGET=android-15 ANDROID_ABI=armeabi-v7a #- ANDROID_SDKS=android-16,build-tools-19.1.0 ANDROID_TARGET=android-16 ANDROID_ABI=armeabi-v7a #- ANDROID_SDKS=android-17,build-tools-19.1.0 ANDROID_TARGET=android-17 ANDROID_ABI=armeabi-v7a #- ANDROID_SDKS=android-18,build-tools-19.1.0 ANDROID_TARGET=android-18 ANDROID_ABI=armeabi-v7a #- ANDROID_SDKS=android-19,build-tools-19.1.0 ANDROID_TARGET=android-19 ANDROID_ABI=armeabi-v7a before_install: - android list sdk --no-ui --all --extended | grep -E '^id:' | awk -F '"' '{$1=""; print $2}' - android list targets # bug with language lint-side; web validation though works just fine (http://lint.travis-ci.org/) #- gem install travis-lint #- travis-lint # - echo no | android create avd --force -n test -t android-10 --abi armeabi - echo no | android create avd --force -n test -t $ANDROID_TARGET --abi $ANDROID_ABI - emulator -avd test -no-skin -no-audio -no-window & #-dns-server 8.8.8.8 & install: after_install: before_script: - adb wait-for-device - adb devices -l script: mvn -pl !deviceinfo-app,!SlidingMenu/library,!ActionBarSherlock/actionbarsherlock -P ci -e clean install -Pintegration-tests -Dandroid.device=test -Dandroid.extractDuplicates=true -X #-DignoreTestFailure=true after_script: - adb logcat & after_success: after_failure: |
To run the maven tests locally you can also create an emulator and run the maven goal. Note: On MAC you can also install travis-build to simply run everything by using travis run.
Create the emulator named test with e.g. API 10 and armeabi cpu.
1 | echo no | android create avd --force -n test -t android-10 --abi armeabi |
Start the emulator. Note: No window required for first tests, if you want to tests the ui you might want to remove the -no-window parameter.
1 | emulator -avd test -no-skin -no-audio -no-window & |
Run the test goal. Note: -e -X for showing errors and warnings in the console.
1 | mvn -e clean install -Pintegration-tests -Dandroid.device=test -Dandroid.extractDuplicates=true -X |
See also Travis CI for iOS.