Cloud Functions로 Firebase Test Lab 확장


시작하려면 Firebase Test Lab 이벤트 처리에 필요한 모듈을 가져옵니다.

Node.jsPython
 // The Cloud Functions for Firebase SDK to set up triggers and logging.
const {onTestMatrixCompleted} = require("firebase-functions/v2/testLab");
const {logger} = require("firebase-functions");
 # The Cloud Functions for Firebase SDK to set up triggers and logging.
from firebase_functions import test_lab_fn, params

# The requests library to send web requests to Slack.
import requests

TestMatrix 완료 시 함수 트리거

Firebase Test Lab 함수를 트리거하려면 테스트 매트릭스 완료 이벤트의 핸들러를 정의합니다. 이 예시에서 함수는 테스트 완료 시 트리거되고, CloudEvent 객체에서 테스트 매트릭스 데이터를 검색하며, 상응하는 테스트 결과를 Slack 채널로 전송합니다.

Node.jsPython
exports.posttestresultstoslack = onTestMatrixCompleted(
    {secrets: ["SLACK_WEBHOOK_URL"]},
    async (event) => {
    // Obtain Test Matrix properties from the CloudEvent
      const {testMatrixId, state, outcomeSummary} = event.data;

      // Create the title of the message
      const title = `${getSlackmoji(state)} ${getSlackmoji(
          outcomeSummary,
      )} ${testMatrixId}`;

      // Create the details of the message
      const details = `Status: *${state}* ${getSlackmoji(
          state,
      )}\nOutcome: *${outcomeSummary}* ${getSlackmoji(outcomeSummary)}
    `;

      // Post the message to slack
      const slackResponse = await postToSlack(title, details);

      // Log the response
      logger.log(slackResponse);
    });
@test_lab_fn.on_test_matrix_completed(secrets=["SLACK_WEBHOOK_URL"])
def posttestresultstoslack(
        event: test_lab_fn.CloudEvent[test_lab_fn.TestMatrixCompletedData]) -> None:
    """Posts a test matrix result to Slack."""

    # Obtain Test Matrix properties from the CloudEvent
    test_matrix_id = event.data.test_matrix_id
    state = event.data.state
    outcome_summary = event.data.outcome_summary

    # Create the title of the message
    title = f"{slackmoji(state)} {slackmoji(outcome_summary)} {test_matrix_id}"

    # Create the details of the message
    details = (f"Status: *{state}* {slackmoji(state)}\n"
               f"Outcome: *{outcome_summary}* {slackmoji(outcome_summary)}")

    # Post the message to Slack
    response = post_to_slack(title, details)

    # Log the response
    print(response.status_code, response.text)

클라이언트 세부정보 액세스

테스트 매트릭스는 다양한 소스나 워크플로에서 생성될 수 있습니다. 따라서 대부분의 경우 테스트의 소스나 기타 중요 컨텍스트를 기반으로 다양한 작업을 수행하는 함수를 만드는 것이 좋습니다. 이를 위해 gcloud를 사용하면 테스트를 시작할 때 임의의 정보를 전달할 수 있으며 이후에 함수에서 이 정보에 액세스할 수 있습니다. 예를 들면 다음과 같습니다.

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

그런 다음 함수의 정보에 액세스할 수 있습니다.

Node.jsPython
const testType = event.data.clientInfo.details.testType;
const link = event.data.clientInfo.details.link;
test_type: str | None = event.data.client_info.details.get("testType")
link: str | None = event.data.client_info.details.get("link")