27 Mart 2025'ten itibaren AOSP'yi derlemek ve AOSP'ye katkıda bulunmak için aosp-main
yerine android-latest-release
kullanmanızı öneririz. Daha fazla bilgi için AOSP'de yapılan değişiklikler başlıklı makaleyi inceleyin.
HAL testi için parametreli GTest
Koleksiyonlar ile düzeninizi koruyun
İçeriği tercihlerinize göre kaydedin ve kategorilere ayırın.
HAL arayüzü için birden fazla uygulama olabilir. HAL uygulamasının her örneğini test etmek için standart yöntem, değer parametreli bir GTest yazmaktır.
Temel test kurulumu
GTest, testing::TestWithParam
temel sınıfını devralmalıdır. Bu sınıfta parametre, her örneğin adıdır. SetUp
yönteminde, hizmet aşağıdaki kod snippet'inde gösterildiği gibi örnek adına göre örneklendirilebilir.
// 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);
...
}
Her test yöntemi için aşağıdaki örnekte gösterildiği gibi TEST_P
makrosunu kullanın:
TEST_P(UsbHidlTest, setCallback) {
...
}
Aşağıdaki örnekte gösterildiği gibi paketi INSTANTIATE_TEST_SUITE_P
makrosuyla örneklendirin:
INSTANTIATE_TEST_SUITE_P(
PerInstance, UsbHidlTest,
testing::ValuesIn(android::hardware::getAllHalInstanceNames(IUsb::descriptor)),
android::hardware::PrintInstanceNameToString);
Bağımsız değişkenler şunlardır:
InstantiationName
, testinizle eşleşen herhangi bir şey olabilir. PerInstance
yaygın bir addır.
Test sınıfının adı.
Yerleşik yöntemden alınabilen örnek adları koleksiyonu (ör. getAllHalInstanceNames
).
Test yöntemi adını yazdırma yöntemi.
PrintInstanceNameToString
, örnek adına ve test yöntemi adına göre bir test adı derlemek için kullanabileceğiniz yerleşik bir addır.
GTest, değer parametreli testler için tuple'leri destekler. Bir HAL testinin birden fazla girişle test edilmesi gerektiğinde (ör. birden fazla arayüz içeren bir test), test parametresi olarak tuple
içeren bir GTest yazabilirsiniz. Kodun tamamını VtsHalGraphicsMapperV2_1TargetTest
adresinde bulabilirsiniz.
Tek bir test parametresi içeren GTest'e kıyasla bu testin, aşağıdaki örnekte gösterildiği gibi test parametresi olarak tuple
kullanması gerekir:
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())));
…
}
Daha karmaşık parametrelere ihtiyaç duyuluyorsa yapı ve özel GTest ToString
işlevleri kullanmanız önerilir.
Test paketini örneklemek için INSTANTIATE\_TEST\_CASE\_P
makrosu da kullanılır. Bu makroda iki fark vardır:
- Üçüncü bağımsız değişken, temel durumdaki dize koleksiyonunun aksine bir tuple koleksiyonudur.
- Test adını derleme yönteminin
tuple
'ü desteklemesi gerekir. Aşağıdaki örnekte gösterildiği gibi, dize tuple'lerini işleyebilecek yerleşik PrintInstanceTupleNameToString
yöntemini kullanabilirsiniz:
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<>);
Bu sayfadaki içerik ve kod örnekleri, İçerik Lisansı sayfasında açıklanan lisanslara tabidir. Java ve OpenJDK, Oracle ve/veya satış ortaklarının tescilli ticari markasıdır.
Son güncelleme tarihi: 2025-07-27 UTC.
[[["Anlaması kolay","easyToUnderstand","thumb-up"],["Sorunumu çözdü","solvedMyProblem","thumb-up"],["Diğer","otherUp","thumb-up"]],[["İhtiyacım olan bilgiler yok","missingTheInformationINeed","thumb-down"],["Çok karmaşık / çok fazla adım var","tooComplicatedTooManySteps","thumb-down"],["Güncel değil","outOfDate","thumb-down"],["Çeviri sorunu","translationIssue","thumb-down"],["Örnek veya kod sorunu","samplesCodeIssue","thumb-down"],["Diğer","otherDown","thumb-down"]],["Son güncelleme tarihi: 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);"]]