How to run specific test cases in GoogleTest
Asked Answered
F

3

191

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.

Foret answered 22/8, 2012 at 15:4 Comment(0)
A
262

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
Ane answered 22/8, 2012 at 15:10 Comment(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.Gigi
@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?Foret
@nogard: Thanks A Lot for the help... I will go ahead as you have directed.. I will let you know once I am done.Foret
@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?Foret
@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_filterAne
@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.Foret
@nogard: That's Ok, No Problem, But anyhow Thanks for all the help.Foret
Simply forward the command-line arguments to the call to testing::InitGoogleTest. This call is usually found in the main(argc, argv)Cabby
--gtest_filter was exactly what I needed. Big question now is, why isn't it --gtest-filter...Shainashaine
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.Certified
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.Bewley
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?Somme
C
150

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.

Coupler answered 22/8, 2012 at 15:4 Comment(3)
very usefull the part "On the code"!! This is what I was looking for!!Salable
That link 404s. What is the console command to which you add the flag?Willem
@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.Subatomic
F
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.

Foret answered 23/8, 2012 at 12:27 Comment(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.Fortnightly
@Fortnightly 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.Tissue

© 2022 - 2024 — McMap. All rights reserved.