Chapters

The whole tutorial, in order.

Every part and chapter, linked. Read it top to bottom the first time through; each chapter also stands on its own. New here? Start with the Outline.

Part 0 Prerequisites

Set up a Fedora 44 C++ toolchain and prove it works before writing a single test.

  1. 1 Setup Install and prove the exact Fedora 44 toolchain every example in this book builds and tests with: Clang, CMake, Ninja, Conan, GDB, and GoogleTest via Conan.

Part 1 Foundations

From an empty directory to a running GoogleTest suite: CMake presets, CTest, assertions, and fixtures.

  1. 2 Why test C++, and the toolchain What a test actually buys you, and how the build files turn a library and a test file into a running GoogleTest suite: the conanfile, the CMake test target, gtest_discover_tests, and CTest.
  2. 3 Your first test Write a GoogleTest suite from an empty file: the anatomy of a TEST, choosing cases that cover the branches, and how CMake presets, CTest, and gtest_discover_tests run them.
  3. 4 Assertions and expectations The difference between a fatal ASSERT and a non-fatal EXPECT, the assertion vocabulary GoogleTest gives you, and how to organize cases into suites.
  4. 5 Fixtures Share prepared state across a group of tests with TEST_F, SetUp, and a fixture class, and rely on per-test construction and RAII to keep each case isolated.

Part 2 Beyond the basics

Parameterized and typed tests, death tests, and testing modern C++20/23 features.

  1. 6 Parameterized and typed tests Run one test body over many inputs with TEST_P, and over many types with TYPED_TEST, instead of copying a case and changing one value.
  2. 7 Death tests, exceptions, and std::expected Test the failure paths: a recoverable error returned as a std::expected, a contract violation thrown as an exception, and a programming bug that aborts, checked with EXPECT_DEATH.
  3. 8 Testing modern C++20 and C++23 Test the features that define modern C++: a concept checked at compile time, a lazy ranges pipeline materialized to a value, and a generator coroutine whose yields you collect, including an infinite one made safe by laziness.

Part 3 Mocking

GoogleMock from first principles: mocks, matchers, actions, and designing code that can be mocked.

  1. 9 GoogleMock fundamentals Replace a real collaborator with a mock so you can test a unit in isolation: declare a mock with MOCK_METHOD, set expectations with EXPECT_CALL, match arguments, assert how many times a call happens, and stub background interactions with ON_CALL.
  2. 10 Advanced mocking and testability Go beyond the first mock: design code with seams you can inject, control a mock's strictness with NiceMock and StrictMock, assert call ordering with InSequence, capture arguments with SaveArg, and know when a fake beats a mock.

Part 4 Testing concurrency

Testing threaded code, catching real races with sanitizers, and hunting flaky tests.

  1. 11 Testing threaded code Write deterministic tests for concurrent code: launch threads with std::jthread, release them together with a latch to force overlap, synchronize with std::async and futures, and assert the aggregate that must hold no matter how the threads interleave.
  2. 12 Races and memory bugs Read what the sanitizers report. Run the suite under AddressSanitizer, UndefinedBehaviorSanitizer, and ThreadSanitizer, and see each one catch a bug written to trip it: a dangling view, a signed overflow, and a data race.
  3. 13 Deterministic and stress testing Kill flakiness at the source: inject the clock and the random engine so a test controls time and chance, then stress the suite with gtest_repeat and gtest_shuffle to flush out the rare failures that remain.

Part 5 Tooling and workflow

Debugging failing tests, measuring coverage, and running the suite in CI.

  1. 14 Debugging a failing test with GDB Turn a red test into a located bug: run the test binary under GDB, isolate the case with a filter, stop in the function with a conditional breakpoint, and read the state with print, step, and backtrace.
  2. 15 Measuring coverage with llvm-cov See which lines and branches the suite actually runs: build instrumented with Clang's source-based coverage, report with llvm-cov, read line, region, and branch numbers, and know what coverage can and cannot tell you.
  3. 16 CI and reproducible test runs Wire the whole suite into one pipeline: a script that runs the default lane, both sanitizer lanes, and a coverage gate, and the GitHub Actions workflow that runs the same stages on every push, so local and CI results mean the same thing.