项目简介
GoogleTest 是成熟的 C++ xUnit 测试框架,支持自动测试发现、丰富断言、死亡测试、参数化测试及模拟功能。被众多大型开源项目广泛采用,帮助开发者灵活便捷地编写和运行测试。
README 预览
# GoogleTest\n\n### Announcements\n\n#### Documentation Updates\n\nOur documentation is now live on GitHub Pages at\nhttps://google.github.io/googletest/. We recommend browsing the documentation on\nGitHub Pages rather than directly in the repository.\n\n#### Release 1.18.0\n\n[Release 1.18.0](https://github.com/google/googletest/releases/tag/v1.18.0) is\nnow available.\n\nThe 1.18.x branch\n[requires at least C++17](https://opensource.google/documentation/policies/cplusplus-support#c_language_standard).\n\n#### Continuous Integration\n\nWe use Google's internal systems for continuous integration.\n\n#### Coming Soon\n\n* We are planning to take a dependency on\n [Abseil](https://github.com/abseil/abseil-cpp).\n\n## Welcome to **GoogleTest**, Google's C++ test framework!\n\nThis repository is a merger of the formerly separate GoogleTest and GoogleMock\nprojects. These were so closely related that it makes sense to maintain and\nrelease them together.\n\n### Getting Started\n\nSee the [GoogleTest User's Guide](https://google.github.io/googletest/) for\ndocumentation. We recommend starting with the\n[GoogleTest Primer](https://google.github.io/googletest/primer.html).\n\nMore information about building GoogleTest can be found at\n[googletest/README.md](googletest/README.md).\n\n## Features\n\n* xUnit test framework: \\n Googletest is based on the [xUnit](https://en.wikipedia.org/wiki/XUnit)\n testing framework, a popular architecture for unit testing.\n* Test discovery: \\n Googletest automatically discovers and runs your tests, eliminating the need\n to manually register your tests.\n* Rich set of assertions: \\n Googletest provides a variety of assertions, such as equality, inequality,\n exceptions, and more, making it easy to test your code.\n* User-defined assertions: \\n You can define your own assertions with Googletest, making it simple to\n write tests that are specific to your code.\n* Death tests: \\n Googletest supp
常见问题 (5)
故障排除为什么在 GoogleTest 中启用 --gtest_shuffle 时,UnitTest::GetTestSuite(i) 会返回错误的测试套件?
UnitTestImpl::GetTestSuite(int i) 从 test_suite_indices_ 计算出乱序后的位置,但却错误地使用原始的 i 来索引 test_suites_。在 googletest/src/gtest-internal-inl.h 中,将:
return test_suites_[i];
修改为:
return test_suites_[index];
mutable 访问器已经正确地使用了 index。应用补丁后,请重新构建 GoogleTest 并重新链接你的测试二进制文件。此 bug 自提交 bd851333 起就已存在,并在 main 分支的提交 08da6214 上被观察到。
故障排除为什么 GoogleTest 的 JSON 输出会将 UTF-8 属性值逐字节转义为 \u00XX,从而导致内容损坏?
这种情况发生在普通 char 为有符号类型的平台上。在 JsonUnitTestResultPrinter::EscapeJson 中,0x80 到 0xFF 范围内的 UTF-8 字节会变成负数,因此 ch < ' ' 检查会将它们当作 JSON 控制字符,并对每个字节单独进行转义。
修复方法是在进行控制字符检查之前,将该字节按无符号类型解释:
const unsigned char ch = static_cast<unsigned char>(str[i]);
if (ch < ' ') {
// 仅对真正的 ASCII 控制字符输出所需的 \u00XX 转义。
}
高位被置位的字节应保持原样,以便有效的 UTF-8 序列按 UTF-8 写入。继续转义 JSON 要求必须转义的字符,例如 " 和 \。然后重新构建 libgtest 和 libgtest_main,或者升级到包含 issue #5081 修复的 GoogleTest 版本。
作为临时变通方案,请避免在 RecordProperty、测试名称、文件路径和失败消息中使用非 ASCII 字符,或者对 JSON 进行后处理,以重建被错误拆分的 UTF-8 序列。
故障排除如何修复使用 CMake FetchContent 时 GoogleTest 出现 LNK2038 错误:'MTd_StaticDebug' 与 'MDd_DynamicDebug' 不匹配?
该错误表示 GoogleTest 是使用静态调试 C 运行时(/MTd)构建的,而你的可执行文件使用的是动态调试 C 运行时(/MDd)。
将 GoogleTest 升级到包含 issue #5042 修复的 main 提交。该修复仅在 GoogleTest 作为顶层项目时才会覆盖 CRT 设置,因此通过 FetchContent 和 add_subdirectory() 引入的使用者会继承父项目的 CRT 设置。
如果无法立即更新,请在声明 GoogleTest 之前强制其使用共享 CRT:
set(gtest_force_shared_crt ON CACHE BOOL "Use shared CRT for GoogleTest" FORCE)
include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest
GIT_TAG main
)
FetchContent_MakeAvailable(googletest)
然后删除构建目录并重新配置:
Remove-Item -Recurse -Force build
cmake -S . -B build
cmake --build build
或者,如果希望在所有地方统一使用静态 CRT,请在创建目标和添加 GoogleTest 之前设置:
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")故障排除如何修复在 Linux 或 Fedora 上构建 GoogleTest 时出现的 undefined reference to `regcompA` 错误?
当 MinGW 或 Cygwin 的 regex.h 头文件泄漏到 Linux 包含路径中时,就会出现此错误。它会通过宏将 regcomp、regexec 和 regfree 等 POSIX 函数重映射为 regcompA 之类的 Windows 风格名称,而这些名称在 glibc 中并不存在。CMake 和 GCC 的版本并不是问题的原因。
1. 检查是否存在泄漏的包含路径:
echo "$CPATH $C_INCLUDE_PATH $CPLUS_INCLUDE_PATH $CMAKE_INCLUDE_PATH"
echo | g++ -x c++ -E -Wp,-v -留意 MinGW、Cygwin、
w64-mingw32 或交叉编译器的包含目录。
2. 从环境中移除这些路径,并从头重新构建:
unset CPATH C_INCLUDE_PATH CPLUS_INCLUDE_PATH CMAKE_INCLUDE_PATH
rm -rf build
cmake -S . -B build
cmake --build build
如果无法立即移除错误的包含路径,可以强制 GoogleTest 使用其简易正则表达式实现,而不是 POSIX 正则表达式:
rm -rf build
cmake -S . -B build -DCMAKE_CXX_FLAGS='-DGTEST_HAS_POSIX_RE=0'
cmake --build build
要永久修复此问题,请将 GoogleTest 更新到包含 issue #5001 修复的较新版本或提交,该修复会检测被宏重定义的 POSIX 正则表达式符号,并自动回退到 SIMPLE_RE。
故障排除在使用 C++14 构建 GoogleTest 时,如何修复 [[maybe_unused]] 引发的 -Wc++17-attribute-extensions 警告?
GoogleTest 1.15.x 需要 C++17,因此请使用 C++17 而非 C++14 来构建它:
CXXFLAGS="-Werror -Wall -Wextra -Wpedantic" cmake -S . -B /tmp/gtestbuildfolder -DCMAKE_CXX_STANDARD=17 -DCMAKE_CXX_STANDARD_REQUIRED=ON
cmake --build /tmp/gtestbuildfolder
出现该警告是因为 Apple Clang 在 C++14 中将 [[maybe_unused]] 作为编译器扩展提供,尽管该属性是在 C++17 中才引入的。
如果必须在 C++14 中编译较旧版本的 GoogleTest,请修改 GTEST_INTERNAL_HAVE_CPP_ATTRIBUTE 宏,使其同时检查 __cplusplus:
#if defined(__has_cpp_attribute)
#define GTEST_INTERNAL_HAVE_CPP_ATTRIBUTE(x) (__cplusplus >= __has_cpp_attribute(x))
#else
#define GTEST_INTERNAL_HAVE_CPP_ATTRIBUTE(x) 0
#endif
首选的修复方法是使用 C++17 或升级 GoogleTest。