TDD 101
1. Test Driven Development
Test Driven Development
Test Driven
Development
It’s a discipline

Unit test first

Only code to pass the test

Loop of minimal changes
Classic post-test vs TDD
TestCodeDesign
DesignCodeTest
Waterfall
TDD
TDD laws
TDD laws
You can't write any
production code
until you have first
written a failing unit
test.
You can't write
more of a unit test
than is sufficient to
fail, and not
compiling is failing.
You can't write
more production
code than is
sufficient to pass
the currently failing
unit test.
Robert C. Martin
TDD cycle
TDD cycle
Write a
failing test
Write just
enough code
to make the
test pass
Refactor if
needed

(mostly
reduce
duplication)
Write a failing test
Write a test to
prove a simple case

Run the test and
see how it fails

Errors == test fails
Make the test pass
Write production
code until:

• all errors are fixed

• the test passes
Refactor
Keep tests passing

Remove duplication

Improve design
Outcomes
Outcomes Always backed by tests

Decoupled design

Automatically you get:

• full coverage

• regression tests
Code
Write a failing test
class ValidateNifTest extends TestCase
{
public function testRealNIFShouldBeValid()
{
$validateNif = new ValidateNif();
$this->assertTrue($validateNif->isValid('00000000T'));
}
}
Error : Class 'DojoValidateNifValidateNif' not found
/Users/franiglesias/PhpstormProjects/dojo/tests/Dojo/ValidateNif/ValidateNifTest.php:
13
Make the test pass
class ValidateNif
{
}
Error : Call to undefined method DojoValidateNifValidateNif::isValid()
/Users/franiglesias/PhpstormProjects/dojo/tests/Dojo/ValidateNif/ValidateNifTest.php:
14
Make the test pass
Failed asserting that null is true.
/Users/franiglesias/PhpstormProjects/dojo/tests/Dojo/ValidateNif/ValidateNifTest.php:
14
class ValidateNif
{
public function isValid(string $nif)
{
}
}
Make the test pass
OK (1 test, 1 assertion)
class ValidateNif
{
public function isValid(string $nif):bool
{
return true;
}
}
Hands on…
Fizz Buzz Kata
stage 1
Write a program that prints the numbers
from 1 to 100.

But for multiples of three print “Fizz”
instead of the number and for the
multiples of five print “Buzz”. 

For numbers which are multiples of both
three and five print “FizzBuzz “.
http://codingdojo.org/kata/FizzBuzz/
Fizz Buzz Kata

stage 2
A number is “Fizz" if it is divisible by 3 or
if it has a 3 in it

A number is “Buzz” if it is divisible by 5 or
if it has a 5 in it

http://codingdojo.org/kata/FizzBuzz/
Fizz Buzz Kata

stage 3
We want all these rules configurable.

http://codingdojo.org/kata/FizzBuzz/

Tdd red-green-refactor

  • 1.
    TDD 101 1. TestDriven Development
  • 2.
  • 3.
    Test Driven Development It’s adiscipline Unit test first Only code to pass the test Loop of minimal changes
  • 4.
    Classic post-test vsTDD TestCodeDesign DesignCodeTest Waterfall TDD
  • 5.
  • 6.
    TDD laws You can'twrite any production code until you have first written a failing unit test. You can't write more of a unit test than is sufficient to fail, and not compiling is failing. You can't write more production code than is sufficient to pass the currently failing unit test. Robert C. Martin
  • 7.
  • 8.
    TDD cycle Write a failingtest Write just enough code to make the test pass Refactor if needed
 (mostly reduce duplication)
  • 9.
    Write a failingtest Write a test to prove a simple case Run the test and see how it fails Errors == test fails
  • 10.
    Make the testpass Write production code until: • all errors are fixed • the test passes
  • 11.
    Refactor Keep tests passing Removeduplication Improve design
  • 12.
  • 13.
    Outcomes Always backedby tests Decoupled design Automatically you get: • full coverage • regression tests
  • 14.
  • 15.
    Write a failingtest class ValidateNifTest extends TestCase { public function testRealNIFShouldBeValid() { $validateNif = new ValidateNif(); $this->assertTrue($validateNif->isValid('00000000T')); } } Error : Class 'DojoValidateNifValidateNif' not found /Users/franiglesias/PhpstormProjects/dojo/tests/Dojo/ValidateNif/ValidateNifTest.php: 13
  • 16.
    Make the testpass class ValidateNif { } Error : Call to undefined method DojoValidateNifValidateNif::isValid() /Users/franiglesias/PhpstormProjects/dojo/tests/Dojo/ValidateNif/ValidateNifTest.php: 14
  • 17.
    Make the testpass Failed asserting that null is true. /Users/franiglesias/PhpstormProjects/dojo/tests/Dojo/ValidateNif/ValidateNifTest.php: 14 class ValidateNif { public function isValid(string $nif) { } }
  • 18.
    Make the testpass OK (1 test, 1 assertion) class ValidateNif { public function isValid(string $nif):bool { return true; } }
  • 19.
  • 20.
    Fizz Buzz Kata stage1 Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz “. http://codingdojo.org/kata/FizzBuzz/
  • 21.
    Fizz Buzz Kata
 stage2 A number is “Fizz" if it is divisible by 3 or if it has a 3 in it A number is “Buzz” if it is divisible by 5 or if it has a 5 in it http://codingdojo.org/kata/FizzBuzz/
  • 22.
    Fizz Buzz Kata
 stage3 We want all these rules configurable. http://codingdojo.org/kata/FizzBuzz/