Test Driven DevelopmentDriving Development by Tests
Haarlem Software Developer Meetup@mcbeelen
Confidential Page 2
KvK 53479335
Lichtfabriekplein 1, 2031 TE Haarlem
P.O. Box 2031 , 2002 CA Haarlem
+31 (0)88 248 1632, [email protected]
www.ximedes.com
Meetup Sponsor:
01-10-2015 Confidential Page 3
01-10-2015 Confidential Page 4
User StoryIn order to ….. achieve some goalAs a .... actorI want to ...... have certain functionality
Scenario: ExampleGiven….. some situationWhen.... perform actionThen...... verify correct behaviour
01-10-2015 Confidential Page 5
01-10-2015 Confidential Page 6
Once upon a time …
01-10-2015 Confidential Page 7
Controller
Validator Service
Collaborator
Converter
Acceptance Test
01-10-2015 Confidential Page 8
Controller
Validator Service
Collaborator
Converter
Acceptance Test
UnitTest
01-10-2015 Confidential Page 9
Controller
MockValidator
MockService
ObsoleteCollaborator
MockConverter
UnitTestLondon School of TDD
01-10-2015 Confidential Page 10
01-10-2015 Confidential Page 11
Three options to make a test pass:
1 2
Obvious Implementation
01-10-2015 Confidential Page 12
assertThat(calculate.multiply(2 * 5), is(10));
1 2multiply(x, y) { return x * y;}
multiply(x, y) { return 10;}
multiply(x, y) { return 2 * 5;}
multiply(x, y) { return x * y;}
01-10-2015 Confidential Page 13
01-10-2015 Confidential Page 14
Triangulation3
01-10-2015 Confidential Page 15
assertThat(calculate.multiply(2 * 5), is(10));3
multiply(x, y) { return 10;}
assertThat(calculate.multiply(2 * 7), is(14));
multiply(x, y) { return 2 * y;}
assertThat(calculate.multiply(3 * 7), is(21));
multiply(x, y) { return x * y;}
01-10-2015 Confidential Page 16
Baby steps TDD
http://www.tddfellow.com/blog/2016/10/19/do-more-with-baby-steps-tdd/
01-10-2015 Confidential Page 17
The Transformation Priority Premise
“As the tests get more specific, the code gets more generic.”
https://8thlight.com/blog/uncle-bob/2013/05/27/TheTransformationPriorityPremise.html
01-10-2015 Confidential Page 18
01-10-2015 Confidential Page 19
1. You are here to LEARN. (and have fun)
2. Try to do your best.
3. Work in pairs.Find a partner in a similar programming language and operating system
01-10-2015 Confidential Page 20
01-10-2015 Confidential Page 21
1 2
Primefactors:
Factorize a positive integer number into its prime factors.
Pokerhands:
Determine the winner given two pokerhands.
See you next time: March16thTest Driven Development
Refactoring Haarlem Software Developer Meetup