[英]What is the use of a test fixture in google test?
Google 建議盡可能使用文本夾具構造函數/析構函數而不是 SetUp()/TearDown() ( https://google.github.io/googletest/faq.html#CtorVsSetUp )。 假設我這樣做,即使使用測試夾具有什么用? 以下有什么不同,第一個的優勢是什么?
TEST_F(MyFixture, MyTest) {
... run test, using member functions of MyFixture for common code ...
}
TEST(MySuite, MyTest) {
MyFixture fixture; // will call ctor
... run test, using public functions of MyFixture for common code ...
} // will call dtor
當存在多個 TEST/TEST_F 時,優勢顯而易見。 相比:
TEST(MyTest, shallX)
{
MyTest test;
test.setUpX();
test.objectUnderTest.doX();
}
TEST(MyTest, shallY)
{
OtherTest test;
test.setUpY();
test.objectUnderTest.doY();
}
和
TEST_F(MyTest, shallX)
{
setUpX();
objectUnderTest.doX();
}
TEST_F(MyTest, shallY)
{
setUpY();
objectUnderTest.doY();
}
我們可以看到的是:
MyTest..shallDoY
- 您是否發現使用了錯誤的測試助手對象,而不是 testname 所承諾的對象。因此,如果您的測試需要一些測試助手類,最好使用 TEST_F。 如果不是 - 然后使用測試。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.