Test Driven Development

Jan 16, 2020·
Markus Hofbauer
Markus Hofbauer
· 1 min read

Test Driven Development

Code


Unit Tests

  • Separate (non production) program/code to test your code
  • Test your code on the lowest (unit) layer
  • General 3 step structure of a unit test
    • Given: Setup unit and environment
    • When: Execute unit to test
    • Then: Test for expected result

Why Tests

Michael Feathers, Working Effectively with Legacy Code

“To me, legacy code is simply code without tests.”


TDD

  • Write your test before your production code
  • 3 Phases
    • Red Phase: Define a failing test
    • Green Phase: Fix that test (Solve Simple)
    • Refactoring: Clean up your code

TDD Workflow

FizzBuzz Task

Write a function that returns the number it was given or Fizz if it is multiple of 3, Buzz if it is multiple of 5 or FizzBuzz if it is multiple of both.

C++ | Python


Task Definition

  1. Can call function fizzBuzz
  2. Return 1 for 1
  3. Return 2 for 2
  4. Return Fizz for 3
  5. Return Buzz for 5
  6. Return Fizz for 6
  7. Return Buzz for 10
  8. Return FizzBuzz for 15

Questions?

Try yourself