# GoogleTest — C++ Testing Framework by Google > GoogleTest is Google's open-source C++ testing and mocking framework used across thousands of production codebases. ## Install Save as a script file and run: # GoogleTest — C++ Testing Framework by Google ## Quick Use ```bash # CMake FetchContent (recommended) include(FetchContent) FetchContent_Declare(googletest URL https://github.com/google/googletest/archive/refs/heads/main.zip) FetchContent_MakeAvailable(googletest) # Link in CMakeLists.txt target_link_libraries(my_test GTest::gtest_main) # Run tests ctest --test-dir build --output-on-failure ``` ## Introduction GoogleTest (also known as gtest) is the C++ testing framework developed and maintained by Google. It provides a rich set of assertions, test fixtures, and death tests that scale from small unit tests to large integration suites. The framework runs on Linux, macOS, Windows, and several embedded platforms. ## What GoogleTest Does - Provides `EXPECT_*` and `ASSERT_*` macros for value, string, floating-point, and exception checks - Supports test fixtures (`TEST_F`) for shared setup and teardown logic - Includes Google Mock for creating mock objects with `MOCK_METHOD` - Runs parameterized tests via `TEST_P` for data-driven testing - Generates XML/JSON reports compatible with CI dashboards ## Architecture Overview GoogleTest compiles as a static library that links into your test binary. Each `TEST` or `TEST_F` macro registers a test case in a global registry at static-init time. The test runner iterates that registry, executing each test in its own fixture instance so state does not leak. Google Mock layers on top, using template metaprogramming to generate expectation matchers and action stubs at compile time. ## Self-Hosting & Configuration - Add via CMake FetchContent, git submodule, or system package (`libgtest-dev`) - Set `GTEST_COLOR=1` for colored terminal output - Use `--gtest_filter=Suite.Case` to run specific tests - Enable `--gtest_shuffle` to randomize test order and catch hidden dependencies - Integrate with CTest or Bazel for parallel execution ## Key Features - Assertion macros that print rich diagnostics on failure without extra work - Death tests (`EXPECT_DEATH`) verify that code exits or aborts as expected - Type-parameterized tests exercise templates over multiple types in one definition - Google Mock provides matchers, actions, and cardinality checks for mock objects - Supports running on bare-metal and RTOS targets with minimal porting ## Comparison with Similar Tools - **Catch2** — header-only with natural BDD syntax; GoogleTest offers richer mocking via Google Mock - **doctest** — faster compile times and lighter headers; GoogleTest has broader CI and IDE support - **Boost.Test** — part of the Boost ecosystem; GoogleTest is self-contained with a simpler API - **CppUnit** — older xUnit-style framework; GoogleTest provides better diagnostics and parameterized tests - **CTest** — a test runner, not a framework; often used alongside GoogleTest ## FAQ **Q: Does GoogleTest support C or only C++?** A: GoogleTest targets C++14 and later. For pure C code, wrap calls in `extern "C"` blocks inside C++ test files. **Q: Can I use Google Mock without GoogleTest?** A: Google Mock ships as part of the GoogleTest repository and depends on its assertion macros, so they are used together. **Q: How do I integrate GoogleTest with Bazel?** A: Add `@com_google_googletest` as a dependency in your WORKSPACE and use `cc_test` rules that link `gtest_main`. **Q: Is GoogleTest thread-safe?** A: Assertions are thread-safe on platforms with pthreads or C++11 threads, though tests should avoid concurrent writes to shared fixtures. ## Sources - https://github.com/google/googletest - https://google.github.io/googletest/ --- Source: https://tokrepo.com/en/workflows/b01948da-4277-11f1-9bc6-00163e2b0d79 Author: Script Depot