Have you heard of TDD? Are you interested or familiar with this practice but have never been able to understand it?
In this presentation we will see the benefits of Test-Driven Development (TDD), understand how it works and what the benefits are of using it. We will see in a more detailed approach this way of developing software, where our software is always built guided by tests.
We will go over some history about TDD, which is the main process we must follow when we work with this mechanic and the rules that surround it. We will also list the main advantages and disadvantages that most developers who practice TDD find and whether the arguments in favour add up to more than those that subtract. Finally, we will review some good habits and practices when applying TDD and see how to do it step by step with an example of "live" coding session with Java.
At the end of the session, we hope that you will have a wider understanding of what TDD is, what advantages it brings, why it is interesting to master it and also that you will take with you some tricks and good practices to be able to apply them in your day-to-day life when writing code
3. 3Confidential
Whythis session?
• Understand the basics around TDD
• Be able building a software starting with tests
• Understand the benefits of building code
covered by tests
VS
building the tests after we write our code
(before VS after)
• Code a bit (and probably) learn something
new from your colleagues
• … so do not run away please! 😉
4. Confidential 4
NachoCougil
• Senior Software Engineerat Dynatrace
• TDD &cleancode fan
• Started to write in Javabefore the effect 2000
• Founderof the Barcelona Java Users Group &
co-founderof the Java and JVMBarcelona Conference (JBCNConf)
• Father, formermountain marathonrunner 😅
@icougil
WhoamI?
7. 7Confidential
• Whereit camesfrom?
• Advantages & disadvantages
• Process &rules
• Good habits
• Questions?
• Demo
• Pair programming
• Kata
• Final Recap
Agenda
8. 8Confidential
• Where it cames from?
• Advantages & disadvantages
• Process &rules
• Good habits
• Questions?
• Demo
• Pair programming
• Kata
• Final Recap
Agenda
9. Confidential 9
• Kent Beck “rediscovered” TDD whenwriting first
testing framework SUnitin1994.
“Taketheinputtape,manuallytypeintheoutput
tapeyouexpect, then programuntiltheactual
outputtapematches the expected output.”
• TDD became part of Extreme Programmingbook
in1999.
Abit of history
10. Confidential 10
• … youcan find defects earlier ⏱ (when runningor
designing yourtests)
• … youcan easily detect regression errors 🔴
• … youfollow a simple process ⚙️ that helps youto
develop yoursoftware in a straightforward way
• … yoursoftware is going to be easier to refactor
because of ✅ green (safety net)
For a moment,imagine…
11. Confidential 11
For a moment,imagine…
• ... yoursoftware guides youhow a consumerwill use your
component ⏛
• … tests are living documentation (really 🖖 !!)
• … the software youwrite it is likely to have less bugs 🐛
• … yourdevelopment costs 💰 will be lower
References:
- Test-drivendevelopmentasa defect-reductionpractice(IEEE)
- GuestEditors' Introduction:TDD--TheArt of FearlessProgramming (computer.org)
12. Confidential 12
Yes,TDDhassomedisadvantages
• It is not so easy to start with 😅
(not manygood places to help, majority of people are not using it
daily)
• Has a highlearningcurve 😟
(depends directly on thecomplexity of yourcode + yourdesign
and engineeringcapabilities)
• Can be a largeinvestment 💸 in a team (depending on the
experience of the members + requires time ⏳ &effort 🙇♀️🙇♂️)
13. Confidential 13
• It is reallyeasy to forget 😓 about it
(especiallyifyou arenot inan environment/ teamthat
does not encourageits use or you cannot experiment
withitcomfortably)
• It provokesresistance☹️ (in somepeople)
• Can be corrupted and lead to the syndromeof reaching
the highest possible levelof coverage 🙈
• It is extremelydifficult 😤 to masterat it
Yes,TDDhassomedisadvantages
14. Confidential 14
So,… how it is?
• Youlearnedhow to write code time ago,…
• …and now you’dmay learn a different way of writing software.
Like“learningto rideaverydifferent bike”,... but being older 😅
18. Confidential 18
Focus on building tests first that will help us demonstrate the
behaviour we want.
Wewill start writing tests and we can’t writeverymuchapart
from a unittest. Tests are first class citizens
No overengineering, simple code, simple solution: makethe test
pass
• You are not allowed to writeany production code unless it is to
makea failing unit test pass.
• Youare not allowed to write any more of a unit test than is
sufficient to fail.
• Youare not allowed to write any
more production code than is
sufficient topass the one failing
unit test.
Robert C. Martin
(uncleBob)
Therules
19. Confidential 19
• Whereit camesfrom?
• Advantages & disadvantages
• Process &rules
• Good habits
• Questions?
• Demo
• Pair programming
• Kata
• Final Recap
Agenda
20. Confidential 20
• Before you write production code,
check that your test is failing 🔴!
• Each test has only 1 reason to fail
• Write the assertion first
Goodhabits
21. Confidential 21
Tests namingconvention
Describe the
expected
behaviour, not
the internals.
Our tests
should describe
behaviour in
plain english
Ending the class name
with Should will “force “
you to start describing
an action for every test
you may create
22. Confidential 22
• Ourtests methods will only describe behavior.
Thereforewewill havea better understanding on
what this class does
• Ourtest will be moreclear
• If some tests fails, we can havea look and see
easily which case is failing
• Wedon’t need to get into details on anyparticular
test if wedon’t need it
Theresult will befocusedonthe business
23. Confidential 23
Test creation order
1) Name the class
2) Name the method
3) Define what you want to check
4) Trigger the code
5) Do the setup
24. Confidential 24
• Startlittleby little...
• Katas
• Proofs of concept
• Personal projects
• Practicewithsomebody else (pair-programming)
• Practice...
• ... and continues to practice
• ... and continues
• ... until you finally internalizeit 🖖♀️
• Checkonline content
But, howto start?
25. Confidential 25
• Whereit camesfrom?
• Advantages & disadvantages
• Process &rules
• Good habits
• Questions?
• Demo
• Pair programming
• Kata
• Final Recap
Agenda
29. Confidential 29
Demo/ Kata 1:FizzBuzz
• https://kata-log.rocks/fizz-buzz-kata • Write a program that prints one linefor each numberfrom 1 to
100
• For multiples of 3 print Fizzinstead of the number
• For the multiples of 5 print Buzzinstead of thenumber
• For numberswhich are multiples of both 3 and 5
print FizzBuzzinstead of thenumber
30. Confidential 30
• Whereit camesfrom?
• Advantages & disadvantages
• Process &rules
• Good habits
• Questions?
• Demo
• Pair programming
• Kata
• Final Recap
Agenda
31. Confidential 31
• An ExtremeProgramming(XP)Practicein which2 developersparticipate inone
developmenteffort at one workstation.
• One,the driver,writescode whilethe other, the navigator orobserver,reviewsthe
code as it istyped in. The 2engineersswitchrolesfrequently.
• While reviewing,the observeralso considers the "strategic"direction of the work,
coming up with ideas for improvementsand likelyfuture problemsto address.This is
intended to freethe driverto focus all of their attention on the"tactical" aspectsof
completingthe currenttask, using theobserveras a safety netand guide.
Pair programming
32. Confidential 32
• Theprocess is:
1. One person (A) writes a newtest and verifies that it fails 🔴
2. Theother developer (B) implements the code needed to pass the test ✅
3. (optional) -Theother developer (B) decides to refactor part of the code
4. Theother developer (B) writes thenext test and verifies that it fails 🔴
5. Thefirst developer (A) implements thecode needed to pass thetest ✅
Ping-pongpairprogramming
33. Confidential 33
Thinkaboutcraftmanship
• Be patience, don’t rush.
• Tryto learn as muchas youcan from your colleague
• No pair pressure -> negotiation for the win!
• Write code youwill be proud of 😃👍
37. Confidential 37
Kata:Leap year
• As a user, I want to know if a yearis a leap yearor not. • All yearsdivisible by400are leap years (so, for example, 2000
was indeed a leap year),
• All yearsdivisible by100but not by 400 are NOTleap years (so,
for example, 1700,1800,and 1900were not leap years, NORwill
2100bea leap year),
• All yearsdivisible by 4but not by100are leap years (e.g., 2008,
2012,2016),
• All yearsnot divisible by4 are NOT leap years (e.g. 2017,2018,
2019).
38. Confidential 38
Kata:String calculator
• Createa simple String calculator with a method signature:
int Add(string numbers)
• Themethod can takeup to two numbers, separated by commas,
and will returntheir sum.
Examples:
• “” or “1” or “1,2” as inputs.
• For an empty string it will return0.
40. Confidential 40
Kata:String calculator
• Iteration 2
Allow theAdd method to handle newlinesbetween numbers
(instead of commas).
Examples:
• Thefollowing input is ok: “1n2,3”(will equal 6)
• Thefollowing input is NOTok: “1,n” (not need to proveit -just
clarifying)
41. Confidential 41
Kata:String calculator
• Iteration 3
Support different delimiters:
• To change adelimiter, the beginning ofthe string will contain a
separate line that looks like this: “//[delimiter]n[numbers…]” for
example “//;n1;2” should return three where the default delimiter
is‘;’.
• The firstline is optional. All existing scenarios should still be
supported.
42. Confidential 42
Kata:String calculator
• Iteration 4
Calling Add with a negative numberwill throw an
exception “negatives not allowed” -and the negative that was
passed.
• If there are multiple negatives, show all of them in the exception
message.
43. Confidential 43
Kata:String calculator
• Iteration 5
Numbersbigger than 1000should be ignored.
• Ifyou want more,you can playmore!See: https://kata-
log.rocks/string-calculator-kata
Example:
• 2+ 1001= 2
44. Confidential 44
• Whereit camesfrom?
• Advantages & disadvantages
• Process &rules
• Good habits
• Questions?
• Demo
• Pair programming
• Kata
• Final Recap
Agenda
45. Confidential 45
• Martin Fowler (main concepts around testing)
• https://www.martinfowler.com/bliki/TestDouble.html
• https://martinfowler.com/articles/mocksArentStubs.html
• https://martinfowler.com/articles/practical-test-
pyramid.html
• JamesShore(JS practices)
• https://www.youtube.com/user/jdlshore/videos
• Jason Gorman(Java practices)
• https://www.youtube.com/user/parlezuml/videos
Recommendedcontent
46. Confidential 46
• TDD helpsyouto develop yourcode in a simple and effective way
👌
• It is difficult to adapt to its mechanicsand sometimes is difficult 😅
to maintainits practice...
• Itworth a try 😉 ( yoursoftware will haveless bugs 🐛 & it will be
easier to maintain 💰 )
• Thereare some tips 💡
( could makeyourlife easier )
• Tryto pair, it will help 😃 you (a lot )
• Practice, practice &practice 🖖 again
FinalRecap
Around 2hs:
Theorical content - 35mins
1st practice (demo) - 15mins
2nd practice (by attendees) – 50’ – 40-50mins
3rd practice (by attendees) – 1:30-40
Final recap – 1:40 - 15-20mins
Around 2hs:
Theorical content - 35-40mins
(demo) - 15mins
1st practice (by attendees) – 60’ – 40-50mins
Final recap – 1:50 - 5-10mins
Kent Beck’s book…
Developed XP during his work at Chrysler
VS create tests after…
You have to kind of "break" your rules and think outside your comfort zone
Around 2hs:
Theorical content - 35-40mins
(demo) - 15mins
1st practice (by attendees) – 60’ – 40-50mins
Final recap – 1:50 - 5-10mins
Specify what you want to code to do
Create just enough code to satisfy the desired behaviour (just make it work!)
Clean your code and test / remove duplication / make it better
Around 2hs:
Theorical content - 35-40mins
(demo) - 15mins
1st practice (by attendees) – 60’ – 40-50mins
Final recap – 1:50 - 5-10mins
Nacho -
Nacho -
(copy the image in the Slack Channel ?)
Nacho -
Nacho -
Around 2hs:
Theorical content - 35-40mins
(demo) - 15mins
1st practice (by attendees) – 60’ – 40-50mins
Final recap – 1:50 - 5-10mins
Around 2hs:
Theorical content - 35-40mins
(demo) - 15mins
1st practice (by attendees) – 60’ – 40-50mins
Final recap – 1:50 - 5-10mins
Around 2hs:
Theorical content - 35-40mins
(demo) - 15mins
1st practice (by attendees) – 60’ – 40-50mins
Final recap – 1:50 - 5-10mins
This back-and-forth between test and code, driver and navigator, can offer better/richer feedback when building a system.
Around 2hs:
Theorical content - 35-40mins
(demo) - 15mins
1st practice (by attendees) – 60’ – 40-50mins
Final recap – 1:50 - 5-10mins
XP explained – 1st time TDD appeared – nice intro to XP practices
TDD by example – basic intro, idealised situation…
The art of unit testing – easy to read
TDD a practical guide – some examples with GUI & real examples