Mở rộng Phòng thử nghiệm Firebase bằng Cloud Functions

Nhập các mô-đun bắt buộc

Để bắt đầu, hãy nhập các mô-đun cần thiết để xử lý các Firebase Test Lab sự kiện:

Node.js

 // The Cloud Functions for Firebase SDK to set up triggers and logging.
const {onTestMatrixCompleted} = require("firebase-functions/testLab");
const {logger} = require("firebase-functions");

Python

 # 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

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

Để kích hoạt một hàm Firebase Test Lab, hãy xác định một trình xử lý cho sự kiện hoàn tất ma trận thử nghiệm. Trong ví dụ này, hàm sẽ kích hoạt khi quá trình kiểm thử hoàn tất, truy xuất dữ liệu ma trận thử nghiệm từ đối tượng CloudEvent và gửi kết quả kiểm thử tương ứng đến một kênh Slack:

Node.js

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

Python

@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)

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

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

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

Sau đó, để truy cập thông tin trong hàm của bạn:

Node.js

const testType = event.data.clientInfo.details.testType;
const link = event.data.clientInfo.details.link;

Python

test_type: str | None = event.data.client_info.details.get("testType")
link: str | None = event.data.client_info.details.get("link")