SlideShare a Scribd company logo
An Intro to Test Driven
Development (TDD)
Rob Hale
April 11, 2016
Goals
• Automated testing isn’t (always) TDD
• Where it fits
• Moq
Automated Unit
Testing
What’s a “unit”
• The test operates in isolation
• tests should never depend on each other
Benefits of unit tests over end-to-end tests
• Error localization
• Execution time
• Tighter feedback loop
• Coverage
40x
What’s not a unit test?
I like Michael Feathers’ explanation from Working Effectively with Legacy Code.
It’s not a unit test if:
• It talks to a database
• It communicates across a network
• It touches the file system
• You have to do special things to your environment to run it (like editing a
config file)
DIFFICULT
SCENARIOS TO
UNIT TEST
Closed Object Models
(Sharepoint, Silverlight)
Client – server architecture
- Communicating Across a Network
An out-of-process call
- Includes talking to databases and
Web Services
UI/GUI interaction
Touching the File System
Legacy Code
Requires the Environment to be
configured
Test Driven
Development
The basic cycle
Write a
failing test
Make the
test pass
Refactor
Put another way...
New
require
ment
Run
Tests
Write
new
test
Write
new
test
Write
new
code
Run
Tests
Refact
or
Make it Better
Make it Fail
Make it Work
“When you first start at doing TDD you know what the design should be. You
know what code you want to write. So you write a test that will let you write that
bit of code.
“When you do this you aren't really doing TDD – since you are writing the code
first (even if the code is only in your head )
“It takes some time to realize that you need to focus on the test. Write the test
for the behavior you want - then write the minimal code needed to make it pass
- then let the design emerge through refactoring. Repeat until done.”
--Brian Rasmussen
http://www.slideshare.net/baronslideshare/testdriven-development-tdd
An Exercise in
Test Driven
Development
The idea here is going to be to go
through the exercise of designing a
portion of a new solution in real
time.
After the exercise I want to circle
back and point out the benefits we
gained from the test first approach
which may have been missed had
we coded and then wrote tests.
Principles
• Think About What You Are Trying To Do
• Follow The TDD Cycle
• Never Write New Functionality Without A
Failing Test
- Each test should be for a single
concept
• 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
• Is an activity of the developer
• Test code should be treated the same as
“production”
- maintained
- refactor
- source control
• Experimentation
- Names don’t matter initially
• When you want to change existing code,
first write covering, white box, unit tests
and then use TDD to add new
functionality
• Fix it in the simplest way possible using
as many assumptions as you like
TDD gives you
Design
Confidence
Documentation
Feedback
Realizing quality improvement through test driven development
Metric Description IBM:
Drivers
MSFT:
Windows
MSFT:
MSN
MSFT:
VS
Defect density of
non-TDD team
W X Y Z
Defect density of
team using TDD
0.61W 0.38X 0.24Y 0.09Z
% increase in
coding time
because
of TDD
[Management
estimates]
15-20% 25-35% 15% 25-30%
http://research.microsoft.com/en-us/groups/ese/nagappan_tdd.pdf
Visualized another way...
http://research.microsoft.com/en-us/groups/ese/nagappan_tdd.pdf
Moq
What’s a mock object?
A Test Double object that is pre-programmed with expectations which form a
specification of the calls they are expected to receive
The nice thing about using mocks while doing TDD is we can mock something
that doesn’t actually exist. Just define an interface and mock that. Later on we
can create concrete implementations of the interface (through TDD, of course)
Chicago vs London
Ledger
● Calculate(string expression)
Calculator
● Add
● Subtract
● Multiply
● ...
Based on http://programmers.stackexchange.com/a/123672
ledger.Calculate("5 * 7")
[TestFixture]
public class LedgerTests
{
public static Ledger Ledger { set; get; }
public static int Value { get; set; }
public static Mock<ICalculator> calculator;
[SetUp]
public void LedgerSetup()
{
calculator = new Mock<ICalculator>();
Ledger = new Ledger(calculator.Object);
}
Chicago vs London
[Test]
public void ChicagoStyle()
{
Value = Ledger.Calculate("5 * 7");
Assert.AreEqual(35, Value);
}
The Chicago/State school would have you assert whether the result is 35. The jUnit/nUnit frameworks are generally geared
towards doing this.
Chicago vs London
[Test]
public void ChicagoStyle()
{
Value = Ledger.Calculate("5 * 7");
Assert.AreEqual(35, Value);
}
The Chicago/State school would have you assert whether the result is 35. The jUnit/nUnit frameworks are generally geared
towards doing this.
[Test]
public void LondonStyle()
{
calculator.Verify(calc => calc.Multiply(5, 7), Times.Exactly(1));
}
The London/Interaction school would have you assert whether Calculator.Multiply(5,7) got called. The various
mocking frameworks are useful for this, and it can be very useful if, for example, you don't have ownership of the "Calculator"
object (suppose it is an external component or service that you cannot test directly, but you do know you have to call in a
particular way).
Resources
• http://www.slideshare.net/dehringer/test-driven-development-5785229
• http://www.slideshare.net/baronslideshare/testdriven-development-tdd
• http://research.microsoft.com/en-us/groups/ese/nagappan_tdd.pdf
• http://www.martinfowler.com/articles/mocksArentStubs.html
• Video: Llewellyn Falco & Woody Zuill - Practical Refactoring
• https://youtu.be/aWiwDdx_rdo
• Video: Raymond Lim & Roy Osherove - TDD Pairing Session 1
• https://youtu.be/xX9hfPkA800
• Video: Doc Norton - The Technical Debt Trap
• https://vimeo.com/97507576
Other resources mentioned by attendees
The following resources were mentioned after the presentation. They require
purchase or subscription.
• Play by Play: TDD with Brad Wilson (Pluralsight)
• https://www.pluralsight.com/courses/play-by-play-wilson-tdd
• Automated Testing for Fraidy Cats Like Me with Julie Lerman (Pluralsight)
• https://www.pluralsight.com/courses/automated-testing-fraidy-cats
• Test-Driven Development training by Mike Hill & Joshua Kerievsky (Industrial
Logic)
• https://elearning.industriallogic.com/gh/submit?Action=AlbumContentsAction&album=before
&devLanguage=Java

More Related Content

What's hot

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
Ahmed Shreef
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
guestc8093a6
 
TDD (Test Driven Design)
TDD (Test Driven Design)TDD (Test Driven Design)
TDD (Test Driven Design)
nedirtv
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven Development
Pablo Villar
 
Test-Driven Development (TDD)
Test-Driven Development (TDD)Test-Driven Development (TDD)
Test-Driven Development (TDD)
Brian Rasmussen
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Sachithra Gayan
 
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
guest5639fa9
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
John Blum
 
Scrum and Test-driven development
Scrum and Test-driven developmentScrum and Test-driven development
Scrum and Test-driven developmenttoteb5
 
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
 
Agile Programming Systems # TDD intro
Agile Programming Systems # TDD introAgile Programming Systems # TDD intro
Agile Programming Systems # TDD intro
Vitaliy Kulikov
 
PHPUnit - Unit testing
PHPUnit - Unit testingPHPUnit - Unit testing
PHPUnit - Unit testing
Nguyễn Đào Thiên Thư
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
guy_davis
 
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
Christian Hujer
 
Getting Unstuck: Working with Legacy Code and Data
Getting Unstuck: Working with Legacy Code and DataGetting Unstuck: Working with Legacy Code and Data
Getting Unstuck: Working with Legacy Code and Data
Cory Foy
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven DevelopmentDhaval Dalal
 
Unit testing legacy code
Unit testing legacy codeUnit testing legacy code
Unit testing legacy code
Lars Thorup
 
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)
CodeOps Technologies LLP
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Mireia Sangalo
 
