自 2025 年 3 月 27 日起,我们建议您使用 android-latest-release
而非 aosp-main
构建 AOSP 并为其做出贡献。如需了解详情,请参阅 AOSP 的变更。
用于 HAL 测试的参数化 gtest
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
对于 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 测试需要多个输入(例如,有着多个接口的测试),您可以编写一个使用 tuple
作为测试参数的 GTest。完整的代码可在 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
,但有两个不同之处:
- 第三个参数是元组的集合(在基本用例中是一组字符串)。
- 编译测试名称的方法需要支持
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 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-03-26。
[null,null,["最后更新时间 (UTC):2025-03-26。"],[],[],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);"]]