SlideShare ist ein Scribd-Unternehmen logo
1 von 40
Downloaden Sie, um offline zu lesen
TEST DRIVEN DEVELOPMENT
DAVID EHRINGER
This work by David Ehringer is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License
DAVIDEHRINGER.COM
CONTENTS
What Is TDD?
TDD Principals
Tools
Live Coding
Best Practices And Smells
Other (Tips, Resources, BDD, Etc.)
BACKGROUND
What is TDD?
A PERSPECTIVE ON TDD
TDD is Mostly about Design
Gives Confidence
Enables Change
Is Automated
Validates Your Design
Is Documentation By Example
As Well As an Up To Date, “Live”,
And Executable Specification
Provides Rapid Feedback
Quality of Implementation
Quality of Design
Forces Constant Integration
Requires More Discipline
Brings Professionalism to Software
Development
Isn’t The Only Testing You’ll Need
To Do
Developers Write Tests
RESULT
Higher Quality
Flexibility
Readability
Maintainability
Predicability
Simplicity
The Hard Stuff And Surprises Are Tackled Early On
Both Internal And External Quality Are Maintained
A Well-Designed Application
THE BASIC CYCLE
THE THREE LAWS OF TDD
1: You May Not Write Production Code Until You Have
Written A Failing Unit (or Acceptance) Test
2: You May Not Write More Of A Unit (or Acceptance) Test
Than Is Sufficient To Fail, And Compiling Is Failing
3: You May Not Write More Production Code Than Is
Sufficient To Pass The Currently Failing Test
Ever so slightly adapted from Professionalism and Test-Driven
Development by Robert “Uncle Bob” C. Martin
FUNDAMENTAL PRINCIPLES
Think About What You Are Trying To Do
Follow The TDD Cycle And The Three Laws
Never Write New Functionality Without A Failing Test
Continually Make Small, Incremental Changes
Keep The System Running At All Times
No One Can Make A Change That Breaks The System
Failures Must Be Address Immediately
In reality, unwavering adherence the cycle and laws can sometimes be very hard. In my opinion, it
doesn’t always have to be all or nothing (especially when you are just learning). But I’ve found
that when I don’t write tests first, I usually regret it afterwards. Also, TDD isn’t applicable for
every single scenario or line of code.
“TDD IN A NUTSHELL”
As we develop the system, we use TDD to give us feedback on the quality of both its implementation
(“Does it work?”) and design (“Is it well structured?”). Developing test-first, we find we benefit
twice from the effort. Writing Tests:
• makes us clarify the acceptance criteria for the next piece of work - we have to ask ourselves
how we can tell when we’re done (Design);
• encourages us to write loosely coupled components, so they can easily be tested in isolation and,
at higher levels, combined together (Design);
• adds an executable description of what the code does (Design); and,
• adds to a complete regression suite (Implementation)
whereas running tests:
• detects errors while the context is fresh in our mind (Implementation); and,
• lets us know when we’ve done enough, discouraging “gold plating” and unnecessary features
(Design)
From Growing Object-Oriented Software, Guided By Tests
BUT HOW MUCH LONGER DOES TDD
TAKE?
“If it doesn’t have to work, I can get it done a lot
faster!” - Kent Beck paraphrased
My Experience
- Initial Progress Will Be Slower
- Greater Consistency
- Less Experienced Developer Learning/Ramp-up Time
- I personally think I can develop faster using TDD
than without, but it took a while to get there
- Long-term cost is drastically lower
Studies
- Takes 15-30% longer
- 45-90% fewer bugs
- Takes 10x as long to fix a bug in later phases
- One study with Microsoft and IBM: http://
research.microsoft.com/en-us/projects/esm/
nagappan_tdd.pdf
SOME DESIGN PRINCIPALS
Loosely coupled, highly cohesive
Dependency Injection/Inversion of
Control (IOC)
Single Responsibility Principal
Strive for Simplicity
Open/Closed Principal
Law of Demeter (“Tell don’t ask.”)
Modularization
Separation of Concerns
Encapsulation
Favor Immutability
Domain Driven Design
Decentralized Management and
Decision Making
Strict Dependency Management
Clean Code
Ubiquitous Language
Eliminate Duplication
WHAT ABOUT ARCHITECTURE AND
DESIGN?
TDD Does Not Replace Architecture Or Design
You Need To Do Up Front Design And Have A Vision
But, You Should Not Do “Big Up Front Design”
Defer Architectural Decisions As Long As Is “Responsibly” Possible
TDD Will Inform And Validate (or Invalidate) Your Design
Decisions
TDD Will Almost Undoubtedly Uncover Weaknesses And Flaws In
Your Design...Listen To Them!
CATEGORIES OF TESTS
Unit
Integration
Acceptance
Can Encompass “system” Or “functional” Tests
End-to-end Is Desirable When Feasible
Learning Or Exploratory
THE EXTENDED CYCLE
From Growing Object-Oriented Software, Guided By Tests
TOOLS OF THE TRADE
Unit Test Framework
Mocking Framework
Automated Build Tool
Acceptance Test Framework (often The Same As The Unit Test Framework)
Source Code Management
Build And Continuous Integration Servers
Test Coverage Tools
Code Quality Tools
For Java-based Apps, Consider Using A Dynamic Language (e.g. Groovy) To Write Tests If
You Are Comfortable With It
TOOLS OF THE TRADE -
RECOMMENDATIONS
Unit Test Framework - JUnit 4, Hamcrest Matchers Are Recommended For
Improved Readability And Expressiveness
Mocking Framework - Mockito
Automated Build Tool - Maven
Acceptance Test Framework - Web Services: SoapUI, Web App: Selenium
Source Code Management - Depends On The Project Needs
Continuous Integration - Bamboo Is Nice, Should Be Simple And Easy To Use And
Support Your Build Tool
Test Coverage Tools - Cobertura Or Clover (soapUI Pro For Web Services)
Code Quality Tools - PMD, FindBugs, Checkstyle
SOME NOTES ON YOUR BUILD
Should Be Fully Automated
Should Run Anywhere And Be Decoupled From The IDE
You Should Be Able To Build Your Artifact In One Command
“mvn package”
You Should Be Able To Run Your Unit Tests In One Command
“mvn test”
You Should Be Able To Run You Acceptance Tests In One Command
“mvn integration-test”
Test Coverage And Code Quality Tools Should Be Integrated Into The Build
BUILD PIPELINE
MAVEN BUILD LIFECYCLE
MOCKING
Limit The Scope Of Your Test To The Object Under Test
Mocks Mimic The Behavior Of Objects (the Dependencies Of
Your Object Under Test) In Predictable Ways
Mocks Contains Assertions Themselves
Prefer Mocking Of Behavior Rather Than Data (strictly
Stubs)
Be Careful: Mocks Can Couple The Test To The Underlying
Implementation Of The Code Being Tested
TDD BY EXAMPLE
Time For Some Live Coding
BUT FIRST... A FEW MORE WORDS
Iteration 0/Walking Skeleton
Build, Deploy, Test End-to-end
Includes Process As Well As Infrastructure
THE EXAMPLE
A Trivial Customer Management App
Web-service, Business Logic, Database
Starts With A Full, Walking Skeleton
BACK TO THE EXAMPLES
Starting With An Acceptance Test
Unit Tests To Fill In The Details
Refactoring Along The Way
Components/Modules And Large Projects
CODE COVERAGE AND QUALITY TOOLS
NON-LIVE EXAMPLES
Learning/Exploratory
Mocking
Test Data Builders
Others?
BEST PRACTICES: F.I.R.S.T
Fast
Independent
Repeatable
Self-validating
Timely
From Object Mentor, www.objectmentor.com
SPEED: TRADITIONAL DEVELOPMENT
• 5 mins per coding hour becomes 6000
minutes annually (2.5, 40-hour
workweeks)
• 9 mins per coding hour becomes 10800
minutes annually (4.5 weeks)
• 14 mins per coding hour becomes
16800 mins annually (7 weeks)
Survey by ZeroTurnaround
http://www.zeroturnaround.com/blog/java-ee-
container-redeploy-restart-turnaround-report/
SPEED IS CRITICAL
Developers Need Immediate Feedback
Tests Must Run Fast
Consider Using Jetty And HSQLDB For Automated Unit And
Acceptance Testing
Depending On Your Framework, It Can Be Easy To Use Jetty/
HSQLDB For Local Development And Automated Testing And
Deploy To Websphere/Oracle In “Real” Environments
Make Sure You Do Continuous And Early Integration
BEST PRACTICES: CONSISTENCY
Test Environments Should Be Scripted And Consistent
Test Data Should Be Scripted And Consistent
Database
In-memory Databases
Fast
Reliable
Easily Scriptable
HSQLDB
Messaging
ActiveMQ Is A Good Alternative For Tests
Simple To Set Up
BEST PRACTICES: TEST NAMING AND
STRUCTURE
The Name Of The Test Should Describe A Feature Or Specification
The Name Should Clearly Describe The Purpose Of The Test
Frequently Read The Test Documentation
Each Test Should Be For A Single Concept
Strive For One Assertion Per Test
Or As Few As Possible
More Than One May Indicate More Than One Concept Is Being Tested
What Does The Object Do, Not What Is The Object
BEST PRACTICES
Only Specify What Is Important
Even Seemingly Trivial Tests Are Important
Treat Your Test Code Like Production Code
Maintain
Refactor
Expressiveness And Readability Of Tests
Check In To Source Control Frequently
Consistent Test Structure
Setup/Execute/Verify/Teardown
Given/When/Then
BEST PRACTICES: MANAGING TEST DATA
Objects
Avoid Mocking Values
Test Data Builders
Persistent
In-memory Database
Scripted Data
SMELLS
Doing The Opposite Of The Best Practices :-)
Just Making It Work Isn’t Good Enough
Should Be Able To Run Tests Independently
Tests Should Never Depend On Each Other
TIPS: ECLIPSE SHORTCUTS RELATED TO
TESTING
Ctrl+1: Quick Fix
Ctrl+Shift+X, T : Run As Unit Test (Use In Any Context: Method, Class, Package,
Project, Etc.)
F11: Launch Last Test
F3: Open Declaration
Alt+Shift+3: Refactoring Menu
Alt+Shift+S: Code Completion/Generation/Format Menu
Ctrl+e: Switch Between Open Windows
Eclipse Static Import Favorites
These are Windows specific. On Mac you usually,
but not always, substitute Command for Ctrl
TIPS: SOME OTHER ECLIPSE SHORTCUTS
Ctrl-Space: Code Completion
Ctrl-Shift-F: Format Code
Ctrl-Shift-O: Organize Import
Ctrl-Shift-T: Open Type
Ctrl-Shift-R: Open Resource
Ctrl-/: Toggle Comment
Ctrl-Shift-W: Close All Open Windows
Ctrl-N: New Wizard
Ctrl-F7: Move Between Open Views
Ctrl+Shift+L: Show Shortcuts
Alt+left, Alt+right: Toggle Windows
TDD CHALLENGES
Discipline Is Required
Developers Are Often Stubborn And Lazy
It Is Hard For Developers To Drop Bad Habits
Management Doesn’t Often Understand “Internal Quality”
SOME RESOURCES RELATED TO TDD
Should Be On Every Professional Developers Bookshelf
Refactoring (Fowler)
Domain Driven Design (Evans)
Clean Code (“Uncle Bob” Martin)
Recommended
Growing Object-Oriented Software, Guided By Tests (Freeman, Pryce)
Implementation Patterns (Beck)
Haven’t Read But, Given The Authors, Should Be Good
Test Driven Development: By Example (Beck)
The Book That “Rediscovered” TDD
XUnit Patterns (Meszaros)
By No Means An Exhaustive List But Should Keep You Busy For A While
BEHAVIOR DRIVEN DEVELOPMENT
“TDD Done Right”
Stories And Specifications
Ubiquitous Language
Given/When/Then
Should/Ensure
easyb (Groovy) Is Nice If You Are Working In The Java
World

