C++ Test Automation and Coverage

GoogleTest and GoogleMock, Catch2 and doctest, CTest as the runner, and coverage with gcov/lcov and llvm-cov. Covers test tiering, flake hunting, and coverage gates that are not trivially gamed.

Overview

Failure modes other ecosystems mostly lack

Testing C++ goes wrong in ways that Python or Java teams rarely see, and each one has a structural cause.

  • Tests have a build time as well as a run time: tests are compiled artifacts, and in a large repo the build often dominates.
  • Global state leaks between tests: tests share a process.
  • A test can crash the runner: when it does, it takes every other test in the binary with it.
  • Coverage numbers are subtly wrong: instrumentation interacts with optimization.

A C++ test stack answers these problems in four layers, and this chapter takes them in order.

Key concepts

  • TEST_F fixture lifecycle
  • ASSERT_ vs EXPECT_
  • TEST_P and INSTANTIATE_TEST_SUITE_P
  • gtest_discover_tests
  • RESOURCE_LOCK
  • ctest --repeat until-fail:N
  • gcov vs source-based coverage
  • Line vs branch vs MC/DC coverage

Interview questions

  • Design the CMake and CTest layout for a repo with 4000 fast unit tests and 60 integration tests that need a real database and a fixed port.
  • Explain line, branch and MC/DC coverage, and why branch coverage matters more in C++ specifically.
  • A test passes when run alone and fails under ctest -j16. Walk me through diagnosing it.
  • When would you use TEST_P versus TYPED_TEST versus a plain loop inside one TEST, and what do you give up with the loop?
  • How do you enforce a coverage gate that actually improves quality instead of getting gamed?

Sign up free to read the full topic

More DevOps topics

Start free on Cariara · Coding problems · System design studio