Dependency Injection in iOS
Dependency Injection in iOSDependency Injection in iOS
Dependency Injection in iOS
Pablo Villar
 

What's hot (20)

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
 
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)
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven Development
 
Test-Driven Development (TDD)
Test-Driven Development (TDD)Test-Driven Development (TDD)
Test-Driven Development (TDD)
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
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
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 
Scrum and Test-driven development
Scrum and Test-driven developmentScrum and Test-driven development
Scrum and 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...
 
Agile Programming Systems # TDD intro
Agile Programming Systems # TDD introAgile Programming Systems # TDD intro
Agile Programming Systems # TDD intro
 
PHPUnit - Unit testing
PHPUnit - Unit testingPHPUnit - Unit testing
PHPUnit - Unit testing
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
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
 
Getting Unstuck: Working with Legacy Code and Data
Getting Unstuck: Working with Legacy Code and DataGetting Unstuck: Working with Legacy Code and Data
Getting Unstuck: Working with Legacy Code and Data
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Unit testing legacy code
Unit testing legacy codeUnit testing legacy code
Unit testing legacy code
 
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
 
Dependency Injection in iOS
Dependency Injection in iOSDependency Injection in iOS
Dependency Injection in iOS
 

Viewers also liked

ALM Practices - Devops para grandes Organizações
ALM Practices - Devops para grandes OrganizaçõesALM Practices - Devops para grandes Organizações
ALM Practices - Devops para grandes Organizações
especificacoes.com
 
