[英]How can i decorate Google Test fixture
我有一些測試:
class Somefixture: ::testing::Test{};
class Somefixture2: ::testing::Test{};
TEST_F(SomeFixture, SomeName)
{
// ...
}
如何自動將測試鏈接到兩個燈具(裝飾)?
TEST_F2(SomeFixture, SomeFixture2, SomeName){}
雖然所需的結果就像我寫的那樣:
TEST_F(SomeFixture, SomeName)
{
// ...
}
TEST_F(SomeFixture2, SomeName)
{
// ...
}
沒有不必要的代碼重復
除了一個小例外(兩個測試不能使用相同的名稱),這應該正確地執行:
#define TEST_F2(F1, F2, Name) \
template <struct Fixture> struct MyTester##Name : Fixture { \
void test##Name(); \
}; \
\
TEST_F(MyTester##Name<F1>, Name##1){ test##Name(); } \
TEST_F(MyTester##Name<F2>, Name##2){ test##Name(); } \
\
template <struct Fixture> void MyTester##Name::test##Name()
這將調用兩個測試,每個測試都使用MyTester作為從兩個固定裝置之一繼承的固定裝置。 由於do_test是MyTester的成員,因此它可以訪問固定裝置中所有繼承的成員。 測試框架將為每個測試創建一個MyTester對象,並且相應的實際燈具將被創建為基類對象。 為避免與其他測試的命名沖突或在TEST_F2的不同調用之間發生命名沖突,我在模板名稱和測試方法名稱后附加了名稱。 TEST_F宏調用提供了名稱和索引。 我沒有進行測試,因為我沒有Google Test,但是許多測試框架中的機制都類似。
如何自動將測試鏈接到兩個燈具(裝飾)?
通過添加一個通用的基類:
class CommonFixture
{
public:
// add member variables and initialize them in the constructor
};
class Somefixture1 : ::testing::Test, protected CommonFixture{}
class Somefixture2 : ::testing::Test, protected CommonFixture{}
測試保持不變:
TEST_F(SomeFixture1, SomeName)
{
// ...
}
TEST_F(SomeFixture2, SomeName)
{
// ...
}
現在,您已經為Somefixture1和Somefixture2有了一個通用的燈具。 您可以從測試中訪問這些通用對象。
您可以采用似乎不錯的BЈовић
方法。
或者需要對測試本身進行少量更改的另一種方法可以是具有“超級”類,該類將兩個實例都作為成員。
class superFuxture
{
public:
Somefixture1 f1;
Somefixture2 f2;
}
然后,您將進行如下測試:
TEST_F(superFuxture, SomeName)
{
//when you were accessing a member of Somefixture1 you'll now need to do:
//f1.SomeFixture1Member
}
Google Test有兩種在不同上下文中執行同一測試主體的方式: 值參數化測試或類型/類型參數化測試 。 不完全是您想要的,而是它提供的最接近的東西。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.