Test-Driven Development (TDD)

Test-driven development (TDD) is a proven software development process. It turns out that many more bugs get caught when tests are written alongside code.

title: TDD Mantra
Don’t write any production code unless you have a failing test.

“Testing shows the presence, not the absence of bugs” - Dijkstra

Some Testing Ideas / Topics

TDD Cycle

You first write failing tests before writing the program itself.

Steps

  1. Write the simplest possible test case (that the program doesn’t yet solve) → Test Fails
  2. Write the simplest possible program until the test passes. Some sub-steps for this in C++:
    • G++: Compile the code
    • gtest: Run the test harness (ex: ./run_gtest.sh)
    • gcov: Confirm that the test suite provides 100% coverage
    • Valgrind: check for memory safety
    • Git: make commits
  3. Important step that is often overlooked: Refactor and tidy up code.
  4. Repeat until you have written passing test for all required functionalities

Benefits of TDD

  • Reduce debugging effort
  • Work incrementally. Stay focused on manageable tasks
  • Always have a working version of the program. git restore file.cpp to roll back to the working version