An instrumentation unit test is a unit test that is executed on an actual machine or emulator. For this test, you can take advantage of the Android framework API and supporting APIs such as AndroidX Test. [^ 1] This article describes how to run unit tests on the Android emulator on Docker without using the actual device. Unit tests are run automatically by GitLab CI.
Enter the registration token:
in the GitLab admin area http: // <GitLab address>/admin/runners
. .. docker
inEnter an executor:
Use Android Emulator Container Scripts.
docker run \
-d \
--restart=always \
-e ADBKEY="$(cat ~/.android/adbkey)" \
-e EMULATOR_PARAMS="-wipe-data" \
--device /dev/kvm \
--publish 8554:8554/tcp \
--publish 5555:5555/tcp \
--name android-emulator \
us-docker.pkg.dev/android-emulator-268719/images/30-google-x64:30.1.2
By specifying the option --restart = always
, the Android emulator will be started automatically in the following cases. [^ 2]
By specifying the environment variable EMULATOR_PARAMS ="-wipe-data "
, the user data on the emulator will be deleted when the Android emulator is started. [^ 3]
For example, exiting the Android emulator with the adb reboot -p
command will launch the emulator with user data removed. Repeating unit tests may leave unnecessary files in shared storage, etc. They will be erased. Of course, it should be a unit test that doesn't leave such files so that it doesn't affect other unit test results.
The steps above will always launch the Android emulator on your Docker container. When you no longer need the Android emulator, delete the container with the following command.
docker rm -f android-emulator
Create a .gitlab-ci.yml
file in the root directory of your repository.
yml:.gitlab-ci.yml
image: androidsdk/android-30:latest
cache:
paths:
- .gradle/wrapper
- .gradle/caches
before_script:
- export GRADLE_USER_HOME="${PWD}/.gradle"
- chmod +x ./gradlew
stages:
- check
- connectedCheck
check:
interruptible: true
stage: check
script:
- ./gradlew -Pci --console=plain assembleDebug lintDebug testDebugUnitTest
artifacts:
expire_in: 1 week
paths:
- app/build/outputs/
- app/build/reports/
connectedCheck:
interruptible: true
stage: connectedCheck
before_script:
- adb connect 172.17.0.1:5555
script:
- ./gradlew -Pci --console=plain connectedDebugAndroidTest
artifacts:
expire_in: 1 week
paths:
- app/build/reports/androidTests/
172.17.0.1
is the IP address of the host PC as seen from the Docker container.
This may vary depending on your environment and Docker network settings.
For Docker Desktop for Mac and Docker Desktop for Windows
You can also access it with host.docker.internal
instead of the IP address. [^ 4]
Recommended Posts