186

I am trying to write a function/method for my project, which will ask to user which all test cases are you going to run? It looks like below...,

Test_Cases_1
         |_TestNo1
         |_TestNo2....so on
Test_Cases_2
         |_TestNo1
         |_TestNo2....so on
....
....so on
Test_Cases_N
         |_TestNo1
         |_TestNo2....so on

So, now the challenge is while running the project it should prompt me what all test cases you would like to execute? If I select Test_Cases_1 and Test_Cases_N. Then it should execute these two test cases and should exclude all other from Test_Cases_2 to ..... In result window also I would like to see the results of Test_Cases_1 and Test_Cases_N.

So, if I will see the GoogleTest, there is a method called test_case_to_run_count(); But all the test cases are getting registered with Test_F() method. So, I did lots of analysis, but still did not find any solution. Please help me.

Rasmi Ranjan Nayak
  • 11,510
  • 29
  • 82
  • 122

3 Answers3

255

You could use advanced options to run Google tests.

To run only some unit tests you could use --gtest_filter=Test_Cases1* command line option with value that accepts the * and ? wildcards for matching with multiple tests. I think it will solve your problem.

UPD:

Well, the question was how to run specific test cases. Integration of gtest with your GUI is another thing, which I can't really comment, because you didn't provide details of your approach. However I believe the following approach might be a good start:

  1. Get all testcases by running tests with --gtest_list_tests
  2. Parse this data into your GUI
  3. Select test cases you want ro run
  4. Run test executable with option --gtest_filter
DerKasper
  • 167
  • 2
  • 11
nogard
  • 9,432
  • 6
  • 33
  • 53
  • I'd recommend this, and if you need a prompt-based system then I'd suggest creating a wrapper script that gets the user input and runs the test program accordingly. – boycy Aug 22 '12 at 15:13
  • @nogard: Yes, you are right, but my/our aim is to have a GUI, which will have the `Tree` of `TestCases` and `TestNo`. So, do you think `--gtest_filter=..` will help? – Rasmi Ranjan Nayak Aug 22 '12 at 19:11
  • @nogard: Thanks A Lot for the help... I will go ahead as you have directed.. I will let you know once I am done. – Rasmi Ranjan Nayak Aug 23 '12 at 05:08
  • 1
    @nogard: I am doing the same as you have directed; `int main(int argc, char **argv) { //::testing::GTEST_FLAG(list_tests) = true; // For Testing InitGoogleTest(&argc, argv); ::testing::GTEST_FLAG(list_tests) = true; ........ RUN_ALL_TEST();...}` But it prints only the Testcase and TestNo. and but it does not run the tests. So, how can I run the tests now? – Rasmi Ranjan Nayak Aug 23 '12 at 08:27
  • @RasmiRanjanNayak: I proposed not to change the main at all. I propose to run tests twice: 1st time to run with --gtest_list_tests, then 2nd run for the selected tests with option --gtest_filter – nogard Aug 23 '12 at 08:42
  • @nogard: I know, I am asking for more help from you. Could you please write a sudo code for me? And could you please tell me where to put the same? It's my request to you. – Rasmi Ranjan Nayak Aug 23 '12 at 09:04
  • @nogard: That's Ok, No Problem, But anyhow Thanks for all the help. – Rasmi Ranjan Nayak Aug 23 '12 at 09:53
  • Simply forward the command-line arguments to the call to `testing::InitGoogleTest`. This call is usually found in the `main(argc, argv)` – rwong Aug 02 '13 at 20:09
  • `--gtest_filter` was exactly what I needed. Big question now is, why isn't it `--gtest-filter`... – dwanderson Dec 10 '15 at 14:16
  • Just to clarify, the argument to `--gtest_filter` is **not** a regular expression (as mentioned in the answer), but a glob pattern. From the doc: _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._ – BeeOnRope Feb 28 '17 at 22:10
  • It is probably worth noting that as of gtest-1.8.1 if you have a test called `foo`, then executing `--gtest_filter=foo` **will not** execute that test. It costed me some time and confusion, and it turns out that if you look at the output of `--gtest_list_tests`, you will see there're sections, like `tests-a`, `tests-b`, and each of them has subsections naming tests, like `foo`, `bar`, etc… Now, the actual syntax needs to be `section.subsection`, e.g. you want to run `foo` it's `--gtest_filter=tests-a.foo`. – Hi-Angel Oct 10 '19 at 13:28
  • The information I was looking for when coming here was "which character should I use to separate test names in gtest_filter". I only found the answer to that question (:) in the last answer on this page; could you include this information in your accepted answer? – Corentin Schreiber Apr 28 '20 at 07:51
143

Summarising Rasmi Ranjan Nayak's and nogard's answers and adding another option:

On the console

You should use the flag --gtest_filter, like this (quotes needed with wildcards),

--gtest_filter="Test_Cases1*"

(You can also do this in Properties|Configuration Properties|Debugging|Command Arguments)

On the environment

You should set the variable GTEST_FILTER like

export GTEST_FILTER = "Test_Cases1*"

On the code

You should set a flag filter, like

::testing::GTEST_FLAG(filter) = "Test_Cases1*";

such that your main function becomes something like

int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    ::testing::GTEST_FLAG(filter) = "Test_Cases1*";
    return RUN_ALL_TESTS();
}

See section Running a Subset of the Tests for more info on the syntax of the string you can use.

Tom
  • 5
  • 2
Jorge Leitao
  • 19,085
  • 19
  • 85
  • 121
  • 7
    very usefull the part "On the code"!! This is what I was looking for!! – Iero Aug 30 '16 at 08:01
  • That link 404s. What is the console command to which you add the flag? – Big Money Mar 04 '21 at 19:58
  • @BigMoney In case you are still interested, I've updated the link. Anyway the console command is simply the name of the executable that corresponds to your unit test. The string indicated here is a command line argument that must be added after it. – Fabio says Reinstate Monica Oct 05 '21 at 10:42
36

Finally I got some answer, ::test::GTEST_FLAG(list_tests) = true; //From your program, not w.r.t console.

If you would like to use --gtest_filter =*; /* =*, =xyz*... etc*/ // You need to use them in Console.

So, my requirement is to use them from the program not from the console.

Updated:-

Finally I got the answer for updating the same in from the program.

 ::testing::GTEST_FLAG(filter) = "*Counter*:*IsPrime*:*ListenersTest.DoesNotLeak*";//":-:*Counter*";
      InitGoogleTest(&argc, argv);
RUN_ALL_TEST();

So, Thanks for all the answers.

You people are great.

Rasmi Ranjan Nayak
  • 11,510
  • 29
  • 82
  • 122
  • Very useful but looks like InitGoogleTest( ) should come BEFORE setting the GTEST_FLAG. Correct? I tried the "summary" answer given below by @jorge and it seems to work for me. – venk Jul 15 '21 at 16:34
  • @venk I was wondering about this too. I believe the answer depends on what we want overriding what. Looking at the google test source code, it seems like setting the `filter` is setting a global and calling `InitGoogleTest` will override that global setting. Changing the order so that `InitGoogleTest` is called first, I believe then means the command line filter setting won't be able to override the filter. Compiling and testing this theory seems to support it. – Louis Langholtz Mar 14 '23 at 17:27