Extend Firebase Test Lab with Cloud Functions

Import the required modules

To get started, import the modules required for handlingFirebase Test Labevents:

Node.js

// The Cloud Functions for Firebase SDK to set up triggers and logging.const{onTestMatrixCompleted}=require("firebase-functions/testLab");const{logger}=require("firebase-functions");

Python

# The Cloud Functions for Firebase SDK to set up triggers and logging.fromfirebase_functionsimporttest_lab_fn,params# The requests library to send web requests to Slack.importrequests

Trigger a function on TestMatrix completion

To trigger aFirebase Test Lab function, define a handlerfor the test matrix completion event. In this example, the functiontriggers on test completion, retrieves the test matrix data fromthe CloudEvent object, and sendsthe corresponding test results to a Slack channel:

Node.js

exports.posttestresultstoslack=onTestMatrixCompleted({secrets:["SLACK_WEBHOOK_URL"]},async(event)=>{// Obtain Test Matrix properties from the CloudEventconst{testMatrixId,state,outcomeSummary}=event.data;// Create the title of the messageconsttitle=`${getSlackmoji(state)}${getSlackmoji(outcomeSummary,)}${testMatrixId}`;// Create the details of the messageconstdetails=`Status: *${state}*${getSlackmoji(state,)}\nOutcome: *${outcomeSummary}*${getSlackmoji(outcomeSummary)}    `;// Post the message to slackconstslackResponse=awaitpostToSlack(title,details);// Log the responselogger.log(slackResponse);});

Python

@test_lab_fn.on_test_matrix_completed(secrets=["SLACK_WEBHOOK_URL"])defposttestresultstoslack(event:test_lab_fn.CloudEvent[test_lab_fn.TestMatrixCompletedData])->None:"""Posts a test matrix result to Slack."""# Obtain Test Matrix properties from the CloudEventtest_matrix_id=event.data.test_matrix_idstate=event.data.stateoutcome_summary=event.data.outcome_summary# Create the title of the messagetitle=f"{slackmoji(state)}{slackmoji(outcome_summary)}{test_matrix_id}"# Create the details of the messagedetails=(f"Status: *{state}*{slackmoji(state)}\n"f"Outcome: *{outcome_summary}*{slackmoji(outcome_summary)}")# Post the message to Slackresponse=post_to_slack(title,details)# Log the responseprint(response.status_code,response.text)

Access client details

Test matrices may be created from different sources or workflows. It istherefore often desirable to create functions that perform different actionsbased on the source or other important context of the test. To help with this,gcloud allows you to pass arbitrary information when starting a test that canbe accessed later in your function. For example:

gcloud beta firebase test android run \    --app=path/to/app.apk \    --client-details testType=pr,link=<path/to/pull-request>

And then to access the information in your function:

Node.js

consttestType=event.data.clientInfo.details.testType;constlink=event.data.clientInfo.details.link;

Python

test_type:str|None=event.data.client_info.details.get("testType")link:str|None=event.data.client_info.details.get("link")

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-12-17 UTC.