[错误]: 当测试名称包含 ':' 时,线程安全的死亡测试在不运行的情况下也会通过(例如,命名为"std::string"的字符串测试)
作者: cindykrafft创建于 2026年9月23日更新于 2026年9月24日
#include <string>
#include <unistd.h>
#include "gtest/gtest.h"
template<typename T> class TT : public ::testing::Test {};
struct Names {
template<typename T> static std::string GetName(int) {
return std::is_same<T, std::string>::value ? "std::string" : "int";
}
};
using Types = ::testing::Types<int, std::string>;
TYPED_TEST_SUITE(TT, Types, Names);
TYPED_TEST(TT, DoesNotExit) {
// Does not exit, so this should fail with "failed to die".
EXPECT_EXIT({ fprintf(stderr, "child ran statement\n"); },
::testing::ExitedWithCode(0), "");
}
TYPED_TEST(TT, Exits1) {
EXPECT_EXIT(_exit(1), ::testing::ExitedWithCode(1), "");
}
int main(int argc, char** argv) {
::testing::InitGoogleTest(&argc, argv);
GTEST_FLAG_SET(death_test_style, "threadsafe");
return RUN_ALL_TESTS();
}Built with g++ -std=c++17 repro.cc -lgtest -pthread. Output (trimmed):
[ RUN ] TT/int.DoesNotExit
Result: failed to die.
[ FAILED ] TT/int.DoesNotExit, where TypeParam = int
[ RUN ] TT/int.Exits1
[ OK ] TT/int.Exits1
[ RUN ] TT/std::string.DoesNotExit
WARNING: filter "TT/std::string.DoesNotExit" did not match any test; no tests were run
[ OK ] TT/std::string.DoesNotExit
[ RUN ] TT/std::string.Exits1
WARNING: filter "TT/std::string.Exits1" did not match any test; no tests
…内容来源: google/googletest