简体   繁体   English

如何在visual studio中运行单个google测试?

[英]How to run single google test in visual studio?

I've configured visual studio for google test. 我为google测试配置了visual studio。 Then I've written some simple google test cases in vs2010, as You can see below: 然后我在vs2010中写了一些简单的google测试用例,如下所示:

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? 我的问题是如何运行并非所有(RUN_ALL_TESTS)测试,而是一个特定的测试用例? Is there any macro eg RUN(simpleTest.test1); 有没有宏,例如RUN(simpleTest.test1); ?

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 ) 如果需要,可以使用GTEST_FLAG宏将命令行标志编译到测试可执行文件中(请参阅运行测试程序:高级选项

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? 至于通过Visual Studio在运行时传递标志,我想你知道你可以在目标的属性页的“调试”选项中将--gtest_filter=simpleTest.test1添加到命令参数中吗?

There isn't a macro to specify a single test. 没有用于指定单个测试的宏。 There is just RUN_ALL_TESTS. 只有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: 如果后者是您想要的,您可以使用这些VS扩展,将您的测试集成到VS'测试资源管理器中:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM