นำเข้าโมดูลที่จำเป็น
หากต้องการเริ่มต้นใช้งาน ให้นำเข้าโมดูลที่จำเป็นสำหรับการจัดการ Firebase Test Lab เหตุการณ์ดังนี้
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
เรียกใช้ฟังก์ชันเมื่อ TestMatrix เสร็จสมบูรณ์
หากต้องการเรียกใช้ฟังก์ชัน Firebase Test Lab ให้กำหนดตัวแฮนเดิลเลอร์ สำหรับเหตุการณ์เมทริกซ์ทดสอบเสร็จสมบูรณ์ ในตัวอย่างนี้ ฟังก์ชันจะทริกเกอร์เมื่อการทดสอบเสร็จสมบูรณ์ ดึงข้อมูลเมทริกซ์ทดสอบจากออบเจ็กต์ CloudEvent และส่งผลการทดสอบที่เกี่ยวข้องไปยังช่อง 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)
เข้าถึงรายละเอียดไคลเอ็นต์
ระบบอาจสร้างการทดสอบเมทริกซ์จากแหล่งที่มาหรือเวิร์กโฟลว์ต่างๆ ดังนั้นจึงมักเป็นที่ต้องการที่จะสร้างฟังก์ชันที่ดำเนินการต่างๆ ตามแหล่งที่มาหรือบริบทที่สำคัญอื่นๆ ของการทดสอบ gcloud จึงอนุญาตให้คุณส่งข้อมูลที่กำหนดเองเมื่อเริ่มการทดสอบ ซึ่งคุณจะเข้าถึงข้อมูลดังกล่าวได้ในฟังก์ชันในภายหลัง เช่น
gcloud beta firebase test android run \
--app=path/to/app.apk \
--client-details testType=pr,link=<path/to/pull-request>
จากนั้นเข้าถึงข้อมูลในฟังก์ชันได้ดังนี้
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")