SlideShare a Scribd company logo
1 of 17
Download to read offline
1
TDD PracticesTDD Practices
Sang ShinSang Shin
““Code with Passion!”Code with Passion!”
1
2
Topics
• Characteristics of good tests
• What is and why TDD?
• TDD development cycle
• TDD best practices
• TDD anti-patterns
• Testable code best practices
Characteristics ofCharacteristics of
Good TestsGood Tests
4
Characters of Good Tests
• Run fast
> Short setup, run time, tear down
• Run in isolation
> Tests should not rely on data or state created/modified by other tests
> Tests should be able to be reordered
• Use real data whenever possible
> Use copies of production data if possible
• Repeatable
> Should return same result each time it runs
5
Goals for Well-Written Tests
• Readable by others
• Should serve as sample client code for others
• Should serve as a specification (document)
• Should test what they're supposed to test (i.e. that the requirements
have been met) and no more
• Not tightly coupled to the target code
• Should follow good coding principles (single responsibility, etc.)
What is and Why TDD?What is and Why TDD?
7
What is TDD?
• Test-driven development (TDD) is a software development
process that relies on the repetition of a very short development
cycle of Red, Green, Refactor
> As opposed to writing a big chunk of code at a time
• Requirements are turned into very specific test cases, then the
software is improved to pass the new tests only
> As opposed to writing software beyond the requirements
8
Why TDD?
• It helps you focus on design
> “The act of writing a unit test is more an act of design than of
verification. It is also more an act of documentation than of
verification” (Uncle Bob)
• It helps you to think from customer requirements
> The act of writing a unit test closes a remarkable number of
feedback loops
• It helps you to build better written and better tested software
> It helps you to write more testable code
TDD Development CycleTDD Development Cycle
10
TDD Cycle: Red-Green-Refactor
• Repeat the Red-Green-Refactor cycle
> Add failing test (Red)
> Write just enough and simplest possible code to correct the test
failure (Green)
> Refactor code (Refactor)
• For each cycle
> Prefer to add a few new lines of functional code, typically less than
ten, before rerunning tests
> Prefer to limit each TDD cycle to be less than ~2 to ~5 minutes
(could be longer as you become more fluent in TDD)
TDD Best PracticesTDD Best Practices
12
TDD Best Practices
• Only specify what is important
• Make small steps
> Solve things as simply as possible
> Follow ~2 to ~5 minute rule
• Perform refactoring after each passing test
> Don't lose the best opportunity to refactor
• Make testing code expressive and readable
> Testing code should serve as pseudo-documentation
> Example: should_result_this_when_xyz_occurs()
Anti-PatternsAnti-Patterns
14
Examples of Anti-Patterns
• The free ride
> Rather than write a new test method for another feature, a new
assertion rides along an existing test
• The loud mouth
> Clutters up the console with diagnostic messages, logging messages,
and other chatter, even when passing
• The stranger
> A test case that doesn't even belong in the unit tests it's part of. It's
really testing a separate object
• The local hero
> A test case that depends on something specific to the development
environment, passes on one machine, fails on another
Writing Testable CodeWriting Testable Code
Best PracticesBest Practices
16
Writing Testable Code
• Do not use Singleton class unless there is a reason
> It is impossible to mock a Singleton
• Get dependencies to be injected
> Is it impossible to mock the internally created dependencies
17
Code with Passion!Code with Passion!
17

More Related Content

What's hot

Tdd in php a brief example
Tdd in php   a brief exampleTdd in php   a brief example
Tdd in php a brief example
Jeremy Kendall
 
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Zohirul Alam Tiemoon
 
Scrum and Test-driven development
Scrum and Test-driven developmentScrum and Test-driven development
Scrum and Test-driven development
toteb5
 

What's hot (20)

Test drive on driven development process
Test drive on driven development processTest drive on driven development process
Test drive on driven development process
 
Test driven development vs Behavior driven development
Test driven development vs Behavior driven developmentTest driven development vs Behavior driven development
Test driven development vs Behavior driven development
 
