Leave a rating/review
There are two ways you can run tests in your projects:
- Inside Android Studio, right click on the test directory inside the project and click Run ’Tests` in ‘Quotes.app…’
- Run the Gradle task for the tests from the command line.
To run tests on a remote machine, you have to go with the second option.
Inside Android Studio, open the Terminal tab. Alternatively, you can also use any other terminal app of your choice.
Run Unit Tests
To run the unit tests for the debug variant of your app, run the following command:
./gradlew testDebugUnitTest
After the tests run, you’ll see a log saying that the build has been successful.
Run Instrumented Tests
To run the instrumented tests, you need to first have a device or emulator connected. Then, run the following command to execute the tests for the debug variant of your app:
./gradlew connectedDebugAndroidTest
Similar to the unit tests, you’ll see a log saying that the build has been successful.
Add Jobs to Workflow
Now that you know how to run the tests from the command line, it’s time to add the jobs to the workflow.
Add the following code inside check_and_deploy.yml:
jobs:
unit_tests:
runs-on: [ubuntu-latest]
steps:
- uses: actions/checkout@v3
- name: set up JDK
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: 11
- name: Unit tests
run: ./gradlew testDebugUnitTest
android_tests:
runs-on: [ macos-12 ]
steps:
- uses: actions/checkout@v3
- name: set up JDK
uses: actions/setup-java@v3
with:
distribution: 'zulu'
java-version: 11
- name: Instrumented Tests
uses: reactivecircus/android-emulator-runner@v2
with:
api-level: 29
script: ./gradlew connectedDebugAndroidTest
This code above does a few things:
- Creates two parallel jobs named
unit_testsandandroid_tests - The
unit_testsjob runs on an Ubuntu runner, which checks out the code and runs the unit tests.ubuntu-latestrefers to the latest available Ubuntu version on Github runners. - The
android_testsjob runs on a macOS 12 runner. If you want to use a specific version of an OS, you can mention the version instead of usinglatest. This job also checks out the code but runs the instrumentation tests instead. To do this, it uses thereactivecircus/android-emulator-runneraction. The emulator can use hardware acceleration only on the macOS emulator. Therefore, this job needs to run on a macOS runner while others can run on Ubuntu runners.