Android command line testing with Gradle

I've researched this for several days and dug up some helpful resources like this user guide . I can also build and install just fine thanks to this article . However, I have been unable, for life, to figure out how to run my measurement and control tests without a device connected.

Is it not possible to run tests under app/src/androidTest

(where I have most of my tests) without using a command ./gradlew connectedAndroidTest

?

Also, I've seen it recommended for testing jUnit in test app/src/test

and Instrumentation in gradle preconfigured app/src/androidTest

. Is this a good way to set up application tests, although does that mean creating two different test directories?

I am asking all this because I am using Jenkins CI job to create an Android project with updatable code and I would like Jenkins to run all my tests along with building the project, hopefully I would not have to deal with building some kind of emulator / device for Jenkins to run tests (as required ./gradlew connectedAndroidTest

).

+3


source to share


2 answers


Yes, this is the correct way to set up an application test as JUnit tests (in the / src / test application) run on the JVM and benchmarks running on the connected device.

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

Possible Solution. During Jenkins prebuild step, start emulator / Genymotion from command line and run all tests with



./gradlew cAT

      

OR keep Genymotion / emulator installed at all times. https://www.genymotion.com/#!/

+2


source


For those who have found this thread the same way I did and thinking about running Android from terminal, perhaps in CI.

Recently I have been working with CI at GitLab. I figured out the best way without gennymotion, with a simple android avd generated by avdmanager. New Android tools are optimized to work with the terminal, which can be used for example in docker images.



More info in this tutorial and update your gitlab compose script as shown 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


 
      

0


source







All Articles