如何在多个单元测试中使用用户输入变量?

cls*_*udt 3 c++ unit-testing global-variables googletest

我有一个C++ 11项目,其中包含许多googletest单元测试

TEST_F(GTest, testSomething) {
    int64_t n = 42;
    // following code depends on input size n
    ...
}
Run Code Online (Sandbox Code Playgroud)

n我希望能够从一个位置设置输入大小,而不是在每个测试中都有一个局部常量,最好是命令行:

./RunMyProgram --gtest_filter=* --n=1000
Run Code Online (Sandbox Code Playgroud)

main应该是这样的:

int main(int argc, char **argv) {

     // TODO: parse command line argument n here

    INFO("=== starting unit tests ===");

    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();

}
Run Code Online (Sandbox Code Playgroud)

我应该?在测试功能中替换什么?

TEST_F(GTest, testSomething) {
    int64_t n = ?;
    // following code depends on input size n
    ...
}
Run Code Online (Sandbox Code Playgroud)

Arn*_*rtz 5

首先,如果在多个测试函数中使用相同的值/参数,请考虑使用Fixtures.

你想要为我做什么看起来像一个"价值参数化测试".我猜这在测试世界中很常见,并且--Tadaa,Google Test在其高级指南中有一章称为"值参数化测试"(哦,它使用灯具).