Weitere ähnliche Inhalte

Was ist angesagt?

Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven DevelopmentNaresh Jain
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDDDror Helper
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPTsuhasreddy1
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And MockingJoe Wilson
 
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
 
Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.Mohamed Taman
 
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 ShreefAhmed Shreef
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testingikhwanhayat
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit TestingJoe Tremblay
 
Unit Tests And Automated Testing
Unit Tests And Automated TestingUnit Tests And Automated Testing
Unit Tests And Automated TestingLee Englestone
 
Unit and integration Testing
Unit and integration TestingUnit and integration Testing
Unit and integration TestingDavid Berliner
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesDerek Smith
 
Introduction to CICD
Introduction to CICDIntroduction to CICD
Introduction to CICDKnoldus Inc.
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven DevelopmentMireia Sangalo
 
Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practicesnickokiss
 

Was ist angesagt? (20)

Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPT
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
 
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...
 
Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.
 
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
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testing
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
Unit Tests And Automated Testing
Unit Tests And Automated TestingUnit Tests And Automated Testing
Unit Tests And Automated Testing
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Unit and integration Testing
Unit and integration TestingUnit and integration Testing
Unit and integration Testing
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Introduction to CICD
Introduction to CICDIntroduction to CICD
Introduction to CICD
 
Junit
JunitJunit
Junit
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Intro to DevOps
Intro to DevOpsIntro to DevOps
Intro to DevOps
 
