簡體   English   中英

谷歌測試中的測試夾具有什么用?

[英]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();
}

我們可以看到的是:

  1. 遵循DRY(不要重復自己)原則。 您不必重復創建一些測試助手對象。 在 TEST_F - 宏創建此實例。
  2. 代碼使用 TEST_F 更安全。 請參閱MyTest..shallDoY - 您是否發現使用了錯誤的測試助手對象,而不是 testname 所承諾的對象。

因此,如果您的測試需要一些測試助手類,最好使用 TEST_F。 如果不是 - 然后使用測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM