簡體   English   中英

gtest:僅當必須運行特定裝置時才執行某些操作的最佳方法是什么?

[英]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並且有更好的方法來做到這一點?

您可以在SetUpTearDown查詢測試名稱並將其與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.

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