Unit testing with JUnit
Unit testing with JUnitUnit testing with JUnit
Unit testing with JUnit
 
Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practices
 

Ähnlich wie Test Driven Development (TDD)

Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Developmentadrianmitev
 
Test driven development v1.0
Test driven development v1.0Test driven development v1.0
Test driven development v1.0Ganesh Kondal
 
Unit Testing, TDD and the Walking Skeleton
Unit Testing, TDD and the Walking SkeletonUnit Testing, TDD and the Walking Skeleton
Unit Testing, TDD and the Walking SkeletonSeb Rose
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In ActionJon Kruger
 
Test Driven Development - Overview and Adoption
Test Driven Development - Overview and AdoptionTest Driven Development - Overview and Adoption
Test Driven Development - Overview and AdoptionPyxis Technologies
 
Introduction to test programming
Introduction to test programmingIntroduction to test programming
Introduction to test programmingopenfinanceDev
 
Test-driven development & Behavior-driven development basics
Test-driven development & Behavior-driven development basicsTest-driven development & Behavior-driven development basics
Test-driven development & Behavior-driven development basicsOleksii Prohonnyi
 
Introduction to Test Driven Development
Introduction to Test Driven DevelopmentIntroduction to Test Driven Development
Introduction to Test Driven DevelopmentMichael Denomy
 
