Trình kích hoạt Phòng thử nghiệm Firebase


Kích hoạt một hàm khi hoàn thành TestMatrix

Tạo một hàm mới sẽ kích hoạt khi TestMatrix hoàn tất bằng trình xử lý sự kiện functions.testLab.testMatrix().onComplete():

exports.sendEmailNotification = functions.testLab.testMatrix().onComplete((testMatrix) => {
  // ...
});

Xử lý các trạng thái và kết quả kiểm thử

Mỗi lần thực thi hàm được truyền một TestMatrix, trong đó bao gồm trạng thái cuối cùng của ma trận và thông tin chi tiết để giúp bạn hiểu rõ vấn đề.

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

Truy cập thông tin chi tiết về ứng dụng

Có thể tạo ma trận kiểm thử từ nhiều nguồn hoặc quy trình công việc. Do đó, bạn nên tạo các hàm thực hiện nhiều hành động dựa trên nguồn hoặc ngữ cảnh quan trọng khác của chương trình kiểm thử. Để hỗ trợ việc này, gcloud cho phép bạn truyền thông tin tuỳ ý khi bắt đầu kiểm thử mà bạn có thể truy cập sau này trong hàm. Ví dụ:

gcloud beta firebase test android run \
    --app=path/to/app.apk \
    --client-details testType=pr,link=https://path/to/pull-request

Hàm ví dụ:

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