How-to run Android tests on Firebase TestLab

tamaspapik
Wed, 05 Jul 2017 13:21:47 +0000
Android

On Android stacks we have the gcloud and gsutil tools pre-installed, this guide is based on these tools. If you want to use it on other stacks, you will need to install these first.

###What you’ll need

a Google Cloud Platform service account and its JSON key file.

Read more about creating and configuring service accounts:
Service accounts | Compute Engine Documentation | Google Cloud

Google Cloud Testing API and Cloud Tool Results API enabled in your console.

Read more about how to enable and disable Google APIs:
Getting started | Cloud APIs | Google Cloud

the project to be linked to Firebase
gsutil and gcloud use Google services, so the best is if you create the project on Google Cloud Platform Console, and then import this project into Firebase

Read more about adding projects to Firebase:
https://help.pandasuite.com/articles/create-project-firebase

###Create your app as usual

Running Firebase TestLab tests is similar to the initial Android build workflow, but some modifications will be needed.

###Modify the workflow to use Firebase for tests (in this case: espresso instrumentation)

1. Upload your service account’s JSON key file on Code Signing tab, under Generic File Storage: name it SERVICE_ACCOUNT_KEY and drop the file into the box, so the downloadable environment variable that contains the url will be: $BITRISEIO_SERVICE_ACCOUNT_KEY_URL

2. Create a new variable under Env Vars tab. Name it GCP_PROJECT and set its value to the project ID used in Firebase Console and/or in Google Cloud Platform Console.

3. In the Gradle Runner step
We will need two APKs exported to BITRISE_DEPLOY_DIR: both unaligned, one test apk and the other a debug apk.

Change gradle_task to: "assembleDebug assembleDebugAndroidTest"

Remove everything from apk_file_exclude_filter, leave it empty

Set apk_file_include_filter to: "*-unaligned.apk"

4. Add a script step after your gradle-runner step, and set it’s content to:

           #!/bin/bash
            set -ex

            #Download service account key
            curl -o /tmp/sacc_key.json $BITRISEIO_SERVICE_ACCOUNT_KEY_URL
            #Activate cloud client with the service account
            gcloud auth activate-service-account -q --key-file /tmp/sacc_key.json
            #Set the project's id used on Google Cloud Platform
            gcloud config set project $GCP_PROJECT

            RESULT_DIR="build-$BITRISE_BUILD_NUMBER"

            APK="--app=$BITRISE_DEPLOY_DIR/app-debug-unaligned.apk --test=$BITRISE_DEPLOY_DIR/app-debug-androidTest-unaligned.apk"

            TYPE="instrumentation"
            DEVICES="--device model=Nexus6,version=21,locale=en,orientation=portrait"

            gcloud firebase test android run $APK $DEVICES --type=$TYPE --results-dir=$RESULT_DIR/ >>$BITRISE_DEPLOY_DIR/gcloudlog.log 2>&1

            SRCPTH=$BITRISE_DEPLOY_DIR/test_results
            EXPPTH=$BITRISE_DEPLOY_DIR
            mkdir $SRCPTH

            #Download test results

            FB_BUCKET=$(grep -oP "(?<=browser\/).*(?<=\/$RESULT_DIR)" $BITRISE_DEPLOY_DIR/gcloudlog.log)

            gsutil -m cp -R gs://$FB_BUCKET $SRCPTH

            for file in $(find $SRCPTH -type f)
            do
                if [[ -f $file ]]; then
                   f=$(echo ${file//$SRCPTH\/$RESULT_DIR\/})
                   mv $file $(echo $EXPPTH/${f//\//_})
                fi
            done

If everything is configured correctly, you should see your test results among build artifacts.

If you encounter any issues or have a question, please drop a line here and we’ll figure it out as fast as possible.

Happy Coding

17 posts - 8 participants