Firebase Test Lab 觸發條件


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