SlideShare uma empresa Scribd logo
1 de 29
Dennis van der Stelt

Introduction in the TDD Mantra
Session Code: NE.17
Introduction
• Class-A
–
–
–
–

Kennisprovider
Microsoft development
Training / Coaching
www.class-a.nl

• Dennis van der Stelt
– Trainer / Coach
– Weblog at http://bloggingabout.net/blogs/dennis/
Agenda
•
•
•
•
•
•
•
•

A quick introduction into unit testing
Test Driven Development mantra
Refactoring
Benefits of Test Driven Development
Demo
Key rules of Test Driven Development
Mocking / Stubbing
Legacy code
A quick introduction
Unit testing
1. Automated testing
2. Facilitates change
3. Simplifies integration
4. Documentation
unit testing isn’t about just testing code
it‘s about allowing you to
determine when behavior changes
TDD Mantra
RED

GREEN
I’M TEST-DRIVEN

•
•
•
•

Write the test
Write the code
Refactor
Repeat

REFACTOR
TDD Mantra
Where to begin?
• Work from specification (or notes, or napkin)
• Create a list of tests
• Pick the simplest one
• Make test, make code
• Refactor

• Find a bug? Create the test that would’ve
caught it!
Test Driven Development
...is not about testing.
• It’s about design
• It’s about unambigious requirements
• It’s also about simplicity and YAGNI
Simplicity is more complicated then you think. But it’s well worth it.
Ron Jeffries, Extreme Programming Installed, 2001
Test Driven Development
...is about simple design that
1.
2.
3.
4.

Runs all tests
Reveals all intention
Has no duplication
Has the fewest number of classes and methods
Test Driven Development
“The act of writing a unit test is more an act of design then of verification”

- Robert C. Martin
“Test-driven development seeks specification, not validation, letting you think
through your design before you write your functional code”

- Scott Ambler
“Test-Driven Development is a powerful way to produce well designed code
with fewer defects”

- Martin Fowler
“Fewer defects, less debugging, more confidence, better design, and higher
productivity in my programming practice”

- Kent Beck
Benefits of TDD (1/3)
Benefits on your design & code
• Exposes design flaws & invalid assumptions
• Simpler class hierarchies
• Less code, smaller functions
• Less conditional code
• Faster development time

• Tests are both design & documentation
Benefits of TDD (2/3)
Benefits on your tests & bugs
• Tests cover *everything*
• Bugs found earlier & faster
• Bugs reproduced more easily
• Less (difficult) debugging
Benefits of TDD (3/3)
Cost of change

Test-Driven Development embraces change!
Waterfall
Cons of TDD
•
•
•
•

Needs practice, but you’ll learn to love it!
Needs discipline
Need to know OODP
Scares away managers
Bowling game
First a quick design session…
Bowling game
0 4
4

2 4
10

4 3
17

4 + (2+4)

•
•
•
•

6
30

3 2
35

55

9
65

0 0
65

9 0
74

7 2
83

