I have a C++11 project with many googletest unit tests looking like
TEST_F(GTest, testSomething) {
int64_t n = 42;
// following code depends on input size n
...
}
Rather than having a local constant n in each test, I’d like to be able to set the input size from one location, preferably the command line:
./RunMyProgram --gtest_filter=* --n=1000
The main should look like:
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();
}
With what should I replace ? in my test functions?
TEST_F(GTest, testSomething) {
int64_t n = ?;
// following code depends on input size n
...
}
First of all, if you use the same value/parameter in more than one of your test functions, consider to use Fixtures.
What you are trying to do for me looks like a “value parameterized test”. I guess thats rather common in testing world, and – tadaa, Google Test has a chapter in its advanced guide, called “Value Parameterized Test” (and oh, it uses fixtures).