Unit Test and TDD Fundamentals

TDD results in unit tests. A unit test verifies the behavior of a code unit, where a unit is the smallest testable piece of an application. You typically write a unit test in the programming language of the unit.

Unit Test Organization and Execution

A single unit test consists of a descriptive name and a series of code statements, conceptually divided into four (ordered) parts.

  1. (Optional) statements that set up a context for execution

  2. One or more statements to invoke the behavior you want to verify

  3. One or more statements to verify the expected outcome

  4. (Optional) cleanup statements (for example, to release allocated memory)

Some folks refer to the first three parts as Given-When-Then. In other words, Given a context, ...

Get Modern C++ Programming with Test-Driven Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.