[Go to site: main page, start]

Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Theory

Alternatively : you can watch the slides about the theory

TDD

TDD (Test Driven Development) is cool!

It makes sure we develop the right thing, step by step

TDD’s steps

  1. Write only 1 failing test
  2. Make all tests pass
  3. Refactor
  4. Repeat while it’s incomplete
  5. Until finish

Red step

  1. Write only 1 failing test
  2. Run tests
  3. If the tests pass then write another test
  4. Else if the tests fail then go to the green step

Green step

  1. Write the minimum code to make all tests pass
  2. Run tests
  3. If the tests fail then try something else
  4. Else if the tests pass then go to the refactor step

Refactor step

  1. Rewrite code without changing the behaviour
  2. Run tests
  3. If the tests fail then change something else and repeat the refactor step
  4. Else if the tests pass and
    1. there are another things to refactor then repeat the refactor step
    2. there is another feature to add then go to the red step
    3. there nothing else to do then it’s finished

TCR

TCR (test && commit || revert) is cool!

It encourages doing baby steps, reducing the waste when we are wrong

TCR’s single step

  1. Gambling on test results
  2. If the tests pass then commit
  3. If the tests fail then revert
  1. Change some code
  2. Repeat while it’s incomplete
  3. Until finish

But it doesn’t allow us to see the tests failing

So:

  • Maybe we test nothing (assert forgotten)

    def test_should_be_Buzz_given_5():
        input = 5
        actual = fizz_buzz(input)
        # Oops! Assert has been forgotten
    
  • Maybe we are testing something that is not the thing we should be testing

    it("should be Fizz given 3", () => {
      const input = 3;
      const actual = fizzBuzz(input);
      expect(input).toBe("Fizz");
      // Oops! Asserts on the input value instead of the actual value
    });
    

Detailed view

  1. Change some code
  2. Run tests
    test && commit || revert
  3. If the tests fail then
    1. Revert
    2. Try something else
    3. Repeat
  4. If the tests pass then
    1. Commit
    2. If there are another things to change then repeat
    3. If there is nothing else to do then it’s finished

TCRDD

TCRDD = TCR + TDD

TCRDD blends the constraints of the two methods to benefit from their advantages

Therefore, TCRDD makes sure we develop the right thing, step by step, and we are encouraged to do so by baby steps, reducing the waste when we are wrong

TCRDD’s phases

  1. Red phase
    1. Write only 1 failing test
    2. If the tests pass then
      1. Revert
      2. Retry the red phase
    3. Else if the tests fail then
      1. Commit
      2. Go to the green phase
  2. Green phase
    1. Make all tests pass
    2. If the tests fail then
      1. Revert
      2. Retry the green phase
    3. Else if the tests pass then
      1. Commit
      2. Go to the refactor phase
  3. Refactor phase
    1. Refactor
    2. If the tests fail then
      1. Revert
      2. Retry the refactor phase
    3. Else if the tests pass then
      1. Commit

Red phase

  1. Write only 1 test
  2. Gamble that the test fail
    git gamble --red
  3. Actually run tests
  4. If the tests pass then
    1. Revert
    2. Write another test
  5. Else if the tests fail then
    1. Commit
    2. Go to the green phase

Green phase

  1. Write the minimum code
  2. Gamble that the tests pass
    git gamble --green
  3. Actually run tests
  4. If the tests fail then
    1. Revert
    2. Try something else
  5. Else if the tests pass then
    1. Commit
    2. Go to the refactor phase

Refactor phase

  1. Rewrite code without changing the behaviour
  2. Gamble that the tests pass
    git gamble --refactor
  3. Actually run tests
  4. If the tests fail then
    1. Revert
    2. Change something else
  5. Else if the tests pass then
    1. Commit
    2. If there are another things to refactor then go to the refactor phase
    3. Else if there is another feature to add then go to the red phase
    4. Else if there is nothing else to do then it’s finished

git-gamble is a tool that helps to use the TCRDD method