Cette page est affichée en anglais. Une traduction française est en cours.
ScriptsApr 27, 2026·3 min de lecture

GoogleTest — C++ Testing Framework by Google

GoogleTest is Google's open-source C++ testing and mocking framework used across thousands of production codebases.

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

Fil de discussion

Connectez-vous pour rejoindre la discussion.
Aucun commentaire pour l'instant. Soyez le premier à partager votre avis.

Actifs similaires