DEV/ops Evolution & Revolution 2016
DEV/ops Evolution & Revolution 2016DEV/ops Evolution & Revolution 2016
DEV/ops Evolution & Revolution 2016
Brian Carpio
 
Test Driven Development - The art of fearless programming
Test Driven Development - The art of fearless programmingTest Driven Development - The art of fearless programming
Test Driven Development - The art of fearless programming
Chamil Jeewantha
 
Atdd half day_new_1_up
Atdd half day_new_1_upAtdd half day_new_1_up
Atdd half day_new_1_up
jaredrrichardson
 
Quality management in continuous delivery and dev ops world pm footprints v1
Quality management in continuous delivery and dev ops world  pm footprints v1Quality management in continuous delivery and dev ops world  pm footprints v1
Quality management in continuous delivery and dev ops world pm footprints v1
Dr. Anish Cheriyan (PhD)
 
Nbr 8800 lig sold_04
Nbr 8800 lig sold_04Nbr 8800 lig sold_04
Nbr 8800 lig sold_04
Edson Silva
 
Lightning Talk: Dev/Ops, Kaizen (lean) meets Agile Development
Lightning Talk: Dev/Ops, Kaizen (lean) meets Agile DevelopmentLightning Talk: Dev/Ops, Kaizen (lean) meets Agile Development
Lightning Talk: Dev/Ops, Kaizen (lean) meets Agile Development
Adam Clater
 
Soldagem 2009 2-emi
Soldagem 2009 2-emiSoldagem 2009 2-emi
Soldagem 2009 2-emi
Tadeu Granato
 
DevOps Overview
DevOps OverviewDevOps Overview
DevOps Overview
Omri Spector
 
Demystifiying dev ops
Demystifiying dev opsDemystifiying dev ops
Demystifiying dev ops
Organiq
 
Test Driven Development
Test Driven Development Test Driven Development
Test Driven Development
Nezir Yürekli
 
Home Depot - From Platform Ops to Dev Enablement
Home Depot - From Platform Ops to Dev EnablementHome Depot - From Platform Ops to Dev Enablement
Home Depot - From Platform Ops to Dev Enablement
Anthony McCulley
 
Test Driven Development (TDD) & Continuous Integration (CI)
Test Driven Development (TDD) & Continuous Integration (CI)Test Driven Development (TDD) & Continuous Integration (CI)
Test Driven Development (TDD) & Continuous Integration (CI)
Fatkul Amri
 
Dev ops
Dev opsDev ops
Dev ops with smell v1.2
Dev ops with smell v1.2Dev ops with smell v1.2
Dev ops with smell v1.2
Antons Kranga
 
Deliver Fast and Reliably with Dev Ops and Atlassian
Deliver Fast and Reliably with Dev Ops and AtlassianDeliver Fast and Reliably with Dev Ops and Atlassian
Deliver Fast and Reliably with Dev Ops and Atlassian
Xpand IT
 

Viewers also liked (16)

ALM Practices - Devops para grandes Organizações
ALM Practices - Devops para grandes OrganizaçõesALM Practices - Devops para grandes Organizações
ALM Practices - Devops para grandes Organizações
 
DEV/ops Evolution & Revolution 2016
DEV/ops Evolution & Revolution 2016DEV/ops Evolution & Revolution 2016
DEV/ops Evolution & Revolution 2016
 
Test Driven Development - The art of fearless programming
Test Driven Development - The art of fearless programmingTest Driven Development - The art of fearless programming
Test Driven Development - The art of fearless programming
 
Atdd half day_new_1_up
Atdd half day_new_1_upAtdd half day_new_1_up
Atdd half day_new_1_up
 
Quality management in continuous delivery and dev ops world pm footprints v1
Quality management in continuous delivery and dev ops world  pm footprints v1Quality management in continuous delivery and dev ops world  pm footprints v1
Quality management in continuous delivery and dev ops world pm footprints v1
 
