您可以觸發函數以響應 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 ...
});