Question

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); ?

Was it helpful?

Solution

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?

OTHER TIPS

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();

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:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top