Louie Bertoncin
Louie Bertoncin

Reputation: 4033

Android Command Line Testing with Gradle

I have been researching this for multiple days now and dug up some good resources like this user guide. I can also build and install just fine thanks to this article. However, I cannot, for the life of me, figure out how to run my instrumentation tests without having a connected device.

Is it simply not possible to run the tests under app/src/androidTest (where I have most of my tests) without using the command ./gradlew connectedAndroidTest?

Also, I've seen it's recommended to put jUnit tests under app/src/test and Instrumentation tests under the gradle preconfigured app/src/androidTest. Is this a good way to set up the app's tests, even though that means creating two different testing directories?

I'm asking all of this because I'm using a Jenkins CI job to build the Android project upon the code being updated and I'd like Jenkins to run all of my tests along with building the project, but I'd hope I wouldn't have to concern myself with creating some sort of emulator/device for Jenkins to run the tests on (as required by ./gradlew connectedAndroidTest).

Upvotes: 3

Views: 2902

Answers (2)

lukassos
lukassos

Reputation: 379

For those who have found this topic same as I and are thinking about running Android from terminal, possibly in CI.

I have been working with CI on GitLab lately. I have figured out a better way without gennymotion, with mere android avd created by avdmanager. New Android Tools are optimized to run from terminal, which can be used in docker images for example.

More info in this tutorial and update the gitlab compose script as below.

 
image: openjdk:8-jdk



variables: # such as ..
  ANDROID_SDK_TOOLS_URL: "https://dl.google.com/android/repository/sdk-tools-linux-3859397.zip"  # https://dl.google.com/android/repository/sdk-tools-linux-3859397.zip
  ANDROID_SDK_VERSION: "26"      # 26
  ANDROID_BUILD_TOOLS: "26.0.2"      # 26.0.1


before_script:
  - apt-get --quiet update --yes
  - apt-get --quiet install --yes wget tar unzip lib32stdc++6 lib32z1
  - wget --quiet --output-document=android-tools.zip ${ANDROID_SDK_TOOLS_URL}
  - unzip android-tools.zip -d android-sdk-linux
  - echo y | android-sdk-linux/tools/bin/sdkmanager "platform-tools" --verbose # SDK Platform-Tools
  - echo y | android-sdk-linux/tools/bin/sdkmanager "platforms;android-${ANDROID_SDK_VERSION}" --verbose # SDK Platform
  - echo y | android-sdk-linux/tools/bin/sdkmanager "build-tools;${ANDROID_BUILD_TOOLS}" --verbose # SDK Build-Tools
  - echo y | android-sdk-linux/tools/bin/sdkmanager "extras;android;m2repository" --verbose # Support Repository
  - echo y | android-sdk-linux/tools/bin/sdkmanager "extras;google;m2repository" --verbose # Google Repository
  - echo y | android-sdk-linux/tools/bin/sdkmanager "extras;google;google_play_services" --verbose # Google Play services
  - export ANDROID_HOME=$PWD/android-sdk-linux
  - export PATH=$PATH:$PWD/android-sdk-linux/platform-tools/
  - chmod +x ./gradlew

stages:
  - build
  - test

build:
  stage: build
  script:
    - ./gradlew assembleDebug
  artifacts:
    paths:
    - app/build/outputs/

unitTests:
  stage: test
  script:
    - ./gradlew test

functionalTests:
  stage: test
  script:
    - wget --quiet --output-document=android-wait-for-emulator https://raw.githubusercontent.com/travis-ci/travis-cookbooks/0f497eb71291b52a703143c5cd63a217c8766dc9/community-cookbooks/android-sdk/files/default/android-wait-for-emulator
    - chmod +x android-wait-for-emulator
    - echo y | android-sdk-linux/tools/bin/sdkmanager --verbose --update
    - echo y | android-sdk-linux/tools/bin/sdkmanager --verbose "system-images;android-${ANDROID_SDK_VERSION};google_apis;x86"
    - echo no | android-sdk-linux/tools/bin/avdmanager create avd -n test -k "system-images;android-${ANDROID_SDK_VERSION};google_apis;x86" # no custom HW profile
    - android-sdk-linux/tools/emulator -avd test -no-window -no-audio & # run headless "test" AVD
    - ./android-wait-for-emulator # if ran fast enough it will catch cange of state on Boot Animation ~ init.svc.bootanim
    - adb shell input keyevent 82 # some dummy input
    - ./gradlew cAT


 

Upvotes: 2

Shivam Miglani
Shivam Miglani

Reputation: 21

Yes, it is the proper way to set up app's test as JUnit tests (in app/src/test) run on JVM and Instrumentation tests run on connected device.

http://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html

Possible solution - In pre-build step on Jenkins, run emulator/Genymotion from commandline and run all tests with

./gradlew cAT

OR keep the Genymotion/emulator setup running always. https://www.genymotion.com/#!/

Upvotes: 2

Related Questions