您可以触发函数以响应 Firebase 测试实验室中测试矩阵的完成。例如,如果测试失败,您可以通知 Slack 频道或发送电子邮件。
在 TestMatrix 完成时触发函数
要触发 Firebase 测试实验室功能,请使用firebase-functions/v2/testLab
子包。您可以在 TestMatrix 使用onTestMatrixCompleted()
事件处理程序完成时触发函数。
在此示例中,函数从 CloudEvent 对象检索TestMatrix
数据并将相应的测试结果发送到 Slack 通道:
exports.posttestresultstoslack = onTestMatrixCompleted( {secrets: ["SLACK_WEBHOOK_URL"]}, async (event) => { // Obtain Test Matrix properties from the CloudEvent const {testMatrixId, state, outcomeSummary} = event.data; // Create the title of the message const title = `${getSlackmoji(state)} ${getSlackmoji( outcomeSummary, )} ${testMatrixId}`; // Create the details of the message const details = `Status: *${state}* ${getSlackmoji( state, )}\nOutcome: *${outcomeSummary}* ${getSlackmoji(outcomeSummary)} `; // Post the message to slack const slackResponse = await postToSlack(title, details); // Log the response logger.log(slackResponse); });
访问客户详细信息
可以从不同的来源或工作流程创建测试矩阵。因此,通常需要创建根据源或测试的其他重要上下文执行不同操作的函数。为了解决这个问题, gcloud
允许您在开始测试时传递任意信息,这些信息稍后可以在您的函数中访问。例如:
gcloud beta firebase test android run \
--app=path/to/app.apk \
--client-details testType=pr,link=<path/to/pull-request>