如何在运行时将参数传递给 Google 测试点?

How to pass argument to Google test point during runtime?

有没有办法在 运行 时间内将参数传递给 google 个测试点? 我知道我可以使用

静态参数
TEST_P(fooTest,testPointName)
{
}
INSTANTIATE_TEST_CASE_P(InstantiationName,
                        FooTest,
                        ::testing::Values("blah"));

但在我的用例中,我想做类似

的事情
main(){
//do Something
//create instance of ::testing::Values type
//pass arguments to  multiple test points
//run_tests
}

如有指点,我将不胜感激。

据我所知,这对于 value-parameterized 测试是不可能的,因为这些是在 compile-time 使用宏和模板生成的。

您可以在测试本身中编写一个循环,并对每个值执行一次测试。您可以使用 custom error messages or log additional information to make error messages specific for the value. Or perhaps use SCOPED_TRACE.

您可以使用 SetUpTestCase 来实现。在你的 fixture class 中,创建一个静态变量来保存你想要的值。然后在你的 fixture class 中实现一个静态函数 SetUpTestCaseTearDownTestCase 来初始化和清理你的值。这是总体思路:

class MyFixture : public ::testing::Test {
protected:
    static void SetUpTestCase() {
        // Code that sets up m_values for testing
    }

    static void TearDownTestCase() {
        // Code that cleans up m_values. This function can be ommited if
        // there are no specific clean up tasks necessary.
    }

    static MyValues m_values;
};

现在使用 TEST_F 使您的测试成为夹具的一部分。现在 m_values 将在 fixture 中的所有测试 运行 之前初始化一次,所有这些测试都可以访问它,并且将在所有测试执行完毕后进行清理。

更多信息,请查看https://github.com/google/googletest/blob/master/docs/advanced.md#sharing-resources-between-tests-in-the-same-test-suite