Estendi Firebase Test Lab con Cloud Functions


Attiva una funzione al completamento di TestMatrix

Crea una nuova funzione che si attiva quando un TestMatrix viene completato con il gestore eventi functions.testLab.testMatrix().onComplete() :

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

Gestire gli stati e i risultati dei test

Ad ogni esecuzione della funzione viene passato un 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 cliente

Le matrici di test possono essere create da diverse fonti o flussi di lavoro. Pertanto è spesso desiderabile creare funzioni che eseguano azioni diverse in base alla fonte o ad altro contesto importante del test. Per aiutarti in questo, gcloud ti consente di passare informazioni arbitrarie quando avvii un test a cui puoi accedere successivamente nella tua funzione. Per 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 ...
});