How to run single google test in visual studio?
Asked Answered
R

4

5

I've configured visual studio for google test. Then I've written some simple google test cases in vs2010, as You can see below:

TEST(simpleTest, test1){
    float base = 4.f;
    float exponent = 1.f;
    float expectedValue = 4.f;
    float actualValue = pow(base, exponent);
    EXPECT_FLOAT_EQ(expectedValue, actualValue);
}
TEST(simpleTest, test2){
    float base = 4.f;
    float exponent = 2.f;
    float expectedValue = 16.f;
    float actualValue = pow(base, exponent);
    EXPECT_FLOAT_EQ(expectedValue, actualValue);
}
int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  RUN_ALL_TESTS();
}

My question is how to run not all (RUN_ALL_TESTS) the tests but one specific test case? Is there any macro e.g. RUN(simpleTest.test1); ?

Rectrix answered 5/11, 2013 at 21:0 Comment(2)
You can run one at runtime by using the --gtest_filter option with a glob-based matching pattern. For example, run your test executable with --gtest_filter=*test2. See the documentation for more details. Do you want to select one specific test at run time or at compile time?Smarm
I know that I can use "--gtest_filter" and I am using it when I am programming e.g. on Linux but I would like to run it in visual studio just specific one test not all. In other words when I am using this: RUN_ALL_TESTS(); all test cases which I have got defined are executing, but I don't want to execute alll of them only one specific. The question is how to run one specific test case is there any macro e.g. RUN(simpleTest.test1); ?Rectrix
C
13

You can compile the command line flags into your test executable if you want by using the GTEST_FLAG macro (see Running Test Programs: Advanced Options)

So for example, in your case you could do:

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

However, hardcoding test filters like this is normally undesirable, since you need to recompile every time you want to change the filter.

As far as passing the flags at run-time via Visual Studio, I guess you know that you can just add --gtest_filter=simpleTest.test1 to the Command Arguments in the "Debugging" option of your target's Property Pages?

Cornhusk answered 5/11, 2013 at 23:5 Comment(0)
M
4

There isn't a macro to specify a single test. There is just RUN_ALL_TESTS.

I think this is by design since running all tests usually preferable. However, if you want to put it in code, just fake the command line arguments like this:

const char *testv[2]=
{
    "gtest",
    "--gtest_filter=simpleTest.test1",
};
int testc=2;

::testing::InitGoogleTest(&testc, (char**)testv);
int result = RUN_ALL_TESTS();
Matins answered 5/11, 2013 at 22:16 Comment(1)
Nice. Using GTEST_FLAG() is a better approach, from Fraser's solution: ::testing::GTEST_FLAG(filter) = "simpleTest.test1"Matins
A
0

I haven't really understood whether you indeed want to hardcode your single test, or if you want to decide at test execution time which single test shall be run. If the latter is what you want, you can make use of these VS extensions which integrate your tests into VS' test explorer:

Auto answered 20/7, 2017 at 17:43 Comment(0)
T
0

Via the google test adapter, you can execute it like this enter image description here

Tarsometatarsus answered 20/11, 2022 at 11:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.