TDD - Test Driven Development
TDD - Test Driven DevelopmentTDD - Test Driven Development
TDD - Test Driven DevelopmentLim Chanmann
 
#DOAW16 - DevOps@work Roma 2016 - Testing your databases
#DOAW16 - DevOps@work Roma 2016 - Testing your databases#DOAW16 - DevOps@work Roma 2016 - Testing your databases
#DOAW16 - DevOps@work Roma 2016 - Testing your databasesAlessandro Alpi
 
Test-Driven Developments are Inefficient; Behavior-Driven Developments are a ...
Test-Driven Developments are Inefficient; Behavior-Driven Developments are a ...Test-Driven Developments are Inefficient; Behavior-Driven Developments are a ...
Test-Driven Developments are Inefficient; Behavior-Driven Developments are a ...Abdelkrim Boujraf
 
Software presentation
Software presentationSoftware presentation
Software presentationJennaPrengle
 
[Webinar] Test First, Fail Fast - Simplifying the Tester's Transition to DevOps
[Webinar] Test First, Fail Fast - Simplifying the Tester's Transition to DevOps[Webinar] Test First, Fail Fast - Simplifying the Tester's Transition to DevOps
[Webinar] Test First, Fail Fast - Simplifying the Tester's Transition to DevOpsKMS Technology
 