Nbr 8800 lig sold_04
Nbr 8800 lig sold_04Nbr 8800 lig sold_04
Nbr 8800 lig sold_04
 
Lightning Talk: Dev/Ops, Kaizen (lean) meets Agile Development
Lightning Talk: Dev/Ops, Kaizen (lean) meets Agile DevelopmentLightning Talk: Dev/Ops, Kaizen (lean) meets Agile Development
Lightning Talk: Dev/Ops, Kaizen (lean) meets Agile Development
 
Soldagem 2009 2-emi
Soldagem 2009 2-emiSoldagem 2009 2-emi
Soldagem 2009 2-emi
 
DevOps Overview
DevOps OverviewDevOps Overview
DevOps Overview
 
Demystifiying dev ops
Demystifiying dev opsDemystifiying dev ops
Demystifiying dev ops
 
Test Driven Development
Test Driven Development Test Driven Development
Test Driven Development
 
Home Depot - From Platform Ops to Dev Enablement
Home Depot - From Platform Ops to Dev EnablementHome Depot - From Platform Ops to Dev Enablement
Home Depot - From Platform Ops to Dev Enablement
 
Test Driven Development (TDD) & Continuous Integration (CI)
Test Driven Development (TDD) & Continuous Integration (CI)Test Driven Development (TDD) & Continuous Integration (CI)
Test Driven Development (TDD) & Continuous Integration (CI)
 
Dev ops
Dev opsDev ops
Dev ops
 
Dev ops with smell v1.2
Dev ops with smell v1.2Dev ops with smell v1.2
Dev ops with smell v1.2
 
Deliver Fast and Reliably with Dev Ops and Atlassian
Deliver Fast and Reliably with Dev Ops and AtlassianDeliver Fast and Reliably with Dev Ops and Atlassian
Deliver Fast and Reliably with Dev Ops and Atlassian
 

Similar to VT.NET 20160411: An Intro to Test Driven Development (TDD)

A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)
Thierry Gayet
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Sergey Aganezov
 
Into The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applicationsInto The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applications
Ortus Solutions, Corp
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code clean
Brett Child
 
Advanced A/B Testing at Wix - Aviran Mordo and Sagy Rozman, Wix.com
Advanced A/B Testing at Wix - Aviran Mordo and Sagy Rozman, Wix.comAdvanced A/B Testing at Wix - Aviran Mordo and Sagy Rozman, Wix.com
Advanced A/B Testing at Wix - Aviran Mordo and Sagy Rozman, Wix.com
DevOpsDays Tel Aviv
 
Test Driven Development Introduction
Test Driven Development IntroductionTest Driven Development Introduction
Test Driven Development Introduction
Nguyen Hai
 
Test and Behaviour Driven Development (TDD/BDD)
Test and Behaviour Driven Development (TDD/BDD)Test and Behaviour Driven Development (TDD/BDD)
Test and Behaviour Driven Development (TDD/BDD)
Lars Thorup
 
Test Driven Development with Sql Server
Test Driven Development with Sql ServerTest Driven Development with Sql Server
Test Driven Development with Sql Server
David P. Moore
 
Improving The Quality of Existing Software
Improving The Quality of Existing SoftwareImproving The Quality of Existing Software
Improving The Quality of Existing Software
Steven Smith
 
Get Testing with tSQLt - SQL In The City Workshop 2014
Get Testing with tSQLt - SQL In The City Workshop 2014Get Testing with tSQLt - SQL In The City Workshop 2014
Get Testing with tSQLt - SQL In The City Workshop 2014
Red Gate Software
 
Unit testing
Unit testingUnit testing
Unit testing
Vinod Wilson
 
Unit tests and TDD
Unit tests and TDDUnit tests and TDD
Unit tests and TDD
Roman Okolovich
 
CNUG TDD June 2014
CNUG TDD June 2014CNUG TDD June 2014
CNUG TDD June 2014
Mayank Srivastava
 
Testing the Untestable
Testing the UntestableTesting the Untestable
Testing the Untestable
Mark Baker
 
Functional Tests. PHP Unconf 2016
Functional Tests. PHP Unconf 2016Functional Tests. PHP Unconf 2016
Functional Tests. PHP Unconf 2016
Vladislav Fedorischev
 
Test-Driven development; why you should never code without it
Test-Driven development; why you should never code without itTest-Driven development; why you should never code without it
Test-Driven development; why you should never code without it
Jad Salhani
 
