Have you heard of TDD? Are you interested or familiar with this practice but have never been able to understand it?
Join this session to 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.
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.
===
This is the presentation I gave at Switzerland JUG:
https://www.jug.ch/html/events/2021/tdd.html
3. 3
Confidential
Whythis session?
• Review the basics around TDD
• Understand the benefits of building code
designed by tests
VS
building the tests after we write our code
(before VS after)
• See an example
• (I hope) learn some tips
• … 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?
8. 8
Confidential
• Where it cames from?
• Advantages & disadvantages
• Process &rules
• Good habits
• Example
• 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 1st that will help us demonstrate the
behaviour we want.
Wewill start writing tests and we can’t writeverymuchapart
from a unittest. Testsare first class citizens
No overengineering, simple code, simple solution: make the
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
• Example
• 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
• Ourtests 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
• Practice...
• ... and keeppracticing
• ... and continues
• ... until you finally internalizeit 🖖♀️😉
• Checkonline content
• Practicewithsomebody else ( pair-programming)-
nextslide👉-
But, howto start?
25. Confidential 25
• 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
26. Confidential 26
• Whereit camesfrom?
• Advantages & disadvantages
• Process &rules
• Good habits
• Example
• Final Recap
Agenda
28. Confidential 28
Kata:Film RecommendationService
• Wehave just started workingin a startup that sells and
rentsout films over the Internet
• Ourproduct managers have told us that theywant to add
new functionalities by building a service that allows
them to recommendfilms to the users of the current
platform.
• Requirements:
• Build a service that returnsa list of films that are associated with a
particular genre
• By default, the result must be ordered according to the average ratinggiven
to the films by theusers
• A film should contain at least a title, a yearwhenit was published, one or
more tags and one or moregenres
• Example:
• recommendationService.filmsByGenre("science-fiction”)
https://github.com/icougil/FilmRecommendationService
29. Confidential 29
• Whereit camesfrom?
• Advantages & disadvantages
• Process &rules
• Good habits
• Example
• Final Recap
Agenda
30. Confidential 30
• 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
31. Confidential 31
• TDD helpsyouto develop yourcode in a simple and effective way
(better modular design) 👌
• 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
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
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
Nacho -
Nacho -
(copy the image in the Slack Channel ?)
Nacho -
Nacho -
This back-and-forth between test and code, driver and navigator, can offer better/richer feedback when building a system.
Genre = spoke “jonra”
GOOST >> TDD learn, understand & apply
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