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

标签 c++ visual-studio-2010 googletest

我已经为谷歌测试配置了 visual studio。然后我在 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();
}

我的问题是如何不运行所有 (RUN_ALL_TESTS) 测试而是运行一个特定的测试用例?是否有任何宏,例如运行(simpleTest.test1); ?

最佳答案

如果需要,您可以使用 GTEST_FLAG 宏将命令行标志编译到测试可执行文件中(参见 Running Test Programs: Advanced Options)

例如,在您的情况下,您可以:

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

但是,像这样的硬编码测试过滤器通常是不可取的,因为每次您想要更改过滤器时都需要重新编译。

就通过 Visual Studio 在运行时传递标志而言,我想您知道您可以将 --gtest_filter=simpleTest.test1 添加到“调试”选项中的命令参数目标的属性页?

关于c++ - 如何在 visual studio 中运行单个 google 测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19798879/

相关文章:

c++ - gdi+ GetWorldTransform() 它在 directx 中的什么位置?

c++ - 如何在 C++ 中为非常量和本地数据伪造依赖类型?

c# - 您知道有可用的 F# 和 C# 互操作示例吗?

vb.net - 修复 'Error class is ambiguous in the namespace' 保持其模糊性

c++ - Lambda 捕获 C++14

c++ - 为什么我可以从 char * const 值创建 char * vector ?

.net - 什么是 Visual Studio 2010 Shell?

c++ - 如何为二维数组编写一个好的GMock匹配器?

c++ - 我可以将参数传递给 googletest 测试函数吗

c++ - 如果 Google 测试中的测试失败,如何打印一些东西?