[英]gtest: What is the best way to perform some actions only when the specific fixture must be run?
我有一個派生自::testing::Test 的類和其中的幾個固定裝置。 我還重新實現SetUpTestCase
方法,該方法必須啟動這些測試所需的輔助應用程序。 但是現在我想添加一個新的固定裝置,它需要使用一些額外的參數來啟動該端應用程序以啟用日志記錄。 問題是,我希望僅當我確定新測試在運行列表中並且不會錯過時才記錄它,否則不需要記錄。 所以我想寫這樣的東西:
class MyTest : public ::testing::Test
{
public:
static void SetUpTestCase()
{
std::vector<std::string> args;
args.push_back("--silent");
// if (TestLogging fixture will be run)
args.push_back("--enableLog");
//Start the side application with arguments "args"
}
};
TEST_F(MyTest, Test1)
{/**/}
TEST_F(MyTest, Test2)
{/**/}
TEST_F(MyTest, TestLogging)
{/**/}
有什么辦法可以達到我期望的行為嗎? 或者也許我不應該搞砸SetUpTestCase
並且有更好的方法來做到這一點?
您可以在SetUp
和TearDown
查詢測試名稱並將其與TestLogging
匹配。
如果您想要進行多個日志記錄測試,您可以使用TestLogging
后綴命名它們,並檢查測試名稱是否以該后綴開頭。
然后SetUp
會做額外的設置工作,而TearDown
會還原它。
class MyTest : public ::testing::Test {
protected:
inline static std::vector<std::string> args{};
public:
inline static void SetUpTestCase() {
args.push_back("--silent");
}
virtual void SetUp() {
std::string test_name{ testing::UnitTest::GetInstance()->current_test_info()->name() };
if (test_name.starts_with("TestLogging")) {
args.push_back("--enableLog");
}
}
virtual void TearDown() {
std::string test_name{ testing::UnitTest::GetInstance()->current_test_info()->name() };
if (test_name.starts_with("TestLogging")) {
args.pop_back();
}
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.