Passing argument to google test executable

5,757 views
Skip to first unread message

jaonary

unread,
Jun 8, 2011, 11:19:54 AM6/8/11
to Google C++ Testing Framework
Hi all,

I'm wondering how to pass an argument to my google test executables.
My tests need to load datas that are stored somewhere in my filesystem
and I need a way to pass
this directory as an argument to my tests. From now, I hard coded it
in my test cases but I found that this solution is not scalabe. So is
it possible to pass this as a command line argument when I run the
tests ?

Best regards,

Jaonary

dirk...@gmail.com

unread,
Jun 10, 2011, 10:30:14 AM6/10/11
to jaonary, Google C++ Testing Framework
Hello Jaonary,

usually you pass all command line arguments to gtest for the --gtest_*
arguments with something like

testing::InitGoogleTest(&argc, argv);

Nothing prevents you from passing other than the original arguments.
Here is a un-tested example code:
int main(int argc, char** argv) {

const char* data_path = NULL;

char** gtest_argv = malloc(sizeof(char*) * argc);
gtest_argv[0] = argv[0]; // simply copy first argument
int gtest_argc = 1;

for (int i = 1; < argc; i++) {
if (strcmp(argv[i], "data_path") != 0) {
gtest_argv[gtest_argc++] = argv[i];
} else {
data_path = argv[i];
}
}

testing::InitGoogleTest(&gtest_argc, gtest_argv);
free(gtest_argv);

Here is a stackoverflow question about the same problem:
http://stackoverflow.com/questions/4818785/how-to-pass-parameters-to-the-gtest

I hope this helps.
Dirk

Reply all
Reply to author
Forward
0 new messages