Mở rộng Phòng thí nghiệm kiểm tra Firebase với các chức năng đám mây


Kích hoạt chức năng khi hoàn thành TestMatrix

Tạo một hàm mới kích hoạt khi TestMatrix hoàn thành với hàm xử lý sự 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 tra

Mỗi lần thực thi hàm của bạn đều được thông qua một TestMatrix bao gồm trạng thái cuối cùng của ma trận và các chi tiết để giúp hiểu 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 chi tiết khách hàng

Ma trận thử nghiệm có thể được tạo từ các nguồn hoặc quy trình công việc khác nhau. Do đó, người ta thường mong muốn tạo ra các hàm thực hiện các hành động khác nhau dựa trên nguồn hoặc bối cảnh quan trọng khác của thử nghiệm. Để giải quyết vấn đề này, gcloud cho phép bạn chuyển thông tin tùy ý khi bắt đầu kiểm tra để có thể truy cập sau này trong hàm của bạn. 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 ...
});