Firebase 測試實驗室觸發器


在 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 ...
});