TDD Agile Tour Beirut
TDD  Agile Tour BeirutTDD  Agile Tour Beirut
TDD Agile Tour Beirut
Agile Tour Beirut
 
Test driven development
Test driven developmentTest driven development
Test driven development
christoforosnalmpantis
 
Database continuous integration, unit test and functional test
Database continuous integration, unit test and functional testDatabase continuous integration, unit test and functional test
Database continuous integration, unit test and functional test
Harry Zheng
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven DevelopmentMeilan Ou
 

Similar to VT.NET 20160411: An Intro to Test Driven Development (TDD) (20)

A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Into The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applicationsInto The Box 2018 | Assert control over your legacy applications
Into The Box 2018 | Assert control over your legacy applications
 
Keeping code clean
Keeping code cleanKeeping code clean
Keeping code clean
 
Advanced A/B Testing at Wix - Aviran Mordo and Sagy Rozman, Wix.com
Advanced A/B Testing at Wix - Aviran Mordo and Sagy Rozman, Wix.comAdvanced A/B Testing at Wix - Aviran Mordo and Sagy Rozman, Wix.com
Advanced A/B Testing at Wix - Aviran Mordo and Sagy Rozman, Wix.com
 
Test Driven Development Introduction
Test Driven Development IntroductionTest Driven Development Introduction
Test Driven Development Introduction
 
Test and Behaviour Driven Development (TDD/BDD)
Test and Behaviour Driven Development (TDD/BDD)Test and Behaviour Driven Development (TDD/BDD)
Test and Behaviour Driven Development (TDD/BDD)
 
Test Driven Development with Sql Server
Test Driven Development with Sql ServerTest Driven Development with Sql Server
Test Driven Development with Sql Server
 
Improving The Quality of Existing Software
Improving The Quality of Existing SoftwareImproving The Quality of Existing Software
Improving The Quality of Existing Software
 
Get Testing with tSQLt - SQL In The City Workshop 2014
Get Testing with tSQLt - SQL In The City Workshop 2014Get Testing with tSQLt - SQL In The City Workshop 2014
Get Testing with tSQLt - SQL In The City Workshop 2014
 
Unit testing
Unit testingUnit testing
Unit testing
 
Unit tests and TDD
Unit tests and TDDUnit tests and TDD
Unit tests and TDD
 
CNUG TDD June 2014
CNUG TDD June 2014CNUG TDD June 2014
CNUG TDD June 2014
 
Testing the Untestable
Testing the UntestableTesting the Untestable
Testing the Untestable
 
Functional Tests. PHP Unconf 2016
Functional Tests. PHP Unconf 2016Functional Tests. PHP Unconf 2016
Functional Tests. PHP Unconf 2016
 
Test-Driven development; why you should never code without it
Test-Driven development; why you should never code without itTest-Driven development; why you should never code without it
Test-Driven development; why you should never code without it
 
TDD Agile Tour Beirut
TDD  Agile Tour BeirutTDD  Agile Tour Beirut
TDD Agile Tour Beirut
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Database continuous integration, unit test and functional test
Database continuous integration, unit test and functional testDatabase continuous integration, unit test and functional test
Database continuous integration, unit test and functional test
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 

Recently uploaded

SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Hivelance Technology
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
IES VE
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Visitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.appVisitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.app
NaapbooksPrivateLimi
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
Globus
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
Software Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdfSoftware Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdf
MayankTawar1
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
Tier1 app
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
Tendenci - The Open Source AMS (Association Management Software)
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
Globus
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 

Recently uploaded (20)

SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
Multiple Your Crypto Portfolio with the Innovative Features of Advanced Crypt...
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Visitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.appVisitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.app
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
Software Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdfSoftware Testing Exam imp Ques Notes.pdf
Software Testing Exam imp Ques Notes.pdf
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 

