您可以触发函数以响应 Firebase 测试实验室中测试矩阵的完成。例如,如果测试失败,您可以通知 Slack 频道或发送电子邮件。
有关更多示例用例,请参阅我可以使用 Cloud Functions 做什么? .
在 TestMatrix 完成时触发函数
创建一个新函数,当 TestMatrix 使用事件处理程序functions.testLab.testMatrix().onComplete()
完成时触发:
exports.sendEmailNotification = functions.testLab.testMatrix().onComplete((testMatrix) => {
// ...
});
处理测试状态和结果
函数的每次执行都会传递一个TestMatrix
,其中包括矩阵的最终状态和有助于理解问题的详细信息。
exports.handleTestMatrixCompletion = functions.testLab.testMatrix().onComplete(testMatrix => {
const matrixId = testMatrix.testMatrixId;
switch (testMatrix.state) {
case 'FINISHED':
console.log(`TestMatrix ${matrixId} finished with outcome: ${testMatrix.outcomeSummary}`);
break;
case 'INVALID':
console.log(`TestMatrix ${matrixId} was marked as invalid: ${testMatrix.invalidMatrixDetails}`);
break;
default:
console.log(`TestMatrix ${matrixId} completed with state ${testMatrix.state}`);
}
return null;
});
访问客户详细信息
可以从不同的来源或工作流程创建测试矩阵。因此,通常需要创建根据源或测试的其他重要上下文执行不同操作的函数。为了解决这个问题, gcloud
允许您在开始测试时传递任意信息,这些信息稍后可以在您的函数中访问。例如:
gcloud beta firebase test android run \
--app=path/to/app.apk \
--client-details testType=pr,link=https://path/to/pull-request
示例函数:
exports.notifyOnPullRequestFailure = functions.testLab.testMatrix().onComplete(testMatrix => {
if (testMatrix.clientInfo.details['testType'] != 'pr') {
// Not a pull request
return null;
}
if (testMatrix.state == 'FINISHED' && testMatrix.outcomeSummary == 'SUCCESS') {
// No failure
return null;
}
const link = testMatrix.clientInfo.details['link'];
let message = `Test Lab validation for pull request ${link} failed. `;
if (!!testMatrix.resultStorage.resultsUrl) {
message += `Test results available at ${testMatrix.resultStorage.resultsUrl}. `;
}
// Send notification here ...
});