c++ - gtest 是否支持文件比较?

标签 c++ unit-testing googletest file-comparison

我想使用 gtest 添加一个单元测试来测试我的代码是否可以生成与引用文件相同的文件。 gtest 是否具有获取两个文件并进行比较的功能?

最佳答案

Does gtest has a function to take two files and compare them?

没有,gtest中没有这样的函数。

您可以将生成的文件读入 std::string,并将该文件与您在测试用例中声明的文件进行比较:

 std::ifstream t("generated_file.txt");
 std::string genfile((std::istreambuf_iterator<char>(t)),
                      std::istreambuf_iterator<char>());
 std::string expectedOutput = R"xxx(Expected
 output
 goes
 here
 verbatim
 )xxx";

 ASSERT_EQUAL(expectedOutput,genfile);

关于c++ - gtest 是否支持文件比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48589258/

相关文章:

python - 尝试将非有序查询集与多个有序值进行比较 django

asp.net-mvc - 如何以正确的方式对 ASP.NET MVC 2.0 Controller 进行单元测试!

Java 比 C 快 2 倍以上(作为 C++ 的子集)

c++ - 将 pos_infin 作为超时传递给 timed_wait 时,年份超出有效范围

spring - 类不存在@TestExecutionListeners

c++ - 使用 Google Test 中的 EXPECT_EQ 比较两个 boost::variant 对象

c++ - 如何在 bazel/googletest 中使用环境变量

c++ - 谷歌测试 EXPECT_EQ 和 boost::make_recursive_variant

c++ - 通过查找重复的顶点从 vec3 对象的 vector 中计算索引

c++ - 如何通过 Google 测试捕捉断言?