The local build-and-test loop: edit source, conan install resolves GoogleTest, cmake configures with Ninja, cmake builds with Clang under -Werror, ctest runs the GoogleTest cases, you observe the result, and a failing test sends you back to the source. The same loop runs in three lanes with separate build directories: default, asan-ubsan, and tsan.
Figure 1.1: The build-and-test loop every example runs, and the three lanes that share it.
Download .excalidraw source
·
Edit on excalidraw.com
The build files compose a running suite: conanfile.py provides GoogleTest (gtest_main and gmock), CMake links it with the shipping library and the test source into one unit_tests executable, gtest_discover_tests registers one CTest case per TEST, and ctest runs them and reports per case.
Figure 2.1: How the build files turn a library and a test file into a running suite.
Download .excalidraw source
·
Edit on excalidraw.com
The billable-weight rule: volumetric grams is computed from the dimensions as length times width times height divided by five, billable weight is the greater of volumetric and actual weight, and the three test cases each cover one outcome: a bulky parcel bills volumetric, a heavy parcel bills actual, and a tie bills either.
Figure 3.1: The billable-weight rule and the branch each test case covers.
Download .excalidraw source
·
Edit on excalidraw.com
Inside one test body running top to bottom, a failed ASSERT_EQ on the vector size stops the test immediately so the EXPECT_EQ lines below it never run, while a failed EXPECT_EQ records the failure and lets the test keep going to the next line.
Figure 4.1: A failed ASSERT stops the test; a failed EXPECT records and continues.
Download .excalidraw source
·
Edit on excalidraw.com
For every TEST_F, GoogleTest constructs a fresh ManifestTest, runs SetUp to add two parcels, runs the test body, then destroys the fixture so its vector is freed. The cycle repeats per test, each test getting its own instance, so no test sees another test's state.
Figure 5.1: GoogleTest runs the full fixture lifecycle once per TEST_F.
Download .excalidraw source
·
Edit on excalidraw.com
On the left, one TEST_P body is instantiated by INSTANTIATE_TEST_SUITE_P over five weight rows to produce five cases named w1 through w30001. On the right, one TYPED_TEST body is instantiated by TYPED_TEST_SUITE over the type list int, long, double to produce three cases per body. Both write the body once and let the suite generate the cases.
Figure 6.1: One body, many generated cases, by input row or by type.
Download .excalidraw source
·
Edit on excalidraw.com
Three rows map a situation to how it signals and how you test it. A recoverable input error returns a std::expected value, tested with has_value, the dereferenced value, and error. A broken contract throws std::invalid_argument with a message, tested with EXPECT_THROW and what. A programming bug asserts and aborts, tested with EXPECT_DEATH over a statement and a regex.
Figure 7.1: Three kinds of failure, each with its own signal and its own test.
Download .excalidraw source
·
Edit on excalidraw.com
EXPECT_DEATH forks. The child process runs the statement, its assert fires and calls abort, and it writes the message to stderr. The parent process waits for the child, and the test passes only if the child died and its stderr matched the regex; a clean exit is a failure.
Figure 7.2: A death test forks a child, lets it die, and checks how it died.
Download .excalidraw source
·
Edit on excalidraw.com
Three rows map a modern C++ feature to when it runs and how you test it. A concept that constrains a template is checked when the code compiles and is tested with static_assert and a constexpr bool. A lazy ranges pipeline of filter then transform runs only when fold_left pulls it, and is tested by materializing the result and comparing it. A generator coroutine yields values on demand and may never terminate, and is tested by collecting the yielded values or taking a prefix.
Figure 8.1: Three modern features, each with its own moment to run and its own way to test it.
Download .excalidraw source
·
Edit on excalidraw.com
An infinite running_total generator feeds views::take(3), which feeds the test's collected vector. The generator yields a value on demand; take pulls three and stops; the test compares the collected {500, 1000, 1500}. A note records that the loop body resumes three times and never a fourth, and that laziness is what makes the test terminate.
Figure 8.2: views::take pulls a fixed prefix, so the infinite generator's loop body runs only three times.
Download .excalidraw source
·
Edit on excalidraw.com
Dispatcher, the unit under test, holds a CarrierGateway reference and calls it. CarrierGateway is an abstract interface, the seam. A RealCarrier implements it for production and talks to the network; a MockCarrierGateway implements it for the test with a MOCK_METHOD per virtual. The test sets the mock's return values and observes how it is called.
Figure 9.1: An abstract collaborator is a seam: the real implementation serves production, a mock serves the test.
Download .excalidraw source
·
Edit on excalidraw.com
An EXPECT_CALL broken into parts: EXPECT_CALL(carrier, names which mock; quote_cents(Ge(0), _)) names the method and its argument matchers; .Times(1) is the cardinality; .WillOnce(Return(800)) is the action. A note says omitting Times and using WillOnce implies exactly one call.
Figure 9.2: The parts of an EXPECT_CALL: the mock, the method and its argument matchers, the cardinality, and the action.
Download .excalidraw source
·
Edit on excalidraw.com
Three mock strictness levels and how each treats a call no EXPECT_CALL covers. A NiceMock allows it silently. A default naggy mock allows it but logs a GMOCK WARNING. A StrictMock fails the test immediately. A matching EXPECT_CALL always takes priority on any of the three.
Figure 10.1: NiceMock, the default mock, and StrictMock differ only in how they treat a call no expectation covers.
Download .excalidraw source
·
Edit on excalidraw.com
Two columns contrasting a mock and a fake. A mock is set up with EXPECT_CALL expectations rather than behavior, asserts the interaction (which calls, which arguments, which order), and is verified when the mock is destroyed. A fake is a working implementation with real behavior kept simple, asserts the result by inspecting its state after the run, and is checked by an ordinary EXPECT_EQ afterward.
Figure 10.2: A mock verifies the interaction as it happens; a fake is a working stand-in whose state you inspect afterward.
Download .excalidraw source
·
Edit on excalidraw.com
A std::latch releases every worker at once. N worker threads each call record M times into a mutex-protected ShipmentLedger holding count and total. After the threads join, the test asserts count equals N times M and total equals N times M times the per-parcel weight.
Figure 11.1: A concurrent test releases N threads together, lets them contend, joins, and asserts the aggregate.
Download .excalidraw source
·
Edit on excalidraw.com
One test run exercises one interleaving of very many. That run can come out green because the schedule happened to work, while a race still hides in a schedule not sampled. ThreadSanitizer flags the race on any run, and repetition samples more schedules. A passing run narrows the doubt; the sanitizer and repetition close it.
Figure 11.2: One passing run samples a single schedule, so a race can hide; the sanitizer and repetition close the gap.
Download .excalidraw source
·
Edit on excalidraw.com
AddressSanitizer and UndefinedBehaviorSanitizer share one binary. ASan finds memory errors like use-after-free, buffer overflow, and leaks, and catches a dangling span here. UBSan finds undefined behavior like overflow, bad shifts, and misalignment, and catches a 32-bit sum overflow. ThreadSanitizer is a separate binary that finds data races and catches a shared accumulator. ASan and UBSan combine in one build; TSan cannot join them.
Figure 12.1: Three sanitizers, what each finds, and the bug each catches in this example.
Download .excalidraw source
·
Edit on excalidraw.com
A sanitizer report in four parts. First the error kind and faulting access: heap-use-after-free, READ of size 4. Then where the bad access happened: frame zero in total_weight in weights.cpp. Then where the memory was freed: the vector destructor in make_batch. Then where it was allocated: the vector constructor. Read the three stacks together.
Figure 12.2: An ASan report reads top to bottom: what went wrong, where, then the free and the allocation that frame it.
Download .excalidraw source
·
Edit on excalidraw.com
Four sources of a flaky test, each paired with the technique that makes it deterministic. Randomness from a live seed is fixed by injecting a seeded engine. Wall-clock time and sleeps are fixed by injecting the time and never sleeping. Thread scheduling is fixed by a latch, then repeating under ThreadSanitizer. Dependence on test order is fixed by running with gtest_shuffle.
Figure 13.1: Each source of flakiness has a determinism technique that removes it at the source.
Download .excalidraw source
·
Edit on excalidraw.com
A stress run with gtest_repeat and gtest_shuffle does N runs, each reordered. Most iterations pass on the common schedules; one iteration fails on a rare order or timing, and because the run prints its seed, that failure is now reproducible and gives a seed to debug from.
Figure 13.2: Repetition with shuffling turns a rare, order- or timing-dependent failure into a reproducible one.
Download .excalidraw source
·
Edit on excalidraw.com
The debug loop as four steps with a return path. Isolate the case with gtest_filter, break and run to stop in the function, inspect with print, next, and backtrace, then find the cause and fix. An arrow loops back from the end to the start labeled re-run until green. A note reads: a failing test is a lead, not a verdict.
Figure 14.1: The debug loop: isolate, stop, inspect, fix, and re-run until green.
Download .excalidraw source
·
Edit on excalidraw.com
A reference of GDB commands paired with what each does: break fn if cond stops in a function only when the condition holds; run with a gtest_filter starts just the failing test; print shows a variable or expression; next steps over a line and step descends into a call; finish runs to the end of the current function; backtrace shows the call stack.
Figure 14.2: The GDB commands that cover most test debugging.
Download .excalidraw source
·
Edit on excalidraw.com
The llvm-cov pipeline in four steps. Compile instrumented with -fprofile-instr-generate and -fcoverage-mapping, run the tests with LLVM_PROFILE_FILE set to produce a .profraw, merge it with llvm-profdata into a .profdata, then report line, region, and branch coverage with llvm-cov report or show.
Figure 15.1: Coverage is one build, one run, and one report: instrument, run, merge, report.
Download .excalidraw source
·
Edit on excalidraw.com
Two columns. Coverage tells you which lines and branches ran and where a whole branch went untested. Coverage cannot tell you whether the test asserted the result or whether an untested input breaks it. A note reads: 100% coverage with weak assertions still passes a broken program.
Figure 15.2: Coverage measures what ran, not what was checked.
Download .excalidraw source
·
Edit on excalidraw.com
A push or pull request triggers CI, which fans out into four parallel checks: the default lane building and running ctest, the asan-ubsan lane in one binary, the tsan lane in its own binary, and a coverage gate running llvm-cov. All four converge on an all-green state that allows the merge. A note says ASan and TSan never share a binary, so they are separate legs.
Figure 16.1: A push fans out into the default lane, the two sanitizer lanes, and a coverage gate; all must be green to merge.
Download .excalidraw source
·
Edit on excalidraw.com
ci.sh sits at the top, the four stages in order. It feeds two runners: a developer running ./ci.sh before pushing, and GitHub Actions running it on every push. Both rest on a pinned toolchain that gives the same result. A note reads: same stages, same order, same gate.
Figure 16.2: One script over a pinned toolchain runs the same locally and in CI, so a pass means the same thing in both.
Download .excalidraw source
·
Edit on excalidraw.com