TDD CrashCourse Part2: TDD
TDD CrashCourse Part2: TDDTDD CrashCourse Part2: TDD
TDD CrashCourse Part2: TDD
 
TDD = bra design?
TDD = bra design?TDD = bra design?
TDD = bra design?
 
Tdd com Java
Tdd com JavaTdd com Java
Tdd com Java
 
Test Driven Development Powered by LEGO
Test Driven Development Powered by LEGOTest Driven Development Powered by LEGO
Test Driven Development Powered by LEGO
 
Tdd in php a brief example
Tdd in php   a brief exampleTdd in php   a brief example
Tdd in php a brief example
 
TDD That Was Easy!
TDD   That Was Easy!TDD   That Was Easy!
TDD That Was Easy!
 
A Not-So-Serious Introduction to Test Driven Development (TDD)
A Not-So-Serious Introduction to Test Driven Development (TDD) A Not-So-Serious Introduction to Test Driven Development (TDD)
A Not-So-Serious Introduction to Test Driven Development (TDD)
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
TDD (Test Driven Design)
TDD (Test Driven Design)TDD (Test Driven Design)
TDD (Test Driven Design)
 
TDD - Test Driven Development
TDD - Test Driven DevelopmentTDD - Test Driven Development
TDD - Test Driven Development
 
Agile Test Driven Development
Agile Test Driven DevelopmentAgile Test Driven Development
Agile Test Driven Development
 
TDD vs. ATDD - What, Why, Which, When & Where
TDD vs. ATDD - What, Why, Which, When & WhereTDD vs. ATDD - What, Why, Which, When & Where
TDD vs. ATDD - What, Why, Which, When & Where
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
 
Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010
 
Introduction to TDD (Test Driven development) - Ahmed Shreef
Introduction to TDD (Test Driven development) - Ahmed ShreefIntroduction to TDD (Test Driven development) - Ahmed Shreef
Introduction to TDD (Test Driven development) - Ahmed Shreef
 
Scrum and Test-driven development
Scrum and Test-driven developmentScrum and Test-driven development
Scrum and Test-driven development
 

Similar to Tdd practices

Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
bhochhi
 
Test-Driven Development Reference Card
Test-Driven Development Reference CardTest-Driven Development Reference Card
Test-Driven Development Reference Card
Seapine Software
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
Meilan Ou
 
{10.0} Test Driven Development.pptx
{10.0} Test Driven Development.pptx{10.0} Test Driven Development.pptx
{10.0} Test Driven Development.pptx
AmalEldhose2
 

Similar to Tdd practices (20)

Android tdd
Android tddAndroid tdd
Android tdd
 
Joe Cisar - Everything I Know About TDD - Agile Midwest 2019
Joe Cisar - Everything I Know About TDD - Agile Midwest 2019Joe Cisar - Everything I Know About TDD - Agile Midwest 2019
Joe Cisar - Everything I Know About TDD - Agile Midwest 2019
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Tdd and bdd
Tdd and bddTdd and bdd
Tdd and bdd
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Pair programming and introduction to TDD
Pair programming and introduction to TDDPair programming and introduction to TDD
Pair programming and introduction to TDD
 
Unit Testing talk
Unit Testing talkUnit Testing talk
Unit Testing talk
 
TDD - Christchurch APN May 2012
TDD - Christchurch APN May 2012TDD - Christchurch APN May 2012
TDD - Christchurch APN May 2012
 
Test-Driven Development Reference Card
Test-Driven Development Reference CardTest-Driven Development Reference Card
Test-Driven Development Reference Card
 
Understanding TDD - theory, practice, techniques and tips.
Understanding TDD - theory, practice, techniques and tips.Understanding TDD - theory, practice, techniques and tips.
Understanding TDD - theory, practice, techniques and tips.
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 
TDD and Unit Testing in Golang
TDD and Unit Testing in GolangTDD and Unit Testing in Golang
TDD and Unit Testing in Golang
 
Tdd
TddTdd
Tdd
 
Kata Your Way to SW Craftsmanship
Kata Your Way to SW CraftsmanshipKata Your Way to SW Craftsmanship
Kata Your Way to SW Craftsmanship
 
