ตั้งแต่วันที่ 27 มีนาคม 2025 เป็นต้นไป เราขอแนะนำให้ใช้ android-latest-release
แทน aosp-main
เพื่อสร้างและมีส่วนร่วมใน AOSP โปรดดูข้อมูลเพิ่มเติมที่หัวข้อการเปลี่ยนแปลงใน AOSP
GTest แบบมีพารามิเตอร์สําหรับการทดสอบ HAL
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
สำหรับอินเทอร์เฟซ HAL อาจมีการใช้งานหลายวิธี หากต้องการทดสอบอินสแตนซ์แต่ละรายการสําหรับการติดตั้งใช้งาน HAL วิธีมาตรฐานคือการเขียน GTest แบบพารามิเตอร์ค่า
การตั้งค่าการทดสอบขั้นพื้นฐาน
GTest ต้องรับช่วงคลาสพื้นฐาน testing::TestWithParam
ซึ่งพารามิเตอร์คือชื่อของอินสแตนซ์แต่ละรายการ ในเมธอด SetUp
สามารถสร้างอินสแตนซ์ของบริการตามชื่ออินสแตนซ์ได้ ดังที่แสดงในข้อมูลโค้ดต่อไปนี้
// The main test class for the USB hidl HAL
class UsbHidlTest : public testing::TestWithParam<std::string> {
virtual void SetUp() override {
usb = IUsb::getService(GetParam());
ASSERT_NE(usb, nullptr);
...
}
สำหรับวิธีการทดสอบแต่ละรายการ ให้ใช้มาโคร TEST_P
ดังที่แสดงในตัวอย่างต่อไปนี้
TEST_P(UsbHidlTest, setCallback) {
...
}
สร้างอินสแตนซ์ชุดทดสอบด้วยมาโคร INSTANTIATE_TEST_SUITE_P
ดังที่แสดงในตัวอย่างต่อไปนี้
INSTANTIATE_TEST_SUITE_P(
PerInstance, UsbHidlTest,
testing::ValuesIn(android::hardware::getAllHalInstanceNames(IUsb::descriptor)),
android::hardware::PrintInstanceNameToString);
อาร์กิวเมนต์มีดังนี้
InstantiationName
ซึ่งอาจเป็นอะไรก็ได้ที่ตรงกับการทดสอบของคุณ PerInstance
เป็นชื่อทั่วไป
ชื่อคลาสทดสอบ
คอลเล็กชันชื่ออินสแตนซ์ ซึ่งดึงมาจากเมธอดในตัวได้ เช่น getAllHalInstanceNames
วิธีการพิมพ์ชื่อเมธอดทดสอบ
PrintInstanceNameToString
เป็นชื่อในตัวที่คุณสามารถใช้เพื่อรวบรวมชื่อการทดสอบตามชื่ออินสแตนซ์และชื่อวิธีการทดสอบ
GTest รองรับทูเปิลสําหรับการทดสอบที่มีพารามิเตอร์ค่า เมื่อการทดสอบ HAL ต้องมีการทดสอบด้วยอินพุตหลายรายการ (เช่น การทดสอบที่มีอินเทอร์เฟซหลายรายการ) คุณสามารถเขียน GTest โดยให้ tuple
เป็นพารามิเตอร์การทดสอบ ดูรหัสที่สมบูรณ์ได้ใน VtsHalGraphicsMapperV2_1TargetTest
เมื่อเทียบกับ GTest ที่มีพารามิเตอร์การทดสอบรายการเดียว การทดสอบนี้ต้องใช้ tuple
เป็นพารามิเตอร์การทดสอบตามที่แสดงในตัวอย่างต่อไปนี้
class GraphicsMapperHidlTest
: public ::testing::TestWithParam<std::tuple<std::string, std::string>> {
protected:
void SetUp() override {
ASSERT_NO_FATAL_FAILURE(mGralloc = std::make_unique<Gralloc>(std::get<0>(GetParam()),
std::get<1>(GetParam())));
…
}
หากต้องการพารามิเตอร์ที่ซับซ้อนมากขึ้น เราขอแนะนำให้ใช้โครงสร้างและฟังก์ชัน GTest ToString
ที่กําหนดเอง
หากต้องการสร้างอินสแตนซ์ชุดทดสอบ ก็ต้องใช้มาโคร INSTANTIATE\_TEST\_CASE\_P
ด้วยเช่นกัน โดยมีความแตกต่าง 2 อย่างดังนี้
- อาร์กิวเมนต์ที่ 3 คือคอลเล็กชันของทูเพล (เทียบกับคอลเล็กชันสตริงในกรณีพื้นฐาน)
- วิธีการคอมไพล์ชื่อการทดสอบต้องรองรับ
tuple
คุณสามารถใช้เมธอดในตัว PrintInstanceTupleNameToString
ซึ่งจัดการทูเพลตสตริงได้ ดังตัวอย่างต่อไปนี้
INSTANTIATE_TEST_CASE_P(
PerInstance, GraphicsMapperHidlTest,
testing::Combine(
testing::ValuesIn(
android::hardware::getAllHalInstanceNames(IAllocator::descriptor)),
testing::ValuesIn(android::hardware::getAllHalInstanceNames(IMapper::descriptor))),
android::hardware::PrintInstanceTupleNameToString<>);
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[[["เข้าใจง่าย","easyToUnderstand","thumb-up"],["แก้ปัญหาของฉันได้","solvedMyProblem","thumb-up"],["อื่นๆ","otherUp","thumb-up"]],[["ไม่มีข้อมูลที่ฉันต้องการ","missingTheInformationINeed","thumb-down"],["ซับซ้อนเกินไป/มีหลายขั้นตอนมากเกินไป","tooComplicatedTooManySteps","thumb-down"],["ล้าสมัย","outOfDate","thumb-down"],["ปัญหาเกี่ยวกับการแปล","translationIssue","thumb-down"],["ตัวอย่าง/ปัญหาเกี่ยวกับโค้ด","samplesCodeIssue","thumb-down"],["อื่นๆ","otherDown","thumb-down"]],["อัปเดตล่าสุด 2025-07-27 UTC"],[],[],null,["# Parameterized GTest for HAL testing\n\nFor a HAL interface, there might be multiple implementations. To test each\ninstance for a HAL implementation, the standard way is to write\na [value-parameterized GTest](https://github.com/google/googletest/blob/main/docs/advanced.md#value-parameterized-tests).\n\nBasic test setup\n----------------\n\nThe GTest must inherit the base class `testing::TestWithParam`, of\nwhich the parameter is the name of each instance. In the\n`SetUp` method, the service can be instantiated based on the\ninstance name, as shown in the following code snippet. \n\n // The main test class for the USB hidl HAL\n class UsbHidlTest : public testing::TestWithParam\u003cstd::string\u003e {\n\n virtual void SetUp() override {\n usb = IUsb::getService(GetParam());\n ASSERT_NE(usb, nullptr);\n ...\n }\n\nFor each test method, use the macro `TEST_P` as shown in the following example: \n\n TEST_P(UsbHidlTest, setCallback) {\n ...\n }\n\nInstantiate the suite with\nmacro `INSTANTIATE_TEST_SUITE_P`, as shown in the following example: \n\n INSTANTIATE_TEST_SUITE_P(\n PerInstance, UsbHidlTest,\n testing::ValuesIn(android::hardware::getAllHalInstanceNames(IUsb::descriptor)),\n android::hardware::PrintInstanceNameToString);\n\nThe arguments are:\n\n1. `InstantiationName`, which can be anything that\n matches your test. `PerInstance` is a common name.\n\n2. The test class name.\n\n3. A collection of instance names, which can\n be retrieved from the built-in method, for example,\n `getAllHalInstanceNames`.\n\n4. The method to print the test method name.\n `PrintInstanceNameToString` is a built-in name you can use to\n compile a test name based on the instance name and test method name.\n\nTest with multiple inputs\n-------------------------\n\nGTest supports tuples for value-parameterized tests. When a\nHAL test requires testing with multiple inputs (for example, a test with\nmultiple interfaces), you can write a GTest with `tuple` as the\ntest parameter. The complete\ncode can be found in [`VtsHalGraphicsMapperV2_1TargetTest`](https://cs.android.com/android/platform/superproject/+/android-latest-release:hardware/interfaces/graphics/mapper/2.1/vts/functional/VtsHalGraphicsMapperV2_1TargetTest.cpp).\n\nCompared to the GTest with a single test parameter, this test needs to use\n`tuple` as the test parameter as shown in the following example: \n\n class GraphicsMapperHidlTest\n : public ::testing::TestWithParam\u003cstd::tuple\u003cstd::string, std::string\u003e\u003e {\n protected:\n void SetUp() override {\n ASSERT_NO_FATAL_FAILURE(mGralloc = std::make_unique\u003cGralloc\u003e(std::get\u003c0\u003e(GetParam()),\n std::get\u003c1\u003e(GetParam())));\n ...\n }\n\nIf more complicated parameters are needed, it's recommended to use a\nstructure and custom GTest `ToString` functions.\n\nTo instantiate the test suite, the macro `INSTANTIATE\\_TEST\\_CASE\\_P` is also\nused, with two differences:\n\n- The third argument is a collection of tuples (versus a collection of strings in the basic case).\n- The method to compile a test name needs to support `tuple`. You can use the built-in method `PrintInstanceTupleNameToString`, which can handle tuples of strings, as shown in the following example:\n\n INSTANTIATE_TEST_CASE_P(\n PerInstance, GraphicsMapperHidlTest,\n testing::Combine(\n testing::ValuesIn(\n android::hardware::getAllHalInstanceNames(IAllocator::descriptor)),\n testing::ValuesIn(android::hardware::getAllHalInstanceNames(IMapper::descriptor))),\n android::hardware::PrintInstanceTupleNameToString\u003c\u003e);"]]