Googletest: Using gtest command line arguments with test target's command line arguments

Created on 29 Apr 2016  路  6Comments  路  Source: google/googletest

I'm feeding my application with cmd line args. I've been feeding gtest arguments to select a particular test-case (uising --gtest-filter=TestCaseName.*) . I've read from a stack overflow answer that gtest will automatically parse it's arguments and leave the rest for the program (test target).
Is it true? (I've observes that it's not working in my case)
If yes, do I need to pass gtest args before any other arguments? (order)

Most helpful comment

Yes, InitGoogle{Test,Mock} will remove the arguments it recognizes. You don't have to put the gtest arguments before others. You should, however, call InitGoogleTest early, before you start processing arguments yourself.

#include "gtest/gtest.h"

int main(int argc, char **argv)
{
    ::testing::InitGoogleTest(&argc, argv);

    for (int i = 1; i < argc; ++i) {
        printf("arg %2d = %s\n", i, argv[i]);
    }

    return RUN_ALL_TESTS();
}
$ ./argrm_test foo bar baz
arg  1 = foo
arg  2 = bar
arg  3 = baz

$ ./argrm_test foo bar baz --gtest_filter=foo
arg  1 = foo
arg  2 = bar
arg  3 = baz
Note: Google Test filter = foo
[==========] Running 0 tests from 0 test cases.
[==========] 0 tests from 0 test cases ran. (0 ms total)
[  PASSED  ] 0 tests.

All 6 comments

Yes, InitGoogle{Test,Mock} will remove the arguments it recognizes. You don't have to put the gtest arguments before others. You should, however, call InitGoogleTest early, before you start processing arguments yourself.

#include "gtest/gtest.h"

int main(int argc, char **argv)
{
    ::testing::InitGoogleTest(&argc, argv);

    for (int i = 1; i < argc; ++i) {
        printf("arg %2d = %s\n", i, argv[i]);
    }

    return RUN_ALL_TESTS();
}
$ ./argrm_test foo bar baz
arg  1 = foo
arg  2 = bar
arg  3 = baz

$ ./argrm_test foo bar baz --gtest_filter=foo
arg  1 = foo
arg  2 = bar
arg  3 = baz
Note: Google Test filter = foo
[==========] Running 0 tests from 0 test cases.
[==========] 0 tests from 0 test cases ran. (0 ms total)
[  PASSED  ] 0 tests.

In my case I need to access program's command line arguments inside a test case.

For that I had to do a _dirty_ trick of making argc *_and *_argv global variables.

int argc; // Making arg and arv global to access within TESTs
_TCHAR** argv;
TEST(SampleTest, Test0){
 // Here I'm accessing argc and argv
}

// my main
GTEST_API_ int _tmain(int t_argc, _TCHAR** t_argv){

    argc = t_argc;
    argv = t_argv;

    printf("Initializing gtest \n");
    ::testing::InitGoogleTest(&argc, argv);

    return RUN_ALL_TESTS();
}

In this case when I do TheExe.exe --gtest_filter=SampleTest.Test0 arg1 arg2

I'm getting the following error message from Google Test.
error

Sorry the issue got solved.
I used - instead of _

Hi,

Can you elaborate on how you resolved? I'm having the same issue. Where did you need to use - instead of _?

Thanks!
-zack

See the first comment. He used --gtest-filter= originally. This is not recognized by gtest. The correct option is --gtest_filter .

Hello,

when i run make on ${GTEST_DIR}/make
it runs fine but then I try to use filter:
sample1_unittest --gtest_filter:IsPrimeTest.Negative

and result ist that flag is not recognised:

This program contains tests written using Google Test. You can use the
following command line flags to control its behavior:

Test Selection:
--gtest_list_tests
List the names of all tests instead of running them. The name of
TEST(Foo, Bar) is "Foo.Bar".
--gtest_filter=POSTIVE_PATTERNS[-NEGATIVE_PATTERNS]
Run only the tests whose name matches one of the positive patterns but
none of the negative patterns. '?' matches any single character; '*'
matches any substring; ':' separates two patterns.
--gtest_also_run_disabled_tests
Run all disabled tests too.

Test Execution:
--gtest_repeat=[COUNT]
Run the tests repeatedly; use a negative count to repeat forever.
--gtest_shuffle
Randomize tests' orders on every iteration.
--gtest_random_seed=[NUMBER]
Random number seed to use for shuffling test orders (between 1 and
99999, or 0 to use a seed based on the current time).

Test Output:
--gtest_color=(yes|no|auto)
Enable/disable colored output. The default is auto.
--gtest_print_time=0
Don't print the elapsed time of each test.
--gtest_output=(json|xml)[:DIRECTORY_PATH\|:FILE_PATH]
Generate a JSON or XML report in the given directory or with the given
file name. FILE_PATH defaults to test_detail.xml.

Assertion Behavior:
--gtest_break_on_failure
Turn assertion failures into debugger break-points.
--gtest_throw_on_failure
Turn assertion failures into C++ exceptions for use by an external
test framework.
--gtest_catch_exceptions=0
Do not report exceptions as test failures. Instead, allow them
to crash the program or throw a pop-up (on Windows).

Except for --gtest_list_tests, you can alternatively set the corresponding
environment variable of a flag (all letters in upper-case). For example, to
disable colored text output, you can either specify --gtest_color=no or set
the GTEST_COLOR environment variable to no.

For more information, please read the Google Test documentation at
https://github.com/google/googletest/. If you find a bug in Google Test
(not one in your own code or tests), please report it to
googletestframework@googlegroups.com.

Can you help me.? What am I missing?

Thank you.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

robindegen picture robindegen  路  4Comments

maygamboa picture maygamboa  路  6Comments

nholthaus picture nholthaus  路  6Comments

octoploid picture octoploid  路  3Comments

qiuxin picture qiuxin  路  5Comments