简体   繁体   English

获取 GoogleTest 运行的测试数/运行 0 次测试失败?

[英]Get number of tests ran by GoogleTest / fail on 0 tests run?

I'm using cmake's test runner to run several googletest test binaries.我正在使用 cmake 的测试运行程序来运行几个 googletest 测试二进制文件。 What I want to have happen is if a googletest test binary runs 0 tests via RUN_ALL_TESTS() , it fails.我想要发生的是,如果 googletest 测试二进制文件通过RUN_ALL_TESTS()运行 0 个测试,它会失败。 Currently, RUN_ALL_TESTS() returns success when it runs 0 tests.目前, RUN_ALL_TESTS()在运行 0 次测试时返回成功。 Alternatively, if I could somehow access the number of tests that RUN_ALL_TESTS() ran, I could override its success to be an error and bubble up the error instead.或者,如果我能以某种方式访问RUN_ALL_TESTS()运行的测试数量,我可以将其成功覆盖为错误并改为冒泡错误。

I've tried fixing this at the cmake level, with --no-tests=error .我尝试使用--no-tests=error在 cmake 级别修复此问题。 This doesn't work, however.然而,这不起作用。 I think the reason is the cmake sees itself running googletest test binaries, and those count as "running tests", even though the test binaries themselves don't have any tests.我认为原因是 cmake 认为自己正在运行 googletest 测试二进制文件,这些都算作“运行测试”,即使测试二进制文件本身没有任何测试。 Alternatively it could be because some of the googletest test binaries have tests, the --no-tests=error parameter isn't triggering.或者,可能是因为某些 googletest 测试二进制文件有测试, --no-tests=error参数没有触发。

I've looked into googletest, and none of the flags listed here seem to help https://google.github.io/googletest/advanced.html .我查看了googletest ,这里列出的所有标志似乎都没有帮助I don't see a way to set options, RUN_ALL_TESTS doesn't return any other information, and I don't see a function to get # of tests runs.我没有看到设置选项的方法, RUN_ALL_TESTS没有返回任何其他信息,也没有看到 function 来获取测试运行次数。

What options am I missing?.我错过了哪些选项?

To fail cmake test runner is easy. cmake 测试运行器很容易失败。

#include <gtest/gtest.h>

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  const int rv = RUN_ALL_TESTS();
  if (rv != 0)
    return rv;
#if 1
  if (::testing::UnitTest::GetInstance()->test_to_run_count() == 0)
    return 1;
#else
  if (::testing::UnitTest::GetInstance()->successful_test_count() == 0)
    return 1;
#endif
  return 0;
}

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

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