VT.NET 20160411: An Intro to Test Driven Development (TDD)

  • 1. An Intro to Test Driven Development (TDD) Rob Hale April 11, 2016
  • 2. Goals • Automated testing isn’t (always) TDD • Where it fits • Moq
  • 4. What’s a “unit” • The test operates in isolation • tests should never depend on each other
  • 5. Benefits of unit tests over end-to-end tests • Error localization • Execution time • Tighter feedback loop • Coverage
  • 6. 40x
  • 7. What’s not a unit test? I like Michael Feathers’ explanation from Working Effectively with Legacy Code. It’s not a unit test if: • It talks to a database • It communicates across a network • It touches the file system • You have to do special things to your environment to run it (like editing a config file)
  • 8. DIFFICULT SCENARIOS TO UNIT TEST Closed Object Models (Sharepoint, Silverlight) Client – server architecture - Communicating Across a Network An out-of-process call - Includes talking to databases and Web Services UI/GUI interaction Touching the File System Legacy Code Requires the Environment to be configured
  • 9. Test Driven Development The basic cycle Write a failing test Make the test pass Refactor
  • 11. “When you first start at doing TDD you know what the design should be. You know what code you want to write. So you write a test that will let you write that bit of code. “When you do this you aren't really doing TDD – since you are writing the code first (even if the code is only in your head ) “It takes some time to realize that you need to focus on the test. Write the test for the behavior you want - then write the minimal code needed to make it pass - then let the design emerge through refactoring. Repeat until done.” --Brian Rasmussen http://www.slideshare.net/baronslideshare/testdriven-development-tdd
  • 12. An Exercise in Test Driven Development The idea here is going to be to go through the exercise of designing a portion of a new solution in real time. After the exercise I want to circle back and point out the benefits we gained from the test first approach which may have been missed had we coded and then wrote tests.
  • 13. Principles • Think About What You Are Trying To Do • Follow The TDD Cycle • Never Write New Functionality Without A Failing Test - Each test should be for a single concept • 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 • Is an activity of the developer • Test code should be treated the same as “production” - maintained - refactor - source control • Experimentation - Names don’t matter initially • When you want to change existing code, first write covering, white box, unit tests and then use TDD to add new functionality • Fix it in the simplest way possible using as many assumptions as you like
  • 15. Realizing quality improvement through test driven development Metric Description IBM: Drivers MSFT: Windows MSFT: MSN MSFT: VS Defect density of non-TDD team W X Y Z Defect density of team using TDD 0.61W 0.38X 0.24Y 0.09Z % increase in coding time because of TDD [Management estimates] 15-20% 25-35% 15% 25-30% http://research.microsoft.com/en-us/groups/ese/nagappan_tdd.pdf
  • 17. Moq
  • 18. What’s a mock object? A Test Double object that is pre-programmed with expectations which form a specification of the calls they are expected to receive The nice thing about using mocks while doing TDD is we can mock something that doesn’t actually exist. Just define an interface and mock that. Later on we can create concrete implementations of the interface (through TDD, of course)
  • 19. Chicago vs London Ledger ● Calculate(string expression) Calculator ● Add ● Subtract ● Multiply ● ... Based on http://programmers.stackexchange.com/a/123672 ledger.Calculate("5 * 7")
  • 20. [TestFixture] public class LedgerTests { public static Ledger Ledger { set; get; } public static int Value { get; set; } public static Mock<ICalculator> calculator; [SetUp] public void LedgerSetup() { calculator = new Mock<ICalculator>(); Ledger = new Ledger(calculator.Object); }
  • 21. Chicago vs London [Test] public void ChicagoStyle() { Value = Ledger.Calculate("5 * 7"); Assert.AreEqual(35, Value); } The Chicago/State school would have you assert whether the result is 35. The jUnit/nUnit frameworks are generally geared towards doing this.
  • 22. Chicago vs London [Test] public void ChicagoStyle() { Value = Ledger.Calculate("5 * 7"); Assert.AreEqual(35, Value); } The Chicago/State school would have you assert whether the result is 35. The jUnit/nUnit frameworks are generally geared towards doing this. [Test] public void LondonStyle() { calculator.Verify(calc => calc.Multiply(5, 7), Times.Exactly(1)); } The London/Interaction school would have you assert whether Calculator.Multiply(5,7) got called. The various mocking frameworks are useful for this, and it can be very useful if, for example, you don't have ownership of the "Calculator" object (suppose it is an external component or service that you cannot test directly, but you do know you have to call in a particular way).
  • 23. Resources • http://www.slideshare.net/dehringer/test-driven-development-5785229 • http://www.slideshare.net/baronslideshare/testdriven-development-tdd • http://research.microsoft.com/en-us/groups/ese/nagappan_tdd.pdf • http://www.martinfowler.com/articles/mocksArentStubs.html • Video: Llewellyn Falco & Woody Zuill - Practical Refactoring • https://youtu.be/aWiwDdx_rdo • Video: Raymond Lim & Roy Osherove - TDD Pairing Session 1 • https://youtu.be/xX9hfPkA800 • Video: Doc Norton - The Technical Debt Trap • https://vimeo.com/97507576
  • 24. Other resources mentioned by attendees The following resources were mentioned after the presentation. They require purchase or subscription. • Play by Play: TDD with Brad Wilson (Pluralsight) • https://www.pluralsight.com/courses/play-by-play-wilson-tdd • Automated Testing for Fraidy Cats Like Me with Julie Lerman (Pluralsight) • https://www.pluralsight.com/courses/automated-testing-fraidy-cats • Test-Driven Development training by Mike Hill & Joshua Kerievsky (Industrial Logic) • https://elearning.industriallogic.com/gh/submit?Action=AlbumContentsAction&album=before &devLanguage=Java

Editor's Notes

  1. I’m coming at this as a fellow traveller - not an expert
  2. Understand the differences between automated testing and test driven development Understand where the value of test driven development lies Introduction to the Moq framework
  3. Error localization: “As tests get further from what they test, it’s harder to determine what a test failure means.” Michael Feathers Working Effectively with Legacy Code Execution time: Large tests take longer to run. Longer to run means not run as frequently (if at all) Tighter feedback loop: related to execution time - don’t have to wait as long to know if you’re code changes broke anything Coverage: It’s easier to test the different execution paths in a unit test. Think about testing a new feature in a large, existing end-to-end test versus adding a unit test to exercise it
  4. “Maintenance fixes and “small” code changes may be nearly 40 times more error prone than new development (Managing the Software Process, Watts S. Humphrey, Addison-Wesley, 1989)... By continuously running automated test cases, one can find out whether a change breaks the existing system early on, rather than leading to a late discovery.” - Realizing quality improvement through test driven development: results and experiences of four industrial teams, Nachiappan Nagappan & E. Michael Maximilien & Thirumalesh Bhat & Laurie Williams, Springer Science + Business Media, LLC, 2008
  5. Tests like these aren’t bad. There may be value in writing them. They just aren’t unit tests. Feathers’ definition of a unit test is driven by two properties: Does the test run fast? A unit test that takes 1/10th of a second is too long. 3,000 classes w/ 10 tests each at that speed is about an hour of test time Does it help localize errors quickly?
  6. Notice entry point is a new requirement not a new feature This is a tight loop - short iterations are important We’re only producing code needed for the current test The goal is to produce working clean code that fulfills the requirement
  7. Though poorly worded, the thrust of this quote is helpful.
  8. Add a comment for tests we want to circle back to that aren’t happy path (like in video where passing in coordinates that don’t correspond to a valid board position) Don’t do calculations in your tests b/c it often means you’re recreating a code calculation - you could just be recreating a bug but the test will pass Reminder - NUnit 2.6.4 - My version of R# won’t work with NUnit 3.x
  9. It’s mostly about design Confidence in changes Is documentation by example Immediate feedback
  10. Gerard Meszaros uses the term Test Double as the generic term for any kind of pretend object used in place of a real object for testing purposes. The name comes from the notion of a Stunt Double in movies. He then defined four particular kinds of double: Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists. Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an in memory database is a good example). Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test. Stubs may also record information about calls, such as an email gateway stub that remembers the messages it 'sent', or maybe only how many messages it 'sent'. Mocks are what we are talking about here: objects pre-programmed with expectations which form a specification of the calls they are expected to receive. Only mocks insist upon behavior verification.
  11. Suppose you have class called "ledger" a method called "calculate" that uses a "Calculator" to do different types of calculations depending on the arguments passed to "calculate", for example "multiply(x, y)" or "subtract(x, y)". Now, suppose you want to test what happens when you call ledger.calculate("5 * 7") The Chicago/State school would have you assert whether the result is 35. The jUnit/nUnit frameworks are generally geared towards doing this. The London/Interaction school would have you assert whether Calculator.multiply(5,7) got called. The various mocking frameworks are useful for this, and it can be very useful if, for example, you don't have ownership of the "Calculator" object (suppose it is an external component or service that you cannot test directly, but you do know you have to call in a particular way).
  12. This is the base test class. Don’t worry about the syntax of the mock… just understand what it is.
  13. The Chicago/State school would have you assert whether the result is 35. The jUnit/nUnit frameworks are generally geared towards doing this.
  14. The Chicago/State school would have you assert whether the result is 35. The jUnit/nUnit frameworks are generally geared towards doing this.