17 + (6+4+3) 35 + (10+9+1) + 1 + 0)
55 + (9

10 frames, each holds 2 throws
Spare: 10 pins 2nd try, next throw added extra
Strike: 10 pins in 1st try, score next frame added
10th frame spare or strike,
player can make extra balls to complete frame.
Quick design sessions
next frame

Frame

Game
+roll(pins : int)
+score() : int

The score for spare or strike
depends on the frame’s succesor

10

+score() : int

Roll
1..2

-pins : int

1

We clearlyfunction must A game has ten frames. A frame has 1 or 2 roles.
The score need the Game class.
iterate
through all the frames, and
calculate all their scores.
tenth frame

The tenth frame has two or three roles.
It is different from all the other frames.
Bowling game
Finally some code!
Key rules of TDD
A test is no unit test if…
• …it talks to a database
• …it communicates across the network
• …it touches the file system
• …it can’t run at the same time as other tests
• …you have to perform actions to run the tests
(ex. configuration change)
How to isolate your code
How to isolate your code

database

logging
service

remote
server
How to isolate your code
Mocks & Stubs
A mock or stub is a stunt double for…
• …an object outside your unit
• …an external object (database)
• …a complex object(s)
• …a not yet developed object

Mocking & Stubbing are different things!
Isolation framework
[TestMethod]
public void Does_AllowDependencyInjection_When_CreatingInstance()
{
// Arrange
var fake = Isolate.Fake.Instance<ICustomerRepository>();
Customer cust = new Customer();
// Act
BusinessAction ba = new BusinessAction(fake);
ba.PerformSomeSortOfAction(cust);

// Assert
Isolate.Verify.
WasCalledWithExactArguments(() => fake.SaveChanges(cust));
}
Legacy code
What is legacy code?
Wikipedia : Legacy code is source code that relates
to a no-longer supported or manufactured
operating system or other computer system.

Why is legacy code bad?
• The previous developer isn’t there to explain it.
• It takes time to learn the code.
• It’s not easy to change the code.
• You might break some part of the application
you didn’t even touch.
Legacy code
Now imagine...
Without a tests,
• You’re developer on unitproject,
What if you have unit tests? as we speak!
you’re writing legacy code
• You get sick for two weeks,
• Yourhave tests to explain the code
You code needs to be completed,
• Another developer comes in.
You have tests to learn the code
© Michael Feathers – Working Effectively with Legacy Code

You have tests to verify what you did works,
and did not break any other part of the
application.

- The previous developer isn’t there to explain it.
- It takes time to learn the code.
- It’s not easy to change the code.
- You might break some part of the application you didn’t even touch.
Conclusion:

WRITE UNIT TESTS!
and do it the right way using Test-Driven Development.
Thank you for your attention
• Dennis van der Stelt
– Dennis@BloggingAbout.NET
– http://twitter.com/dvdstelt/
– http://bloggingabout.net/blogs/dennis/
Resources to more information can be found here, incl. this slidedeck.
Evaluation form
Vul je evaluatieformulier in en maak kans
op een van de prachtige prijzen!!
Fill out your evaluation form and win one of
the great prizes!!

Session Code: NE.17

Mais conteúdo relacionado

Mais procurados

Testing and TDD - KoJUG
Testing and TDD - KoJUGTesting and TDD - KoJUG
Testing and TDD - KoJUGlburdz
 
2016 10-04: tdd++: tdd made easier
2016 10-04: tdd++: tdd made easier2016 10-04: tdd++: tdd made easier
2016 10-04: tdd++: tdd made easierChristian Hujer
 
Exception Handling
Exception Handling  Exception Handling
Exception Handling RatnaJava
 
Java Testing With Spock - Ken Sipe (Trexin Consulting)
Java Testing With Spock - Ken Sipe (Trexin Consulting)Java Testing With Spock - Ken Sipe (Trexin Consulting)
Java Testing With Spock - Ken Sipe (Trexin Consulting)jaxLondonConference
 
Test Drive Development
Test Drive DevelopmentTest Drive Development
Test Drive Developmentsatya sudheer
 
Clean Unit Test Patterns
Clean Unit Test PatternsClean Unit Test Patterns
Clean Unit Test PatternsFrank Appel
 
@LinkingNote annotation in YATSPEC
@LinkingNote annotation in YATSPEC@LinkingNote annotation in YATSPEC
@LinkingNote annotation in YATSPECWojciech Bulaty
 
A Brief Introduction to Test-Driven Development
A Brief Introduction to Test-Driven DevelopmentA Brief Introduction to Test-Driven Development
A Brief Introduction to Test-Driven DevelopmentShawn Jones
 
Test driven-development
Test driven-developmentTest driven-development
Test driven-developmentDavid Paluy
 
Clean code - Getting your R&D on board
Clean code - Getting your R&D on boardClean code - Getting your R&D on board
Clean code - Getting your R&D on boardRuth Sperer
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In ActionJon Kruger
 
Test Driven Development (C#)
Test Driven Development (C#)Test Driven Development (C#)
Test Driven Development (C#)Alan Dean
 
Grails unit testing
Grails unit testingGrails unit testing
Grails unit testingpleeps
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven DevelopmentConsulthinkspa
 
TDD CrashCourse Part3: TDD Techniques
TDD CrashCourse Part3: TDD TechniquesTDD CrashCourse Part3: TDD Techniques
TDD CrashCourse Part3: TDD TechniquesDavid Rodenas
 
Getting started with Test Driven Development
Getting started with Test Driven DevelopmentGetting started with Test Driven Development
Getting started with Test Driven DevelopmentFerdous Mahmud Shaon
 
TDD: Develop, Refactor and Release With Confidence
TDD: Develop, Refactor and Release With ConfidenceTDD: Develop, Refactor and Release With Confidence
TDD: Develop, Refactor and Release With ConfidenceMehdi Valikhani
 
Test driven development
Test driven developmentTest driven development
Test driven developmentMarcelo Cure
 
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...Thomas Weller
 

Mais procurados (20)

Testing and TDD - KoJUG
Testing and TDD - KoJUGTesting and TDD - KoJUG
Testing and TDD - KoJUG
 
2016 10-04: tdd++: tdd made easier
2016 10-04: tdd++: tdd made easier2016 10-04: tdd++: tdd made easier
2016 10-04: tdd++: tdd made easier
 
Exception Handling
Exception Handling  Exception Handling
Exception Handling
 
Java Testing With Spock - Ken Sipe (Trexin Consulting)
Java Testing With Spock - Ken Sipe (Trexin Consulting)Java Testing With Spock - Ken Sipe (Trexin Consulting)
Java Testing With Spock - Ken Sipe (Trexin Consulting)
 
Test Drive Development
Test Drive DevelopmentTest Drive Development
Test Drive Development
 
Clean Unit Test Patterns
Clean Unit Test PatternsClean Unit Test Patterns
Clean Unit Test Patterns
 
@LinkingNote annotation in YATSPEC
@LinkingNote annotation in YATSPEC@LinkingNote annotation in YATSPEC
@LinkingNote annotation in YATSPEC
 
A Brief Introduction to Test-Driven Development
A Brief Introduction to Test-Driven DevelopmentA Brief Introduction to Test-Driven Development
A Brief Introduction to Test-Driven Development
 
Exploratory testing workshop
Exploratory testing workshopExploratory testing workshop
Exploratory testing workshop
 
Test driven-development
Test driven-developmentTest driven-development
Test driven-development
 
Clean code - Getting your R&D on board
Clean code - Getting your R&D on boardClean code - Getting your R&D on board
Clean code - Getting your R&D on board
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In Action
 
Test Driven Development (C#)
Test Driven Development (C#)Test Driven Development (C#)
Test Driven Development (C#)
 
Grails unit testing
Grails unit testingGrails unit testing
Grails unit testing
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
TDD CrashCourse Part3: TDD Techniques
TDD CrashCourse Part3: TDD TechniquesTDD CrashCourse Part3: TDD Techniques
TDD CrashCourse Part3: TDD Techniques
 
Getting started with Test Driven Development
Getting started with Test Driven DevelopmentGetting started with Test Driven Development
Getting started with Test Driven Development
 
TDD: Develop, Refactor and Release With Confidence
TDD: Develop, Refactor and Release With ConfidenceTDD: Develop, Refactor and Release With Confidence
TDD: Develop, Refactor and Release With Confidence
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...
Introduction to testing with MSTest, Visual Studio, and Team Foundation Serve...
 

Destaque

Destaque (11)

Distributed Systems principles
Distributed Systems principlesDistributed Systems principles
Distributed Systems principles
 
LIDNUG : Reliable applications with messaging
LIDNUG : Reliable applications with messagingLIDNUG : Reliable applications with messaging
LIDNUG : Reliable applications with messaging
 
Building reliable applications with messaging
Building reliable applications with messagingBuilding reliable applications with messaging
Building reliable applications with messaging
 
Death of the batch job - NServiceBus Sagas
Death of the batch job - NServiceBus SagasDeath of the batch job - NServiceBus Sagas
Death of the batch job - NServiceBus Sagas
 
Een andere kijk op Microservices
Een andere kijk op MicroservicesEen andere kijk op Microservices
Een andere kijk op Microservices
 
Death of the batch job
Death of the batch jobDeath of the batch job
Death of the batch job
 
AppFabric Velocity
AppFabric VelocityAppFabric Velocity
AppFabric Velocity
 
Silverlight & WCF RIA
Silverlight & WCF RIASilverlight & WCF RIA
Silverlight & WCF RIA
 
Distributed Systems Design
Distributed Systems DesignDistributed Systems Design
Distributed Systems Design
 
Duplicating data or replicating data in Micro Services
Duplicating data or replicating data in Micro ServicesDuplicating data or replicating data in Micro Services
Duplicating data or replicating data in Micro Services
 
Distributed Systems Principles
Distributed Systems PrinciplesDistributed Systems Principles
Distributed Systems Principles
 

Semelhante a Test Driven Development

TDD - Seriously, try it! - Trójmiasto Java User Group (17th May '23)
TDD - Seriously, try it! - Trójmiasto Java User Group (17th May '23)TDD - Seriously, try it! - Trójmiasto Java User Group (17th May '23)
TDD - Seriously, try it! - Trójmiasto Java User Group (17th May '23)ssusercaf6c1
 
TDD - Seriously, try it! - Trjjmiasto JUG (17th May '23)
TDD - Seriously, try it! - Trjjmiasto JUG (17th May '23)TDD - Seriously, try it! - Trjjmiasto JUG (17th May '23)
TDD - Seriously, try it! - Trjjmiasto JUG (17th May '23)Nacho Cougil
 
TDD and Related Techniques for Non Developers (2012)
TDD and Related Techniques for Non Developers (2012)TDD and Related Techniques for Non Developers (2012)
TDD and Related Techniques for Non Developers (2012)Peter Kofler
 
TDD - Seriously, try it! - Bucarest Tech Week
TDD - Seriously, try it! - Bucarest Tech WeekTDD - Seriously, try it! - Bucarest Tech Week
TDD - Seriously, try it! - Bucarest Tech WeekNacho Cougil
 
TDD - Seriously, try it! (updated '22)
TDD - Seriously, try it! (updated '22)TDD - Seriously, try it! (updated '22)
TDD - Seriously, try it! (updated '22)Nacho Cougil
 
TDD - Seriously, try it! - Opensouthcode
TDD - Seriously, try it! - OpensouthcodeTDD - Seriously, try it! - Opensouthcode
TDD - Seriously, try it! - OpensouthcodeNacho Cougil
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven DevelopmentPablo Villar
 
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 2019Jason Tice
 
TDD - Christchurch APN May 2012
TDD - Christchurch APN May 2012TDD - Christchurch APN May 2012
TDD - Christchurch APN May 2012Alan Christensen
 
Test driven development v1.0
Test driven development v1.0Test driven development v1.0
Test driven development v1.0Ganesh Kondal
 
TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019Paulo Clavijo
 
Take Pride in Your Code - Test-Driven Development
Take Pride in Your Code - Test-Driven DevelopmentTake Pride in Your Code - Test-Driven Development
Take Pride in Your Code - Test-Driven DevelopmentBADR
 
Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...Codecamp Romania
 
Behaviour Driven Development and Thinking About Testing
Behaviour Driven Development and Thinking About TestingBehaviour Driven Development and Thinking About Testing
Behaviour Driven Development and Thinking About Testingdn
 
Unit Testing Best Practices
Unit Testing Best PracticesUnit Testing Best Practices
Unit Testing Best PracticesTomaš Maconko
 
Adopting tdd in the workplace
Adopting tdd in the workplaceAdopting tdd in the workplace
Adopting tdd in the workplaceDonny Wals
 
Adopting tdd in the workplace
Adopting tdd in the workplaceAdopting tdd in the workplace
Adopting tdd in the workplaceDonny Wals
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)Rob Hale
 

Semelhante a Test Driven Development (20)

TDD - Seriously, try it! - Trójmiasto Java User Group (17th May '23)
TDD - Seriously, try it! - Trójmiasto Java User Group (17th May '23)TDD - Seriously, try it! - Trójmiasto Java User Group (17th May '23)
TDD - Seriously, try it! - Trójmiasto Java User Group (17th May '23)
 
TDD - Seriously, try it! - Trjjmiasto JUG (17th May '23)
TDD - Seriously, try it! - Trjjmiasto JUG (17th May '23)TDD - Seriously, try it! - Trjjmiasto JUG (17th May '23)
TDD - Seriously, try it! - Trjjmiasto JUG (17th May '23)
 
TDD and Related Techniques for Non Developers (2012)
TDD and Related Techniques for Non Developers (2012)TDD and Related Techniques for Non Developers (2012)
TDD and Related Techniques for Non Developers (2012)
 
TDD - Seriously, try it! - Bucarest Tech Week
TDD - Seriously, try it! - Bucarest Tech WeekTDD - Seriously, try it! - Bucarest Tech Week
TDD - Seriously, try it! - Bucarest Tech Week
 
TDD - Seriously, try it! (updated '22)
TDD - Seriously, try it! (updated '22)TDD - Seriously, try it! (updated '22)
TDD - Seriously, try it! (updated '22)
 
TDD - Seriously, try it! - Opensouthcode
TDD - Seriously, try it! - OpensouthcodeTDD - Seriously, try it! - Opensouthcode
TDD - Seriously, try it! - Opensouthcode
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven Development
 
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
 
TDD - Christchurch APN May 2012
TDD - Christchurch APN May 2012TDD - Christchurch APN May 2012
TDD - Christchurch APN May 2012
 
Test driven development v1.0
Test driven development v1.0Test driven development v1.0
Test driven development v1.0
 
TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019TDD and Simple Design Workshop - Session 1 - March 2019
TDD and Simple Design Workshop - Session 1 - March 2019
 
Take Pride in Your Code - Test-Driven Development
Take Pride in Your Code - Test-Driven DevelopmentTake Pride in Your Code - Test-Driven Development
Take Pride in Your Code - Test-Driven Development
 
Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
Iasi code camp 20 april 2013 marian chicu - database unit tests in the sql se...
 
Behaviour Driven Development and Thinking About Testing
Behaviour Driven Development and Thinking About TestingBehaviour Driven Development and Thinking About Testing
Behaviour Driven Development and Thinking About Testing
 
Unit Testing Best Practices
Unit Testing Best PracticesUnit Testing Best Practices
Unit Testing Best Practices
 
Adopting tdd in the workplace
Adopting tdd in the workplaceAdopting tdd in the workplace
Adopting tdd in the workplace
 
Adopting tdd in the workplace
Adopting tdd in the workplaceAdopting tdd in the workplace
Adopting tdd in the workplace
 
Testing automaton
Testing automatonTesting automaton
Testing automaton
 
Testing Automaton - CFSummit 2016
Testing Automaton - CFSummit 2016Testing Automaton - CFSummit 2016
Testing Automaton - CFSummit 2016
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)
 

Mais de Dennis van der Stelt

Mais de Dennis van der Stelt (7)

Change your architecture during deployment
Change your architecture during deploymentChange your architecture during deployment
Change your architecture during deployment
 
Dealing with eventual consistency
Dealing with eventual consistencyDealing with eventual consistency
Dealing with eventual consistency
 
Dealing with eventual consistency
Dealing with eventual consistencyDealing with eventual consistency
Dealing with eventual consistency
 
Continuous integration
Continuous integrationContinuous integration
Continuous integration
 
App fabric introduction
App fabric introductionApp fabric introduction
App fabric introduction
 
SOLID Principles part 2
SOLID Principles part 2SOLID Principles part 2
SOLID Principles part 2
 
SOLID Principles part 1
SOLID Principles part 1SOLID Principles part 1
SOLID Principles part 1
 

Último

Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...gurkirankumar98700
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 

Último (20)

Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
Kalyanpur ) Call Girls in Lucknow Finest Escorts Service 🍸 8923113531 🎰 Avail...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

Test Driven Development

  • 1. Dennis van der Stelt Introduction in the TDD Mantra Session Code: NE.17
  • 2. Introduction • Class-A – – – – Kennisprovider Microsoft development Training / Coaching www.class-a.nl • Dennis van der Stelt – Trainer / Coach – Weblog at http://bloggingabout.net/blogs/dennis/
  • 3. Agenda • • • • • • • • A quick introduction into unit testing Test Driven Development mantra Refactoring Benefits of Test Driven Development Demo Key rules of Test Driven Development Mocking / Stubbing Legacy code
  • 4. A quick introduction Unit testing 1. Automated testing 2. Facilitates change 3. Simplifies integration 4. Documentation
  • 5. unit testing isn’t about just testing code it‘s about allowing you to determine when behavior changes
  • 6. TDD Mantra RED GREEN I’M TEST-DRIVEN • • • • Write the test Write the code Refactor Repeat REFACTOR
  • 7. TDD Mantra Where to begin? • Work from specification (or notes, or napkin) • Create a list of tests • Pick the simplest one • Make test, make code • Refactor • Find a bug? Create the test that would’ve caught it!
  • 8. Test Driven Development ...is not about testing. • It’s about design • It’s about unambigious requirements • It’s also about simplicity and YAGNI Simplicity is more complicated then you think. But it’s well worth it. Ron Jeffries, Extreme Programming Installed, 2001
  • 9. Test Driven Development ...is about simple design that 1. 2. 3. 4. Runs all tests Reveals all intention Has no duplication Has the fewest number of classes and methods
  • 10. Test Driven Development “The act of writing a unit test is more an act of design then of verification” - Robert C. Martin “Test-driven development seeks specification, not validation, letting you think through your design before you write your functional code” - Scott Ambler “Test-Driven Development is a powerful way to produce well designed code with fewer defects” - Martin Fowler “Fewer defects, less debugging, more confidence, better design, and higher productivity in my programming practice” - Kent Beck
  • 11. Benefits of TDD (1/3) Benefits on your design & code • Exposes design flaws & invalid assumptions • Simpler class hierarchies • Less code, smaller functions • Less conditional code • Faster development time • Tests are both design & documentation
  • 12. Benefits of TDD (2/3) Benefits on your tests & bugs • Tests cover *everything* • Bugs found earlier & faster • Bugs reproduced more easily • Less (difficult) debugging
  • 13. Benefits of TDD (3/3) Cost of change Test-Driven Development embraces change! Waterfall
  • 14. Cons of TDD • • • • Needs practice, but you’ll learn to love it! Needs discipline Need to know OODP Scares away managers
  • 15. Bowling game First a quick design session…
  • 16. Bowling game 0 4 4 2 4 10 4 3 17 4 + (2+4) • • • • 6 30 3 2 35 55 9 65 0 0 65 9 0 74 7 2 83 17 + (6+4+3) 35 + (10+9+1) + 1 + 0) 55 + (9 10 frames, each holds 2 throws Spare: 10 pins 2nd try, next throw added extra Strike: 10 pins in 1st try, score next frame added 10th frame spare or strike, player can make extra balls to complete frame.
  • 17. Quick design sessions next frame Frame Game +roll(pins : int) +score() : int The score for spare or strike depends on the frame’s succesor 10 +score() : int Roll 1..2 -pins : int 1 We clearlyfunction must A game has ten frames. A frame has 1 or 2 roles. The score need the Game class. iterate through all the frames, and calculate all their scores. tenth frame The tenth frame has two or three roles. It is different from all the other frames.
  • 19. Key rules of TDD A test is no unit test if… • …it talks to a database • …it communicates across the network • …it touches the file system • …it can’t run at the same time as other tests • …you have to perform actions to run the tests (ex. configuration change)
  • 20. How to isolate your code
  • 21. How to isolate your code database logging service remote server
  • 22. How to isolate your code
  • 23. Mocks & Stubs A mock or stub is a stunt double for… • …an object outside your unit • …an external object (database) • …a complex object(s) • …a not yet developed object Mocking & Stubbing are different things!
  • 24. Isolation framework [TestMethod] public void Does_AllowDependencyInjection_When_CreatingInstance() { // Arrange var fake = Isolate.Fake.Instance<ICustomerRepository>(); Customer cust = new Customer(); // Act BusinessAction ba = new BusinessAction(fake); ba.PerformSomeSortOfAction(cust); // Assert Isolate.Verify. WasCalledWithExactArguments(() => fake.SaveChanges(cust)); }
  • 25. Legacy code What is legacy code? Wikipedia : Legacy code is source code that relates to a no-longer supported or manufactured operating system or other computer system. Why is legacy code bad? • The previous developer isn’t there to explain it. • It takes time to learn the code. • It’s not easy to change the code. • You might break some part of the application you didn’t even touch.
  • 26. Legacy code Now imagine... Without a tests, • You’re developer on unitproject, What if you have unit tests? as we speak! you’re writing legacy code • You get sick for two weeks, • Yourhave tests to explain the code You code needs to be completed, • Another developer comes in. You have tests to learn the code © Michael Feathers – Working Effectively with Legacy Code You have tests to verify what you did works, and did not break any other part of the application. - The previous developer isn’t there to explain it. - It takes time to learn the code. - It’s not easy to change the code. - You might break some part of the application you didn’t even touch.
  • 27. Conclusion: WRITE UNIT TESTS! and do it the right way using Test-Driven Development.
  • 28. Thank you for your attention • Dennis van der Stelt – Dennis@BloggingAbout.NET – http://twitter.com/dvdstelt/ – http://bloggingabout.net/blogs/dennis/ Resources to more information can be found here, incl. this slidedeck.
  • 29. Evaluation form Vul je evaluatieformulier in en maak kans op een van de prachtige prijzen!! Fill out your evaluation form and win one of the great prizes!! Session Code: NE.17