개요
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
FAQ (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를 다시 빌드하고 테스트 바이너리를 다시 링크하세요. 이 버그는 커밋 bd851333부터 존재했으며, main의 커밋 08da6214에서 관찰되었습니다.
문제 해결왜 GoogleTest JSON 출력은 각 바이트를 \u00XX로 이스케이프하여 UTF-8 속성 값을 손상시키나요?
이 문제는 기본 char가 signed인 플랫폼에서 발생합니다. JsonUnitTestResultPrinter::EscapeJson에서 0x80부터 0xFF까지의 UTF-8 바이트가 음수가 되므로, ch < ' ' 검사가 이 바이트들을 JSON 제어 문자로 취급하여 각 바이트를 개별적으로 이스케이프합니다.
제어 문자 검사 전에 바이트를 unsigned로 해석하여 이 문제를 수정하세요:
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를 빌드할 때 발생하는 `regcompA` undefined reference 오류를 해결하는 방법
이 오류는 MinGW 또는 Cygwin regex.h 헤더가 Linux include 경로로 유입될 때 발생합니다. 이 헤더는 regcomp, regexec, regfree와 같은 POSIX 함수를 glibc에는 존재하지 않는 regcompA와 같은 Windows 스타일 이름으로 매크로 재매핑합니다. CMake 및 GCC 버전은 원인이 아닙니다.
1. 유입된 include 경로 확인:
echo "$CPATH $C_INCLUDE_PATH $CPLUS_INCLUDE_PATH $CMAKE_INCLUDE_PATH"
echo | g++ -x c++ -E -Wp,-v -MinGW, Cygwin,
w64-mingw32 또는 크로스 컴파일러 include 디렉터리를 찾습니다.
2. 환경에서 해당 경로를 제거하고 처음부터 다시 빌드:
unset CPATH C_INCLUDE_PATH CPLUS_INCLUDE_PATH CMAKE_INCLUDE_PATH
rm -rf build
cmake -S . -B build
cmake --build build
문제가 되는 include 경로를 즉시 제거할 수 없는 경우, GoogleTest가 POSIX regex 대신 자체 simple regex 구현을 사용하도록 강제합니다:
rm -rf build
cmake -S . -B build -DCMAKE_CXX_FLAGS='-DGTEST_HAS_POSIX_RE=0'
cmake --build build
영구적으로 해결하려면, 매크로로 재정의된 POSIX regex 심볼을 감지하여 자동으로 SIMPLE_RE로 폴백하는 issue #5001 수정이 포함된 최신 릴리스 또는 커밋으로 GoogleTest를 업데이트합니다.
문제 해결C++14에서 GoogleTest를 빌드할 때 [[maybe_unused]]에 대한 -Wc++17-attribute-extensions 경고를 어떻게 수정하나요?
GoogleTest 1.15.x는 C++17을 요구하므로, C++14 대신 C++17로 빌드하세요:
CXXFLAGS="-Werror -Wall -Wextra -Wpedantic" cmake -S . -B /tmp/gtestbuildfolder -DCMAKE_CXX_STANDARD=17 -DCMAKE_CXX_STANDARD_REQUIRED=ON
cmake --build /tmp/gtestbuildfolder
이 경고는 [[maybe_unused]] 속성이 C++17에서 도입되었음에도 불구하고, Apple Clang이 이를 C++14에서 컴파일러 확장으로 노출하기 때문에 발생합니다.
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를 업그레이드하는 것입니다.