gpt4 book ai didi

c++ - 如何在运行时将参数传递给谷歌测试点?

转载 作者:行者123 更新时间:2023-11-30 05:04:02 24 4
gpt4 key购买 nike

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

静态参数
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
}

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

最佳答案

您可以使用 SetUpTestCase 来实现。在你的夹具类中,创建一个静态变量来保存你想要的值。然后在您的 fixture 类中实现静态函数 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

关于c++ - 如何在运行时将参数传递给谷歌测试点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49117657/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com