Deploying Android Apps Using GitHub Actions

May 4 2023 · Kotlin 1.7.21, Android 13, Android Studio Electric Eel

Part 1: Automate Releases with GitHub Actions

03. Run Unit & Instrumented Tests

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 02. Create a GitHub Workflow Next episode: 04. Generate a Signed Build

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Transcript: 03. Run Unit & Instrumented Tests

There are two ways you can run tests in your projects:

  1. Inside Android Studio, right click on the test directory inside the project and click Run ’Tests` in ‘Quotes.app…’
  2. 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:

  1. Creates two parallel jobs named unit_tests and android_tests
  2. The unit_tests job runs on an Ubuntu runner, which checks out the code and runs the unit tests. ubuntu-latest refers to the latest available Ubuntu version on Github runners.
  3. The android_tests job runs on a macOS 12 runner. If you want to use a specific version of an OS, you can mention the version instead of using latest. This job also checks out the code but runs the instrumentation tests instead. To do this, it uses the reactivecircus/android-emulator-runner action. 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.