TDD
TDD
An Overview of Test
Driven Development
Ganesh Samarthyam
[email protected]
www.codeops.tech
Old school approach
Red
Refactor Green
passing test(s)
TDD mantra
Source: Test Driven Development: By Example, Kent Beck, 240 pages, Addison-Wesley Professional, 2002;
Best Practice
Source: Professionalism and Test-Driven Development, Robert C. Martin, IEEE Software, 2007
TDD in action: Case study
❖ “Over the last few years, Micah Martin and I’ve been working on an application named
FitNesse (www.fitnesse.org).
❖ FitNesse is a Web-based application using a Front Controller that defers to servlets that
direct views.
❖ Downloaded tens of thousands of times, FitNesse consists of 45,000 lines of Java code in
nearly 600 files.
❖ Almost 20,000 of those lines and 200 of those files are unit tests.
❖ Over 1,100 specific test cases contain many thousands of assertions, giving FitNesse test
coverage of over 90 percent (as measured by Clover, http://cenqua.com/clover).
❖ These tests execute at the touch of an IDE (integrated development environment) button
in approximately 30 seconds.
❖ These tests, along with the convenience of easily executing them, have benefits that far
exceed simple software verification.”
Source: Professionalism and Test-Driven Development, Robert C. Martin, IEEE Software, 2007
TDD process cycle
https://en.wikipedia.org/wiki/Test-driven_development#/media/File:TDD_Global_Lifecycle.png
“You know you are working on clean code when
each routine you read turns out to be pretty much
what you expected. You can call it beautiful code
when the code also makes it look like the language
was made for the problem.”
–Ward Cunningham
TDD - benefits
JUnit TestNG
jWalk Mockito
“TDD helps with, but does not guarantee, good
design & good code. Skill, talent, and
expertise remain necessary.”
— Esko Luontola
TDD slows down development?
Unit testing (with JUnit)
Unit testing: common excuses
❖ I am paid to write code, not tests!
❖ I am a developer, not tester - so I’m not responsible for writing tests!
❖ We already have tests - why do we need unit tests?
❖ We are working on a tight deadline - where do I have time for
writing unit tests?
❖ I don’t know how to write unit tests
❖ Ours is legacy code - can’t write unit tests for them
❖ If I touch the code, it breaks!
What dependencies can unit test have?
❖ A test is not a unit test if:
❖ It talks to the database
❖ It communicates across the network
❖ It touches the file system
❖ It can’t run at the same time as any of your other unit tests
❖ You have to do special things to your environment (such as
editing config files) to run it.
— Michael Feathers, in A Set Of Unit Testing Rules (2005)
The law of low-hanging fruit!
❖ Fast
❖ Independent
❖ Repeatable
❖ Self-validating
❖ Timely
JUnit
https://github.com/junit-
GitHub repo
team/junit4
Website www.junit.org
Source: wikipedia
"Never in the field of software engineering has so
much been owed by so many to so few lines of code."
–Martin Fowler (on JUnit)
Getting started with JUnit
Execution
Validation
Cleanup
Assert methods in JUnit
assertSame()/
Uses == on the arguments to check whether they are equal
assertNotSame()
assertTrue()/
Checks if the given boolean argument evaluates to true or false
assertFalse()
assertNull()/
Checks if the given argument is null or NOT null
assertNotNull()
jMock Mockito
EasyMock PowerMock
Test “doubles”
Stubs
Mocks
Terminology
❖ Plumbing - “Stub-Outs” - Short pipes put in early during plumbing work during
constructions - eventually, they will be replaced with real pipes and fixtures.
Related topics
Preparatory refactoring
Source: https://martinfowler.com/articles/preparatory-refactoring-example.html
Acceptance TDD
Story
Automate tests
Code quality tools
PMD FindBugs
IntelliJ IDEA/
CheckStyle
Eclipse analysers
Image credits
❖ https://s-media-cache-ak0.pinimg.com/736x/78/c9/9e/78c99e530a69406ec249588ef87a59a9.jpg
❖ http://www.datamation.com/imagesvr_ce/4130/development-driven.jpg
❖ https://s-media-cache-ak0.pinimg.com/736x/ae/22/01/ae2201013b69918a20b6de0adf1517a1.jpg
❖ http://blog.itexus.com/img/tdd_comics.png
❖ https://martinfowler.com/articles/preparatory-refactoring-example/jessitron.png
❖ https://i2.wp.com/www.gilzilberfeld.com/wp-content/uploads/2011/02/regret-testing.png
❖ https://pixabay.com/en/pegasus-horse-winged-mythology-586124/
❖ https://pixabay.com/en/horse-gallop-horses-standard-1401914/
❖ https://refactoring.guru/images/content-public/index-clean-code.png
❖ http://www.lifeisanecho.com/wp-content/uploads/2016/06/ar131070344825846.jpg
❖ https://pixabay.com/en/ball-chain-bug-jail-insect-46207/
❖ https://pixabay.com/en/home-old-school-old-old-building-1824815/
❖ https://pixabay.com/en/escalator-stairs-metal-segments-283448/
❖ https://devops.com/wp-content/uploads/2016/07/tdd-01-1.jpg
❖ http://www.nanrussell.com/wp-content/uploads/2015/08/Not-me.jpg
❖ https://cdn.meme.am/instances/500x/43446748/winter-is-coming-brace-yourselves-endless-client-revisions-are-coming.jpg
❖ https://t4.ftcdn.net/jpg/00/87/17/55/240_F_87175567_I7FK0h2XNxrwtnoYbufTzvpLv3p2cFrk.jpg
❖ https://cdn.meme.am/cache/instances/folder518/500x/64808518/yeah-if-you-could-just-if-we-could-stop-changing-requirements-every-5-minutes-that-would-be-great.jpg
Image credits
❖ http://optymyze.com/blog/wp-content/uploads/sites/2/2017/02/change.jpg
❖ http://bookboon.com/blog/wp-content/uploads/2014/03/D%C3%A9veloppez-votre-potentiel.jpg
❖ https://techbeacon.com/sites/default/files/most_interesting_man_test_in_production_meme.jpg
❖ https://cdn-images-1.medium.com/max/490/1*k-OkcZd2fAyZf1WBkharGA.jpeg
❖ https://akchrish23.files.wordpress.com/2012/12/far-side-first-pants-then-your-shoes.jpg
❖ https://image.slidesharecdn.com/its-all-about-design-1232847245981881-1/95/its-all-about-design-10-728.jpg?cb=1232825731
❖ http://www.fox1023.com/wp-content/uploads/2016/06/fail-sign1.jpg
❖ https://vgarmada.files.wordpress.com/2012/04/pass-sign.jpg
❖ http://codelikethis.com/lessons/agile_development/make-it-green.png
❖ https://refactoring.guru/images/content-public/index-refactoring-how.png
❖ http://geek-and-poke.com/geekandpoke/2014/1/15/philosophising-geeks
❖ https://employmentdiscrimination.foxrothschild.com/wp-content/uploads/sites/18/2014/06/20350757_s.jpg
❖ https://static1.squarespace.com/static/5783a7e19de4bb11478ae2d8/t/5821d2ea09e1c46748737af1/1478614300894/shutterstock_217082875-e1459952801830.jpg
❖ https://lh3.googleusercontent.com/-eM1_28qE1cM/U1bUFmBU1NI/AAAAAAAAHEk/ZqLcxFEhMuA/w530-h398-p/slide-32-638.jpg
❖ http://www.trainingforwarriors.com/wp-content/uploads/2015/03/3-Laws-Post.jpg
Image credits
❖ https://patientsrising.org/sites/default/files/Step%20Therapy.PNG
❖ https://1.bp.blogspot.com/-Q00OoZelCic/WFSmGIUCrGI/AAAAAAAAx5U/i59y1h-
czIIXNswq6aMdAOUGjgPLaPdxACLcB/s1600/awful.png
❖ http://s2.quickmeme.com/img/f4/f4b4744206cf737305f1a4619fefde7b0df54ecc0dc012adcceaadf93196a7e8.jpg
❖ https://pbs.twimg.com/media/CeZu1YjUsAEfhcP.jpg:large
❖ https://upload.wikimedia.org/wikipedia/en/thumb/f/ff/Poison_Help.svg/1024px-Poison_Help.svg.png
❖ http://data.whicdn.com/images/207820816/large.jpg
❖ http://orig04.deviantart.net/c7cb/f/2014/171/d/a/the_bare_minimum_bandits_by_shy_waifu-d7n8813.png
❖ http://fistfuloftalent.com/wp-content/uploads/2017/04/3c0f8f4.jpg
References
❖ Wikipedia page - JUnit: https://en.wikipedia.org/
wiki/JUnit
❖ Wikipedia page - Test Driven Development: https://
en.wikipedia.org/wiki/Test-driven_development
❖ Professionalism and TDD: https://8thlight.com/blog/
uncle-bob/2014/05/02/ProfessionalismAndTDD.html
❖ Preparatory refactoring: https://martinfowler.com/
articles/preparatory-refactoring-example.html
Recommended reading
Classic on TDD by its
originator Kent Beck
Test Driven Development: By Example, Kent Beck, 240 pages, Addison-Wesley Professional, 2002;
Recommended reading
Covers principles, best
practices, heuristics,
and interesting
examples
Growing Object-Oriented Software, Guided by Tests, Steve Freeman, Nat Pryce, Addison Wesley, 2009
Recommended reading
“Refactoring for Software Design Smells: Managing Technical Debt”, Girish Suryanarayana, Ganesh Samarthyam, Tushar Sharma, ISBN - 978-0128013977, Morgan
Kaufmann/Elsevier, 2014.
[email protected] @GSamarthyam
www.codeops.tech slideshare.net/sgganesh
+91 98801 64463 bit.ly/sgganesh