簡體   English   中英

Google測試:使用現有測試夾具類的參數化測試?

[英]Google Test: Parameterized tests which use an existing test fixture class?

我有一個測試夾具類,目前許多測試都在使用它。

#include <gtest/gtest.h>
class MyFixtureTest : public ::testing::Test {
  void SetUp() { ... }
};

我想創建一個參數化測試,該測試還使用MyFixtureTest必須提供的所有功能,而無需更改所有現有測試。

我怎么做?

我在網上發現了類似的討論,但是還沒有完全理解他們的答案。

現在, Google測試文檔中已回答了這個問題(VladLosev的回答在技​​術上是正確的,但可能還有更多工作要做)

具體來說,當您想向現有燈具類添加參數時,可以執行

class MyFixtureTest : public ::testing::Test {
  ...
};
class MyParamFixtureTest : public MyFixtureTest,
                           public ::testing::WithParamInterface<MyParameterType> {
  ...
};

TEST_P(MyParamFixtureTest, MyTestName) { ... }

問題在於,對於常規測試,夾具必須從testing :: Test派生,對於參數化測試,它必須從testing :: TestWithParam <>派生。

為了適應這種情況,您必須修改燈具類才能使用參數類型

template <class T> class MyFixtureBase : public T {
  void SetUp() { ... };
  // Put the rest of your original MyFixtureTest here.
};

// This will work with your non-parameterized tests.
class MyFixtureTest : public MyFixtureBase<testing::Test> {};

// This will be the fixture for all your parameterized tests.
// Just substitute the actual type of your parameters for MyParameterType.
class MyParamFixtureTest : public MyFixtureBase<
    testing::TestWithParam<MyParameterType> > {};

這樣,您可以在使用以下命令創建參數化測試時保持所有現有測試不變

TEST_P(MyParamFixtureTest, MyTestName) { ... }

如果您創建了一個從該通用夾具派生的新夾具,然后在該派生類上創建了參數化測試,那么這將對您有所幫助並解決您的問題嗎?

在Google Test Wiki頁面上 :“在Google Test中,您可以通過將共享邏輯放在基本測試夾具中來在測試用例之間共享夾具,然后從該底座中為想要使用此通用邏輯的每個測試用例派生一個單獨的夾具。”

暫無
暫無

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

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