What is the way to access argc and argv inside a test case in the Google Test platform?

I use Google Test to test my project in C ++. However, some cases require access to argc and argv to load the necessary data.

In the main() method, when initializing, argc and argv are passed to the test constructor.

 testing::InitGoogleTest(&argc, argv); 

How can I access them later in the test?

 TEST(SomeClass, myTest) { // Here I would need to have access to argc and argv } 
+7
source share
3 answers

I don't know the scope of google test, so there may be a better way to do this, but this should do:

 //--------------------------------------------- // some_header.h extern int my_argc; extern char** my_argv; // eof //--------------------------------------------- //--------------------------------------------- // main.cpp int my_argc; char** my_argv; int main(int argc, char** argv) { ::testing::InitGoogleTest(&argc, argv); my_argc = argc; my_argv = argv; return RUN_ALL_TESTS(); } // eof //--------------------------------------------- //--------------------------------------------- // test.cpp #include "some_header.h" TEST(SomeClass, myTest) { // Here you can access my_argc and my_argv } // eof //--------------------------------------------- 

Globals are not very good, but when all you have is a test environment that will not allow you to tunnel some data from main() for any testing functions that you have, they do the job.

+7
source

The command line arguments for your test executable are for the test environment, not your tests. With them, you set things up like --gtest_output , --gtest_repeat or --gtest_filter . The test should be primarily reproducible, but this is not so if it depends on who uses the "correct" parameters.

Anyway, what are you trying to achieve?

+1
source

If you are running Windows using Visual Studio, they are available in __argc and __argv.

+1
source

All Articles