Slide 1

Slide 1 text

TDD 101 1. Test Driven Development

Slide 2

Slide 2 text

Test Driven Development

Slide 3

Slide 3 text

Test Driven Development It’s a discipline Unit test first Only code to pass the test Loop of minimal changes

Slide 4

Slide 4 text

Classic post-test vs TDD Test Code Design Design Code Test Waterfall TDD

Slide 5

Slide 5 text

TDD laws

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

TDD cycle

Slide 8

Slide 8 text

TDD cycle Write a failing test Write just enough code to make the test pass Refactor if needed
 (mostly reduce duplication)

Slide 9

Slide 9 text

Write a failing test Write a test to prove a simple case Run the test and see how it fails Errors == test fails

Slide 10

Slide 10 text

Make the test pass Write production code until: • all errors are fixed • the test passes

Slide 11

Slide 11 text

Refactor Keep tests passing Remove duplication Improve design

Slide 12

Slide 12 text

Outcomes

Slide 13

Slide 13 text

Outcomes Always backed by tests Decoupled design Automatically you get: • full coverage • regression tests

Slide 14

Slide 14 text

Code

Slide 15

Slide 15 text

Write a failing test class ValidateNifTest extends TestCase { public function testRealNIFShouldBeValid() { $validateNif = new ValidateNif(); $this->assertTrue($validateNif->isValid('00000000T')); } } Error : Class 'Dojo\ValidateNif\ValidateNif' not found /Users/franiglesias/PhpstormProjects/dojo/tests/Dojo/ValidateNif/ValidateNifTest.php: 13

Slide 16

Slide 16 text

Make the test pass class ValidateNif { } Error : Call to undefined method Dojo\ValidateNif\ValidateNif::isValid() /Users/franiglesias/PhpstormProjects/dojo/tests/Dojo/ValidateNif/ValidateNifTest.php: 14

Slide 17

Slide 17 text

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) { } }

Slide 18

Slide 18 text

Make the test pass OK (1 test, 1 assertion) class ValidateNif { public function isValid(string $nif):bool { return true; } }

Slide 19

Slide 19 text

Hands on…

Slide 20

Slide 20 text

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/

Slide 21

Slide 21 text

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/

Slide 22

Slide 22 text

Fizz Buzz Kata
 stage 3 We want all these rules configurable. http://codingdojo.org/kata/FizzBuzz/