Test analysis & design good practices@TDT Iasi 17Oct2013
Test analysis & design   good practices@TDT Iasi 17Oct2013Test analysis & design   good practices@TDT Iasi 17Oct2013
Test analysis & design good practices@TDT Iasi 17Oct2013Tabăra de Testare
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@Alex Borsuk
 
TDD with Visual Studio 2010
TDD with Visual Studio 2010TDD with Visual Studio 2010
TDD with Visual Studio 2010Stefano Paluello
 

Ähnlich wie Test Driven Development (TDD) (20)

Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 
Test driven development v1.0
Test driven development v1.0Test driven development v1.0
Test driven development v1.0
 
Unit Testing, TDD and the Walking Skeleton
Unit Testing, TDD and the Walking SkeletonUnit Testing, TDD and the Walking Skeleton
Unit Testing, TDD and the Walking Skeleton
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In Action
 
Test Driven Development - Overview and Adoption
Test Driven Development - Overview and AdoptionTest Driven Development - Overview and Adoption
Test Driven Development - Overview and Adoption
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Introduction to test programming
Introduction to test programmingIntroduction to test programming
Introduction to test programming
 
Test-driven development & Behavior-driven development basics
Test-driven development & Behavior-driven development basicsTest-driven development & Behavior-driven development basics
Test-driven development & Behavior-driven development basics
 
Introduction to Test Driven Development
Introduction to Test Driven DevelopmentIntroduction to Test Driven Development
Introduction to Test Driven Development
 
TDD - Test Driven Development
TDD - Test Driven DevelopmentTDD - Test Driven Development
TDD - Test Driven Development
 
#DOAW16 - DevOps@work Roma 2016 - Testing your databases
#DOAW16 - DevOps@work Roma 2016 - Testing your databases#DOAW16 - DevOps@work Roma 2016 - Testing your databases
#DOAW16 - DevOps@work Roma 2016 - Testing your databases
 
Test-Driven Developments are Inefficient; Behavior-Driven Developments are a ...
Test-Driven Developments are Inefficient; Behavior-Driven Developments are a ...Test-Driven Developments are Inefficient; Behavior-Driven Developments are a ...
Test-Driven Developments are Inefficient; Behavior-Driven Developments are a ...
 
Testing 101
Testing 101Testing 101
Testing 101
 
Tdd
TddTdd
Tdd
 
Software presentation
Software presentationSoftware presentation
Software presentation
 
[Webinar] Test First, Fail Fast - Simplifying the Tester's Transition to DevOps
[Webinar] Test First, Fail Fast - Simplifying the Tester's Transition to DevOps[Webinar] Test First, Fail Fast - Simplifying the Tester's Transition to DevOps
[Webinar] Test First, Fail Fast - Simplifying the Tester's Transition to DevOps
 
Test analysis & design good practices@TDT Iasi 17Oct2013
Test analysis & design   good practices@TDT Iasi 17Oct2013Test analysis & design   good practices@TDT Iasi 17Oct2013
Test analysis & design good practices@TDT Iasi 17Oct2013
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@
 
JavaScript Unit Testing
JavaScript Unit TestingJavaScript Unit Testing
JavaScript Unit Testing
 
TDD with Visual Studio 2010
TDD with Visual Studio 2010TDD with Visual Studio 2010
TDD with Visual Studio 2010
 

Kürzlich hochgeladen

How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.Curtis Poe
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...itnewsafrica
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...itnewsafrica
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfIngrid Airi González
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxLoriGlavin3
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Strongerpanagenda
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024TopCSSGallery
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesManik S Magar
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 

Kürzlich hochgeladen (20)

How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.How AI, OpenAI, and ChatGPT impact business and software.
How AI, OpenAI, and ChatGPT impact business and software.
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...Abdul Kader Baba- Managing Cybersecurity Risks  and Compliance Requirements i...
Abdul Kader Baba- Managing Cybersecurity Risks and Compliance Requirements i...
 
