Estendi Firebase Test Lab con Cloud Functions


Attiva una funzione al completamento di TestMatrix

Crea una nuova funzione che si attiva al termine di un TestMatrix con il gestore eventi functions.testLab.testMatrix().onComplete():

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

Gestire gli stati e i risultati dei test

A ogni esecuzione della funzione viene passato un elemento TestMatrix che include lo stato finale della matrice e dettagli per aiutare a comprendere i problemi.

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

Accedi ai dettagli del client

Le matrici di test possono essere create da origini o flussi di lavoro diversi. Per questo motivo, spesso è auspicabile Creare funzioni che eseguono azioni diverse in base all'origine o ad altri importanti contesti il test. Per aiutarti, gcloud ti consente di passare informazioni arbitrarie quando avvii un test a cui potrai accedere in un secondo momento nella funzione. Ad esempio:

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

Funzione di esempio:

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