{10.0} Test Driven Development.pptx
{10.0} Test Driven Development.pptx{10.0} Test Driven Development.pptx
{10.0} Test Driven Development.pptx
 
Test Driven Development Methodology and Philosophy
Test Driven Development Methodology and Philosophy Test Driven Development Methodology and Philosophy
Test Driven Development Methodology and Philosophy
 
Test first!
Test first!Test first!
Test first!
 
Test Driven Development on Android (Kotlin Kenya)
Test Driven Development on Android (Kotlin Kenya)Test Driven Development on Android (Kotlin Kenya)
Test Driven Development on Android (Kotlin Kenya)
 
TDD
TDDTDD
TDD
 

Recently uploaded

%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
masabamasaba
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 

Recently uploaded (20)

%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
%+27788225528 love spells in Colorado Springs Psychic Readings, Attraction sp...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 

Tdd practices

  • 1. 1 TDD PracticesTDD Practices Sang ShinSang Shin ““Code with Passion!”Code with Passion!” 1
  • 2. 2 Topics • Characteristics of good tests • What is and why TDD? • TDD development cycle • TDD best practices • TDD anti-patterns • Testable code best practices
  • 4. 4 Characters of Good Tests • Run fast > Short setup, run time, tear down • Run in isolation > Tests should not rely on data or state created/modified by other tests > Tests should be able to be reordered • Use real data whenever possible > Use copies of production data if possible • Repeatable > Should return same result each time it runs
  • 5. 5 Goals for Well-Written Tests • Readable by others • Should serve as sample client code for others • Should serve as a specification (document) • Should test what they're supposed to test (i.e. that the requirements have been met) and no more • Not tightly coupled to the target code • Should follow good coding principles (single responsibility, etc.)
  • 6. What is and Why TDD?What is and Why TDD?
  • 7. 7 What is TDD? • Test-driven development (TDD) is a software development process that relies on the repetition of a very short development cycle of Red, Green, Refactor > As opposed to writing a big chunk of code at a time • Requirements are turned into very specific test cases, then the software is improved to pass the new tests only > As opposed to writing software beyond the requirements
  • 8. 8 Why TDD? • It helps you focus on design > “The act of writing a unit test is more an act of design than of verification. It is also more an act of documentation than of verification” (Uncle Bob) • It helps you to think from customer requirements > The act of writing a unit test closes a remarkable number of feedback loops • It helps you to build better written and better tested software > It helps you to write more testable code
  • 9. TDD Development CycleTDD Development Cycle
  • 10. 10 TDD Cycle: Red-Green-Refactor • Repeat the Red-Green-Refactor cycle > Add failing test (Red) > Write just enough and simplest possible code to correct the test failure (Green) > Refactor code (Refactor) • For each cycle > Prefer to add a few new lines of functional code, typically less than ten, before rerunning tests > Prefer to limit each TDD cycle to be less than ~2 to ~5 minutes (could be longer as you become more fluent in TDD)
  • 11. TDD Best PracticesTDD Best Practices
  • 12. 12 TDD Best Practices • Only specify what is important • Make small steps > Solve things as simply as possible > Follow ~2 to ~5 minute rule • Perform refactoring after each passing test > Don't lose the best opportunity to refactor • Make testing code expressive and readable > Testing code should serve as pseudo-documentation > Example: should_result_this_when_xyz_occurs()
  • 14. 14 Examples of Anti-Patterns • The free ride > Rather than write a new test method for another feature, a new assertion rides along an existing test • The loud mouth > Clutters up the console with diagnostic messages, logging messages, and other chatter, even when passing • The stranger > A test case that doesn't even belong in the unit tests it's part of. It's really testing a separate object • The local hero > A test case that depends on something specific to the development environment, passes on one machine, fails on another
  • 15. Writing Testable CodeWriting Testable Code Best PracticesBest Practices
  • 16. 16 Writing Testable Code • Do not use Singleton class unless there is a reason > It is impossible to mock a Singleton • Get dependencies to be injected > Is it impossible to mock the internally created dependencies
  • 17. 17 Code with Passion!Code with Passion! 17