0

My setup is Visual Studio 2015 with the gtest test runner

https://visualstudiogallery.msdn.microsoft.com/94c02701-8043-4851-8458-34f137d10874

Everything works fine. But now I want to pass command line arguments to

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

When debugging I can pass them with Properties->Debugging->Command Argument. That works fine.

I can run the test with Test->Run->Selected Test but how can I pass the command line arguments to the test? It's not taking over the debugging Command Arguments.

Kees
  • 41
  • 1
  • 3

2 Answers2

0

What I do for my C++ unit tests is to have them run as part of the build by adding a custom build step that runs after linking. The custom build step runs the test. Therefore a successful build means that the code compiled and linked and that the tests passed. This gives you assurance that your tests are always running (you can't build successfully without the tests running and passing). I describe how to set this up in Visual Studio in my 5-part blog post on Test-Driven Development.

I looked at the documentation for that VS extension and it is unclear if it supports passing any command-line arguments to the test executable. However, this is trivial to achieve with a custom build step. You might consider using the Q&A section of the Visual Studio Gallery page for that extension to see if they support passing command-line arguments.

legalize
  • 2,214
  • 18
  • 25
  • Thank you for your answer. Your proposal is exactly what we did before (we used post build steps). But we want to move to the test runner because if you want to debug your test and the test fails your unable to debug. It just quits before it starts the debugger because the build fails. And a lot of times I don't want to run all tests when I'm busy with one module, especially when there are to many tests and dependencies. But there are also good reasons to run the tests via the custom build steps. Thanks for the link, nice to read. – Kees Dec 10 '15 at 08:38
  • Here's how I run the tests in the debugger when the tests are compiling, but failing at runtime. You set the startup project as the test project and do F5 to start the debugger. When VS prompts you to build the solution first, just say No. – legalize Dec 10 '15 at 20:29
0

Another workaround is to set the arguments in the Environment Variable and call in the main using getenv()(its cross platform at least).

int main(int argc, char * argv[])
{
    if(argc== 1)
    {
        char* path = getenv("your-argument-env-variable");
        // check if path is not null
    }
    else
    {
        // Default reading from command argument
    }

    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}
AmirulOm
  • 73
  • 1
  • 6