179

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.

3 Answers 3

249

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
12
  • 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, 2012 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? Aug 22, 2012 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. Aug 23, 2012 at 5: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? Aug 23, 2012 at 8: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, 2012 at 8:42
136

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

On the console

You should use the flag --gtest_filter, like

--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.

3
  • 7
    very usefull the part "On the code"!! This is what I was looking for!!
    – Iero
    Aug 30, 2016 at 8:01
  • That link 404s. What is the console command to which you add the flag?
    – Big Money
    Mar 4, 2021 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. Oct 5, 2021 at 10:42
34

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.

2
  • 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, 2021 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. Mar 14 at 17:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.