Generative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdfGenerative Artificial Intelligence: How generative AI works.pdf
Generative Artificial Intelligence: How generative AI works.pdf
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptxThe Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
The Fit for Passkeys for Employee and Consumer Sign-ins: FIDO Paris Seminar.pptx
 
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better StrongerModern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
Modern Roaming for Notes and Nomad – Cheaper Faster Better Stronger
 
Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024Top 10 Hubspot Development Companies in 2024
Top 10 Hubspot Development Companies in 2024
 
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotesMuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
MuleSoft Online Meetup Group - B2B Crash Course: Release SparkNotes
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 

Test Driven Development (TDD)

  • 1. TEST DRIVEN DEVELOPMENT DAVID EHRINGER This work by David Ehringer is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License DAVIDEHRINGER.COM
  • 2. CONTENTS What Is TDD? TDD Principals Tools Live Coding Best Practices And Smells Other (Tips, Resources, BDD, Etc.)
  • 5. A PERSPECTIVE ON TDD TDD is Mostly about Design Gives Confidence Enables Change Is Automated Validates Your Design Is Documentation By Example As Well As an Up To Date, “Live”, And Executable Specification Provides Rapid Feedback Quality of Implementation Quality of Design Forces Constant Integration Requires More Discipline Brings Professionalism to Software Development Isn’t The Only Testing You’ll Need To Do Developers Write Tests
  • 6. RESULT Higher Quality Flexibility Readability Maintainability Predicability Simplicity The Hard Stuff And Surprises Are Tackled Early On Both Internal And External Quality Are Maintained A Well-Designed Application
  • 8. THE THREE LAWS OF TDD 1: You May Not Write Production Code Until You Have Written A Failing Unit (or Acceptance) Test 2: You May Not Write More Of A Unit (or Acceptance) Test Than Is Sufficient To Fail, And Compiling Is Failing 3: You May Not Write More Production Code Than Is Sufficient To Pass The Currently Failing Test Ever so slightly adapted from Professionalism and Test-Driven Development by Robert “Uncle Bob” C. Martin
  • 9. FUNDAMENTAL PRINCIPLES Think About What You Are Trying To Do Follow The TDD Cycle And The Three Laws Never Write New Functionality Without A Failing Test Continually Make Small, Incremental Changes Keep The System Running At All Times No One Can Make A Change That Breaks The System Failures Must Be Address Immediately In reality, unwavering adherence the cycle and laws can sometimes be very hard. In my opinion, it doesn’t always have to be all or nothing (especially when you are just learning). But I’ve found that when I don’t write tests first, I usually regret it afterwards. Also, TDD isn’t applicable for every single scenario or line of code.
  • 10. “TDD IN A NUTSHELL” As we develop the system, we use TDD to give us feedback on the quality of both its implementation (“Does it work?”) and design (“Is it well structured?”). Developing test-first, we find we benefit twice from the effort. Writing Tests: • makes us clarify the acceptance criteria for the next piece of work - we have to ask ourselves how we can tell when we’re done (Design); • encourages us to write loosely coupled components, so they can easily be tested in isolation and, at higher levels, combined together (Design); • adds an executable description of what the code does (Design); and, • adds to a complete regression suite (Implementation) whereas running tests: • detects errors while the context is fresh in our mind (Implementation); and, • lets us know when we’ve done enough, discouraging “gold plating” and unnecessary features (Design) From Growing Object-Oriented Software, Guided By Tests
  • 11. BUT HOW MUCH LONGER DOES TDD TAKE? “If it doesn’t have to work, I can get it done a lot faster!” - Kent Beck paraphrased My Experience - Initial Progress Will Be Slower - Greater Consistency - Less Experienced Developer Learning/Ramp-up Time - I personally think I can develop faster using TDD than without, but it took a while to get there - Long-term cost is drastically lower Studies - Takes 15-30% longer - 45-90% fewer bugs - Takes 10x as long to fix a bug in later phases - One study with Microsoft and IBM: http:// research.microsoft.com/en-us/projects/esm/ nagappan_tdd.pdf
  • 12. SOME DESIGN PRINCIPALS Loosely coupled, highly cohesive Dependency Injection/Inversion of Control (IOC) Single Responsibility Principal Strive for Simplicity Open/Closed Principal Law of Demeter (“Tell don’t ask.”) Modularization Separation of Concerns Encapsulation Favor Immutability Domain Driven Design Decentralized Management and Decision Making Strict Dependency Management Clean Code Ubiquitous Language Eliminate Duplication
  • 13. WHAT ABOUT ARCHITECTURE AND DESIGN? TDD Does Not Replace Architecture Or Design You Need To Do Up Front Design And Have A Vision But, You Should Not Do “Big Up Front Design” Defer Architectural Decisions As Long As Is “Responsibly” Possible TDD Will Inform And Validate (or Invalidate) Your Design Decisions TDD Will Almost Undoubtedly Uncover Weaknesses And Flaws In Your Design...Listen To Them!
  • 14. CATEGORIES OF TESTS Unit Integration Acceptance Can Encompass “system” Or “functional” Tests End-to-end Is Desirable When Feasible Learning Or Exploratory
  • 15. THE EXTENDED CYCLE From Growing Object-Oriented Software, Guided By Tests
  • 16. TOOLS OF THE TRADE Unit Test Framework Mocking Framework Automated Build Tool Acceptance Test Framework (often The Same As The Unit Test Framework) Source Code Management Build And Continuous Integration Servers Test Coverage Tools Code Quality Tools For Java-based Apps, Consider Using A Dynamic Language (e.g. Groovy) To Write Tests If You Are Comfortable With It
  • 17. TOOLS OF THE TRADE - RECOMMENDATIONS Unit Test Framework - JUnit 4, Hamcrest Matchers Are Recommended For Improved Readability And Expressiveness Mocking Framework - Mockito Automated Build Tool - Maven Acceptance Test Framework - Web Services: SoapUI, Web App: Selenium Source Code Management - Depends On The Project Needs Continuous Integration - Bamboo Is Nice, Should Be Simple And Easy To Use And Support Your Build Tool Test Coverage Tools - Cobertura Or Clover (soapUI Pro For Web Services) Code Quality Tools - PMD, FindBugs, Checkstyle
  • 18. SOME NOTES ON YOUR BUILD Should Be Fully Automated Should Run Anywhere And Be Decoupled From The IDE You Should Be Able To Build Your Artifact In One Command “mvn package” You Should Be Able To Run Your Unit Tests In One Command “mvn test” You Should Be Able To Run You Acceptance Tests In One Command “mvn integration-test” Test Coverage And Code Quality Tools Should Be Integrated Into The Build
  • 21. MOCKING Limit The Scope Of Your Test To The Object Under Test Mocks Mimic The Behavior Of Objects (the Dependencies Of Your Object Under Test) In Predictable Ways Mocks Contains Assertions Themselves Prefer Mocking Of Behavior Rather Than Data (strictly Stubs) Be Careful: Mocks Can Couple The Test To The Underlying Implementation Of The Code Being Tested
  • 22. TDD BY EXAMPLE Time For Some Live Coding
  • 23. BUT FIRST... A FEW MORE WORDS Iteration 0/Walking Skeleton Build, Deploy, Test End-to-end Includes Process As Well As Infrastructure
  • 24. THE EXAMPLE A Trivial Customer Management App Web-service, Business Logic, Database Starts With A Full, Walking Skeleton
  • 25. BACK TO THE EXAMPLES Starting With An Acceptance Test Unit Tests To Fill In The Details Refactoring Along The Way Components/Modules And Large Projects
  • 26. CODE COVERAGE AND QUALITY TOOLS
  • 29. SPEED: TRADITIONAL DEVELOPMENT • 5 mins per coding hour becomes 6000 minutes annually (2.5, 40-hour workweeks) • 9 mins per coding hour becomes 10800 minutes annually (4.5 weeks) • 14 mins per coding hour becomes 16800 mins annually (7 weeks) Survey by ZeroTurnaround http://www.zeroturnaround.com/blog/java-ee- container-redeploy-restart-turnaround-report/
  • 30. SPEED IS CRITICAL Developers Need Immediate Feedback Tests Must Run Fast Consider Using Jetty And HSQLDB For Automated Unit And Acceptance Testing Depending On Your Framework, It Can Be Easy To Use Jetty/ HSQLDB For Local Development And Automated Testing And Deploy To Websphere/Oracle In “Real” Environments Make Sure You Do Continuous And Early Integration
  • 31. BEST PRACTICES: CONSISTENCY Test Environments Should Be Scripted And Consistent Test Data Should Be Scripted And Consistent Database In-memory Databases Fast Reliable Easily Scriptable HSQLDB Messaging ActiveMQ Is A Good Alternative For Tests Simple To Set Up
  • 32. BEST PRACTICES: TEST NAMING AND STRUCTURE The Name Of The Test Should Describe A Feature Or Specification The Name Should Clearly Describe The Purpose Of The Test Frequently Read The Test Documentation Each Test Should Be For A Single Concept Strive For One Assertion Per Test Or As Few As Possible More Than One May Indicate More Than One Concept Is Being Tested What Does The Object Do, Not What Is The Object
  • 33. BEST PRACTICES Only Specify What Is Important Even Seemingly Trivial Tests Are Important Treat Your Test Code Like Production Code Maintain Refactor Expressiveness And Readability Of Tests Check In To Source Control Frequently Consistent Test Structure Setup/Execute/Verify/Teardown Given/When/Then
  • 34. BEST PRACTICES: MANAGING TEST DATA Objects Avoid Mocking Values Test Data Builders Persistent In-memory Database Scripted Data
  • 35. SMELLS Doing The Opposite Of The Best Practices :-) Just Making It Work Isn’t Good Enough Should Be Able To Run Tests Independently Tests Should Never Depend On Each Other
  • 36. TIPS: ECLIPSE SHORTCUTS RELATED TO TESTING Ctrl+1: Quick Fix Ctrl+Shift+X, T : Run As Unit Test (Use In Any Context: Method, Class, Package, Project, Etc.) F11: Launch Last Test F3: Open Declaration Alt+Shift+3: Refactoring Menu Alt+Shift+S: Code Completion/Generation/Format Menu Ctrl+e: Switch Between Open Windows Eclipse Static Import Favorites These are Windows specific. On Mac you usually, but not always, substitute Command for Ctrl
  • 37. TIPS: SOME OTHER ECLIPSE SHORTCUTS Ctrl-Space: Code Completion Ctrl-Shift-F: Format Code Ctrl-Shift-O: Organize Import Ctrl-Shift-T: Open Type Ctrl-Shift-R: Open Resource Ctrl-/: Toggle Comment Ctrl-Shift-W: Close All Open Windows Ctrl-N: New Wizard Ctrl-F7: Move Between Open Views Ctrl+Shift+L: Show Shortcuts Alt+left, Alt+right: Toggle Windows
  • 38. TDD CHALLENGES Discipline Is Required Developers Are Often Stubborn And Lazy It Is Hard For Developers To Drop Bad Habits Management Doesn’t Often Understand “Internal Quality”
  • 39. SOME RESOURCES RELATED TO TDD Should Be On Every Professional Developers Bookshelf Refactoring (Fowler) Domain Driven Design (Evans) Clean Code (“Uncle Bob” Martin) Recommended Growing Object-Oriented Software, Guided By Tests (Freeman, Pryce) Implementation Patterns (Beck) Haven’t Read But, Given The Authors, Should Be Good Test Driven Development: By Example (Beck) The Book That “Rediscovered” TDD XUnit Patterns (Meszaros) By No Means An Exhaustive List But Should Keep You Busy For A While
  • 40. BEHAVIOR DRIVEN DEVELOPMENT “TDD Done Right” Stories And Specifications Ubiquitous Language Given/When/Then Should/Ensure easyb (Groovy) Is Nice If You Are Working In The Java World