Favicon of GoogleTest

GoogleTest

GoogleTest is Google's C++ testing and mocking framework, merging GoogleTest and GoogleMock for unit testing and death tests.

GoogleTest website screenshot
GoogleTest GitHub repository preview

GoogleTest is Google's C++ test framework, combining the former GoogleTest and GoogleMock projects into a single library. It's built for C++ developers who need a structured way to write, organize, and run unit tests, including mocks for interfaces and dependencies. Large projects like Chromium, LLVM, Protocol Buffers, and OpenCV rely on it for their test suites.

Key features

  • xUnit architecture: Follows the familiar xUnit testing pattern used across many languages, so developers coming from JUnit or similar frameworks will recognize the structure.
  • Automatic test discovery: Tests register themselves; there's no manual list to maintain as your suite grows.
  • Rich assertion library: Covers equality, inequality, exceptions, floating-point comparisons, and more, with clear failure messages.
  • User-defined assertions: Write custom assertion macros tailored to your own types and invariants.
  • Death tests: Verify that code exits, crashes, or aborts under expected conditions, useful for testing error paths and defensive checks.
  • Fatal vs. non-fatal failures: Choose whether a failed check stops the current test immediately or lets it continue to report additional failures.
  • Value-parameterized tests: Run the same test logic against a set of input values without duplicating test code.
  • Type-parameterized tests: Run the same test logic across multiple data types, useful for testing templates and generic code.
  • Flexible test execution: Run individual tests, filter by name pattern, control ordering, and run tests in parallel (with tools like gtest-parallel).
  • Mocking support: Includes GoogleMock for creating mock objects to isolate code under test from its dependencies.

Ideal use cases

GoogleTest fits any C++ codebase that needs structured unit testing, from small libraries to large multi-million-line projects. It's a strong choice when you need mock objects to isolate components, when you're testing code with many input variations (value/type-parameterized tests), or when you need to verify crash/exit behavior with death tests. It integrates well with CI systems and CMake or Bazel builds, and has editor/IDE tooling (VS Code extensions, GUI runners) built around it.

It's not the right fit if you need a lightweight, header-only, dependency-free test setup for a tiny project. It requires linking against a real library and, as of the 1.18.x branch, requires at least C++17, so older C++ codebases stuck on earlier standards will need an older release or a different framework. If your project is not C++ (or C, via limited compatibility), look elsewhere; GoogleTest is not designed for other languages.

Installation

GoogleTest is typically built from source and linked into your test binaries. The most common approach with CMake:

git clone https://github.com/google/googletest.git
cd googletest
mkdir build
cd build
cmake ..
make

For use inside another CMake project, GoogleTest is commonly pulled in with FetchContent or add_subdirectory, then linked as gtest, gtest_main, gmock, or gmock_main targets. Check googletest/README.md in the repository for build-system-specific instructions (CMake, Bazel) and platform notes.

Detailed usage, from writing your first test to advanced features, is covered in the GoogleTest User's Guide, with the GoogleTest Primer as the recommended starting point for newcomers.

Categories:

Frequently asked questions

Share:

Stars
39.6K
Forks
10.9K
Last commit
4 days ago
Repository age
11 years
License
BSD-3-Clause
Self-hosted
No
Activity score
87/100
View Repository
Ad
Favicon

 

  
 

Similar to GoogleTest

Favicon

 

  
 
Favicon

 

  
 
Favicon