SlideShare ist ein Scribd-Unternehmen logo
1 von 40
Downloaden Sie, um offline zu lesen
Test Driven Development
Dhaval Dalal
software-artisan.com
@softwareartisan
What TDD is not?
TDD
is not
about Testing!
TDD
is not
Test Last
or
DDT
(Design Driven Tests)
What TDD is?
TDD
or
Test First
is about
evolving the design of the system through
Tests.
2
4
3 2
4
5
TDD Episode
[TestFixture]
public class OddNumberFilterTest {
[Test]
public void FiltersOutOddNumbers() {
OddNumberFilter filter = new OddNumberFilter();
int [] numbers = new int [] { 3, 4 };
int [] evenNumbers = filter.Filter(numbers);
Assert.That(evenNumbers, Has.Count(1));
Assert.That(evenNumbers, Has.Member(4));
Assert.That(evenNumbers, Has.No.Member(3));
}
}
dhaval.dalal@software-artisan.com
Specify What Software Should Do...
Then
When
Given
public class OddNumberFilter {
public int [] Filter (params int [] numbers)
{
throw new NotImplementedException();
}
}
dhaval.dalal@software-artisan.com
Write Just Enough Code To Compile
Red
dhaval.dalal@software-artisan.com
public class OddNumberFilter {
public int [] Filter (params int [] numbers)
{
List<int> evenNumbers = new List<int>();
foreach (int number in numbers)
{
if (number % 2 == 0)
{
evenNumbers.Add(number);
}
}
return evenNumbers.ToArray();
}
}
dhaval.dalal@software-artisan.com
Write Enough Code To Pass The
Specification...
Green
dhaval.dalal@software-artisan.com
dhaval.dalal@software-artisan.com
General Test Structure
Setup the Givens
(Context in which the Test runs)
Then verify the Assertions
(State or Behavior verifications)
Exercise the Whens
(Perform the actual operation)
Arrange
Assert
Act
[TestFixture]
public class NumberFilterTest {
[Test]
public void FiltersOddNumbers() {
OddNumberFilter filter = new OddNumberFilter();
int [] numbers = new int [] { 3, 4 };
int [] evenNumbers = filter.Filter(numbers);
Assert.That(evenNumbers, Has.Count(1));
Assert.That(evenNumbers, Has.Member(4));
Assert.That(evenNumbers, Has.No.Member(3));
}
dhaval.dalal@software-artisan.com
Add New Feature:
Filter-out Non-Primes
[TestFixture]
public class NumberFilterTest {
[Test]
public void FiltersOddNumbers() {
OddNumberFilter filter = new OddNumberFilter();
int [] numbers = new int [] { 3, 4 };
int [] evenNumbers = filter.Filter(numbers);
Assert.That(evenNumbers, Has.Count(1));
Assert.That(evenNumbers, Has.Member(4));
Assert.That(evenNumbers, Has.No.Member(3));
}
[Test]
public void FiltersNonPrimes() {
NonPrimesFilter filter = new NonPrimesFilter();
int [] numbers = new int [] { 4, 5 };
int [] primeNumbers = filter.Filter(numbers);
Assert.That(primeNumbers, Has.Count(1));
Assert.That(primeNumbers, Has.Member(5));
Assert.That(primeNumbers, Has.No.Member(4));
}
}
dhaval.dalal@software-artisan.com
Add New Feature:
Filter-out Non-Primes
[TestFixture]
public class NumberFilterTest {
[Test]
public void FiltersOddNumbers() {
OddNumberFilter filter = new OddNumberFilter();
int [] numbers = new int [] { 3, 4 };
int [] evenNumbers = filter.Filter(numbers);
Assert.That(evenNumbers, Has.Count(1));
Assert.That(evenNumbers, Has.Member(4));
Assert.That(evenNumbers, Has.No.Member(3));
}
[Test]
public void FiltersNonPrimes() {
NonPrimesFilter filter = new NonPrimesFilter();
int [] numbers = new int [] { 4, 5 };
int [] primeNumbers = filter.Filter(numbers);
Assert.That(primeNumbers, Has.Count(1));
Assert.That(primeNumbers, Has.Member(5));
Assert.That(primeNumbers, Has.No.Member(4));
}
}
dhaval.dalal@software-artisan.com
Add New Feature:
Filter-out Non-Primes
Duplication
of Concept
[TestFixture]
public class NumberFilterTest {
[Test]
public void FiltersOddNumbers() {
OddNumberFilter filter = new OddNumberFilter();
int [] numbers = new int [] { 3, 4 };
int [] evenNumbers = filter.Filter(numbers);
Assert.That(evenNumbers, Has.Count(1));
Assert.That(evenNumbers, Has.Member(4));
Assert.That(evenNumbers, Has.No.Member(3));
}
[Test]
public void FiltersNonPrimes() {
NonPrimesFilter filter = new NonPrimesFilter();
int [] numbers = new int [] { 4, 5 };
int [] primeNumbers = filter.Filter(numbers);
Assert.That(primeNumbers, Has.Count(1));
Assert.That(primeNumbers, Has.Member(5));
Assert.That(primeNumbers, Has.No.Member(4));
}
}
dhaval.dalal@software-artisan.com
Add New Feature:
Filter-out Non-Primes
Duplication
of Concept
Duplication
of Concept
Refactor
dhaval.dalal@software-artisan.com
public interface IFilter {
int [] filter.Filter(param int [] numbers);
}
dhaval.dalal@software-artisan.com
Introduce Abstraction
[TestFixture]
public class NumberFilterTest {
[Test]
public void FiltersOutOddNumbers() {
IFilter filter = new OddNumberFilter();
int [] numbers = new int [] { 3, 4 };
int [] evenNumbers = filter.Filter(numbers);
Assert.That(evenNumbers, Has.Count(1));
Assert.That(evenNumbers, Has.Member(4));
Assert.That(evenNumbers, Has.No.Member(3));
}
[Test]
public void FiltersOutNonPrimes() {
IFilter filter = new NonPrimesFilter();
int [] numbers = new int [] { 4, 5 };
int [] primeNumbers = filter.Filter(numbers);
Assert.That(primeNumbers, Has.Count(1));
Assert.That(primeNumbers, Has.Member(5));
Assert.That(primeNumbers, Has.No.Member(4));
}
}
dhaval.dalal@software-artisan.com
Refactored Code
Red
TDD Rhythm
Red Green
TDD Rhythm
Red Green
TDD Rhythm
Refactor
dhaval.dalal@software-artisan.com
TDD Rhythm Flowchart
Write just enough
Code to compile
Refactor Code
Pass the Test
(GREEN)
Fail the Test
(RED)
Write just enough
Code to pass the test
Specify what
the software
should do
Pass the Test
(GREEN)
So, TDD is about…
Analyzing what little you actually need to
do and how cleanly you can do it!
Carving design of your code a unit test at a
time.
dhaval.dalal@software-artisan.com
Write Tests Before Writing Code
! Focuses the mind (and the development
process)
" Deliver only what is absolutely necessary.
" System so developed does exactly what it
needs to do and no more.
" Need not code for future
! YAGNI (You Ain’t Gonna Need It!)...no gold plating!
dhaval.dalal@software-artisan.com
TDD is a Design Technique
! Makes you think in terms of Object
behavior.
" How client is going to interact with the object.
! Outside-In
! Object so created is “consumer aware”
" What Object provides and needs from
environment.
" Traditional OOD focuses only on Object’s
Implementation
! Inside-Out
dhaval.dalal@software-artisan.com
TDD results in a Decoupled Design
! Favors Composition over Inheritance.
" Relies on dependency injection for
collaborators.
! Avoids tight coupling to global objects
" Singletons mix static and state.
" Makes design untestable.
! Many small, loosely coupled classes.
! Makes you think of inter-object
interactions in terms of interfaces.
" Promotes Programming to Super-Types and not
Concretes.
Listening to the Tests
! A difficulty in implementing a new
feature is usually a symptom that the
design can be improved.
" Ask why is it difficult to test? Don’t just think
how do I test this?
! TDD is not just about functionality, it gives
us feedback on code’s internal quality.
" How is the coupling and cohesion?
" Are we preserving encapsulation?
" What about implicit or explicit dependencies?
Source: Growing Object-Oriented Software, Guided by Tests
Listening to the Tests
! Check continuously, observe and
meditate on the tests, they might tell you
something.
! The idea is to let the test drive the design.
That’s why it is called Test Driven Design.
Source: Growing Object-Oriented Software, Guided by Tests
dhaval.dalal@software-artisan.com
Benefits of TDD
! Test-a-little and build-a-little gives
confidence
" Green bar gives you confidence
" Reduces fear of change
! Documentation
" Provides starting point to understand code
functionality
! Safety Net
" Checks Regression
" Supports Refactoring
dhaval.dalal@software-artisan.com
Benefits of TDD
! Effort
" Reduces effort to final delivery
" Writing tests is more productive
! Predictable
" Tells me when am I done
" Continuous Success Vs Illusive Success
! Immediate Feedback
" Makes failures shine at you
dhaval.dalal@software-artisan.com
Costs of TDD
Claim: It is too much work to write tests!
Rebut: I’d say “are you looking to create a speculative
design?”, “do you want to sleep with a debugger?”
Claim: Tests themselves are code, we need to maintain them,
that’s overhead!
Rebut: I’d say “do you prefer maintaining a big bug list?”
Claim: I have to spend time re-orient my thinking!
Rebut: I’d say “Just as with any skill, you need to allow some
time to apply this effectively.”
Writing Good Tests
dhaval.dalal@software-artisan.com
Better Test Practices
! Tests must be Small.
" Easy to understand
" They do not break when other parts of the code are
changed.
" One behavioral-assert per test.
! Tests must be Expressive.
" Test code should communicate its intent.
" It should not take more than 2 minutes for readers to
understand what is going on.
! Tests must be Maintainable.
" When a test breaks, what it contains should be easiest to
fix.
dhaval.dalal@software-artisan.com
Better Test Practices
! Tests must execute Fast.
" Slow running tests increase Build Viscosity.
! Tests are Specifications, not Verifications.
" Do not verify whether the code does what its supposed
to do correctly.
" Specify what should the code do to function correctly.
! Tests should talk the Domain Language.
" Communicate the behavior under test and not how the
system implements that behavior.
" Improves Communication with Non-Technical Members
on the team.
" Developers can understand domain faster.
dhaval.dalal@software-artisan.com
Better Test Practices
! Tests must run at will.
" Able to write and execute tests without
worrying about how to execute them.
! Tests must be Isolated
" Very little set-up and minimum collaborators.
! Tests must be Thorough
" Test all behavior, not methods.
dhaval.dalal@software-artisan.com
Better Test Practices
! Tests must be Automated.
" Write them such that methods on objects are invoked by
code rather than by hand.
! Tests must be Self-Verifiable.
" Setup test expectations and compare outcome with
expectations for verdicts.
! Tests must be Repeatable.
" Executing the same test several times under the same
conditions must yield same results.
dhaval.dalal@software-artisan.com
JUnit/NUnit Better Test Practices
! Test anything that could possibly break.
! Make testing exceptional scenarios easy to read.
! Always explain failure reason in Assert calls.
! Test should usually improve the design of the
code.
! For JUnit Tests
" Make Test code reside in same packages, but
different directories.
! For NUnit Tests
" Make Test code reside in separate project from
source project, in same namespace.
dhaval.dalal@software-artisan.com
Flavors of Tests
! Object Tests (Unit or Programmer Tests)
" Tests behavior of single object at a time.
! Integration Tests
" Tests collaboration of a number of objects (how they talk
to each other)
" Complex fixtures
" More brittle
! End-to-End Tests
" Thoroughly test the entire system from end-to-end.
dhaval.dalal@software-artisan.com
Unit Testing Frameworks
! xUnit framework for Unit Testing
" JUnit/TestNG for Java
" NUnit/MbUnit for C#
" cppUnit for C++
" pyUnit for Python
" …and tons of more Unit Testing frameworks.
Thank you
dhaval.dalal@software-artisan.com
software-artisan.com
dhaval.dalal@software-artisan.com
References
! JUnit Recipes
" J. B. Rainsberger
! JUnit In Action
" Vincent Massol
! Agile Java
" Jeff Langr
! Test-Driven Development Rhythm
" Gunjan Doshi
! Agile Principles, Patterns, and
Practices in C#
" Robert C. Martin, Micah Martin
! xUnit Test Patterns
" Gerard Meszaros
! On TDD (InfoQ): How Do We
Know When We’re Done?
" Steve Freeman
! Growing Object-Oriented
Software, Guided by Tests
" Steve Freeman and Nat Pryce
! 10-Ways-to-Better-Code (InfoQ)
" Neal Ford
! Refactoring Away Duplicated Logic
Creates a Domain Specific
Embedded Language for Testing
" Nat Pryce
! Jay Field’s Blog Entry
" http://blog.jayfields.com/
2007/06/testing-inline-setup.html

Weitere ähnliche Inhalte

Was ist angesagt?

TDD Flow: The Mantra in Action
TDD Flow: The Mantra in ActionTDD Flow: The Mantra in Action
TDD Flow: The Mantra in ActionDionatan default
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development CodeOps Technologies LLP
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDDDror Helper
 
Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practicesnickokiss
 
Google test training
Google test trainingGoogle test training
Google test trainingThierry Gayet
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Developmentguestc8093a6
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesDerek Smith
 
Unit and integration Testing
Unit and integration TestingUnit and integration Testing
Unit and integration TestingDavid Berliner
 
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 2010guest5639fa9
 
Code coverage & tools
Code coverage & toolsCode coverage & tools
Code coverage & toolsRajesh Kumar
 
Acceptance Test Driven Development
Acceptance Test Driven DevelopmentAcceptance Test Driven Development
Acceptance Test Driven DevelopmentMike Douglas
 
API Test Automation Using Karate (Anil Kumar Moka)
API Test Automation Using Karate (Anil Kumar Moka)API Test Automation Using Karate (Anil Kumar Moka)
API Test Automation Using Karate (Anil Kumar Moka)Peter Thomas
 
Test Automation Framework using Cucumber BDD overview (part 1)
Test Automation Framework using Cucumber BDD overview (part 1)Test Automation Framework using Cucumber BDD overview (part 1)
Test Automation Framework using Cucumber BDD overview (part 1)Mindfire Solutions
 
Behavior driven development (bdd)
Behavior driven development (bdd)Behavior driven development (bdd)
Behavior driven development (bdd)Rohit Bisht
 
Unit Testing with Python
Unit Testing with PythonUnit Testing with Python
Unit Testing with PythonMicroPyramid .
 

Was ist angesagt? (20)

Tdd and bdd
Tdd and bddTdd and bdd
Tdd and bdd
 
TDD Flow: The Mantra in Action
TDD Flow: The Mantra in ActionTDD Flow: The Mantra in Action
TDD Flow: The Mantra in Action
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development
 
TDD refresher
TDD refresherTDD refresher
TDD refresher
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
 
Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practices
 
Google test training
Google test trainingGoogle test training
Google test training
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
 
Unit and integration Testing
Unit and integration TestingUnit and integration Testing
Unit and integration Testing
 
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
 
Code coverage & tools
Code coverage & toolsCode coverage & tools
Code coverage & tools
 
Unit Testing in Java
Unit Testing in JavaUnit Testing in Java
Unit Testing in Java
 
Acceptance Test Driven Development
Acceptance Test Driven DevelopmentAcceptance Test Driven Development
Acceptance Test Driven Development
 
API Test Automation Using Karate (Anil Kumar Moka)
API Test Automation Using Karate (Anil Kumar Moka)API Test Automation Using Karate (Anil Kumar Moka)
API Test Automation Using Karate (Anil Kumar Moka)
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Test Automation Framework using Cucumber BDD overview (part 1)
Test Automation Framework using Cucumber BDD overview (part 1)Test Automation Framework using Cucumber BDD overview (part 1)
Test Automation Framework using Cucumber BDD overview (part 1)
 
Behavior driven development (bdd)
Behavior driven development (bdd)Behavior driven development (bdd)
Behavior driven development (bdd)
 
Unit Testing with Python
Unit Testing with PythonUnit Testing with Python
Unit Testing with Python
 
Sanity testing and smoke testing
Sanity testing and smoke testingSanity testing and smoke testing
Sanity testing and smoke testing
 

Andere mochten auch

Visual Studio tricks every dot net developer should know
Visual Studio tricks every dot net developer should knowVisual Studio tricks every dot net developer should know
Visual Studio tricks every dot net developer should knowDror Helper
 
Domain languageacceptancetests
Domain languageacceptancetestsDomain languageacceptancetests
Domain languageacceptancetestsDhaval Dalal
 
The tao-of-transformation
The tao-of-transformationThe tao-of-transformation
The tao-of-transformationDhaval Dalal
 
The tao-of-transformation-workshop
The tao-of-transformation-workshopThe tao-of-transformation-workshop
The tao-of-transformation-workshopDhaval Dalal
 
Agile Methodologies
Agile MethodologiesAgile Methodologies
Agile MethodologiesDhaval Dalal
 
DRYing to Monad in Java8
DRYing to Monad in Java8DRYing to Monad in Java8
DRYing to Monad in Java8Dhaval Dalal
 

Andere mochten auch (7)

Visual Studio tricks every dot net developer should know
Visual Studio tricks every dot net developer should knowVisual Studio tricks every dot net developer should know
Visual Studio tricks every dot net developer should know
 
Domain languageacceptancetests
Domain languageacceptancetestsDomain languageacceptancetests
Domain languageacceptancetests
 
The tao-of-transformation
The tao-of-transformationThe tao-of-transformation
The tao-of-transformation
 
Real Developers Don't Need Unit Tests
Real Developers Don't Need Unit TestsReal Developers Don't Need Unit Tests
Real Developers Don't Need Unit Tests
 
The tao-of-transformation-workshop
The tao-of-transformation-workshopThe tao-of-transformation-workshop
The tao-of-transformation-workshop
 
Agile Methodologies
Agile MethodologiesAgile Methodologies
Agile Methodologies
 
DRYing to Monad in Java8
DRYing to Monad in Java8DRYing to Monad in Java8
DRYing to Monad in Java8
 

Ähnlich wie Test Driven Development

Getting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud ShaonGetting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud ShaonCefalo
 
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 is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Gianluca Padovani
 
TDD - survival guide
TDD - survival guide TDD - survival guide
TDD - survival guide vitalipe
 
TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012Pietro Di Bello
 
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 programmingChamil Jeewantha
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven DevelopmentPablo Villar
 
Programming with GUTs
Programming with GUTsProgramming with GUTs
Programming with GUTscatherinewall
 
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
 
Bdd and-testing
Bdd and-testingBdd and-testing
Bdd and-testingmalcolmt
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven DevelopmentMeilan Ou
 
Automated Unit Testing and TDD
Automated Unit Testing and TDDAutomated Unit Testing and TDD
Automated Unit Testing and TDDGreg Sohl
 

Ähnlich wie Test Driven Development (20)

TDD Best Practices
TDD Best PracticesTDD Best Practices
TDD Best Practices
 
Getting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud ShaonGetting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud Shaon
 
Getting started with Test Driven Development
Getting started with Test Driven DevelopmentGetting started with Test Driven Development
Getting started with Test Driven Development
 
Tdd is not about testing (OOP)
Tdd is not about testing (OOP)Tdd is not about testing (OOP)
Tdd is not about testing (OOP)
 
TDD - survival guide
TDD - survival guide TDD - survival guide
TDD - survival guide
 
TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012
 
JavaScript Unit Testing
JavaScript Unit TestingJavaScript Unit Testing
JavaScript Unit Testing
 
TDD talk
TDD talkTDD talk
TDD talk
 
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
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven Development
 
Programming with GUTs
Programming with GUTsProgramming with GUTs
Programming with GUTs
 
BDD Primer
BDD PrimerBDD Primer
BDD Primer
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
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
 
Bdd and-testing
Bdd and-testingBdd and-testing
Bdd and-testing
 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
 
Why unit testingl
Why unit testinglWhy unit testingl
Why unit testingl
 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 
Automated Unit Testing and TDD
Automated Unit Testing and TDDAutomated Unit Testing and TDD
Automated Unit Testing and TDD
 

Mehr von Dhaval Dalal

Test Pyramid in Microservices Context
Test Pyramid in Microservices ContextTest Pyramid in Microservices Context
Test Pyramid in Microservices ContextDhaval Dalal
 
Booting into functional programming
Booting into functional programmingBooting into functional programming
Booting into functional programmingDhaval Dalal
 
Currying and Partial Function Application (PFA)
Currying and Partial Function Application (PFA)Currying and Partial Function Application (PFA)
Currying and Partial Function Application (PFA)Dhaval Dalal
 
Creating Lazy stream in CSharp
Creating Lazy stream in CSharpCreating Lazy stream in CSharp
Creating Lazy stream in CSharpDhaval Dalal
 
Json Viewer Stories
Json Viewer StoriesJson Viewer Stories
Json Viewer StoriesDhaval Dalal
 
Mars rover-extension
Mars rover-extensionMars rover-extension
Mars rover-extensionDhaval Dalal
 
How Is Homeopathy Near To Yoga?
How Is Homeopathy Near To Yoga?How Is Homeopathy Near To Yoga?
How Is Homeopathy Near To Yoga?Dhaval Dalal
 
Approaching ATDD/BDD
Approaching ATDD/BDDApproaching ATDD/BDD
Approaching ATDD/BDDDhaval Dalal
 
Paradigms Code jugalbandi
Paradigms Code jugalbandiParadigms Code jugalbandi
Paradigms Code jugalbandiDhaval Dalal
 
Data Reconciliation
Data ReconciliationData Reconciliation
Data ReconciliationDhaval Dalal
 
4-Code-Jugalbandi-destructuring-patternmatching-healthycode#apr2015
4-Code-Jugalbandi-destructuring-patternmatching-healthycode#apr20154-Code-Jugalbandi-destructuring-patternmatching-healthycode#apr2015
4-Code-Jugalbandi-destructuring-patternmatching-healthycode#apr2015Dhaval Dalal
 
Jumping-with-java8
Jumping-with-java8Jumping-with-java8
Jumping-with-java8Dhaval Dalal
 
3-CodeJugalbandi-currying-pfa-healthycodemagazine#mar2015
3-CodeJugalbandi-currying-pfa-healthycodemagazine#mar20153-CodeJugalbandi-currying-pfa-healthycodemagazine#mar2015
3-CodeJugalbandi-currying-pfa-healthycodemagazine#mar2015Dhaval Dalal
 
CodeJugalbandi-Sequencing-HealthyCode-Magazine-Feb-2015
CodeJugalbandi-Sequencing-HealthyCode-Magazine-Feb-2015CodeJugalbandi-Sequencing-HealthyCode-Magazine-Feb-2015
CodeJugalbandi-Sequencing-HealthyCode-Magazine-Feb-2015Dhaval Dalal
 
CodeJugalbandi-Expression-Problem-HealthyCode-Magazine#Jan-2015-Issue
CodeJugalbandi-Expression-Problem-HealthyCode-Magazine#Jan-2015-IssueCodeJugalbandi-Expression-Problem-HealthyCode-Magazine#Jan-2015-Issue
CodeJugalbandi-Expression-Problem-HealthyCode-Magazine#Jan-2015-IssueDhaval Dalal
 
Grooming with Groovy
Grooming with GroovyGrooming with Groovy
Grooming with GroovyDhaval Dalal
 
Language portfolio
Language portfolioLanguage portfolio
Language portfolioDhaval Dalal
 

Mehr von Dhaval Dalal (20)

Test Pyramid in Microservices Context
Test Pyramid in Microservices ContextTest Pyramid in Microservices Context
Test Pyramid in Microservices Context
 
Code Retreat
Code RetreatCode Retreat
Code Retreat
 
Booting into functional programming
Booting into functional programmingBooting into functional programming
Booting into functional programming
 
Currying and Partial Function Application (PFA)
Currying and Partial Function Application (PFA)Currying and Partial Function Application (PFA)
Currying and Partial Function Application (PFA)
 
Creating Lazy stream in CSharp
Creating Lazy stream in CSharpCreating Lazy stream in CSharp
Creating Lazy stream in CSharp
 
Json Viewer Stories
Json Viewer StoriesJson Viewer Stories
Json Viewer Stories
 
Value Objects
Value ObjectsValue Objects
Value Objects
 
Mars rover-extension
Mars rover-extensionMars rover-extension
Mars rover-extension
 
How Is Homeopathy Near To Yoga?
How Is Homeopathy Near To Yoga?How Is Homeopathy Near To Yoga?
How Is Homeopathy Near To Yoga?
 
Approaching ATDD/BDD
Approaching ATDD/BDDApproaching ATDD/BDD
Approaching ATDD/BDD
 
Paradigms Code jugalbandi
Paradigms Code jugalbandiParadigms Code jugalbandi
Paradigms Code jugalbandi
 
Data Reconciliation
Data ReconciliationData Reconciliation
Data Reconciliation
 
CodeRetreat
CodeRetreatCodeRetreat
CodeRetreat
 
4-Code-Jugalbandi-destructuring-patternmatching-healthycode#apr2015
4-Code-Jugalbandi-destructuring-patternmatching-healthycode#apr20154-Code-Jugalbandi-destructuring-patternmatching-healthycode#apr2015
4-Code-Jugalbandi-destructuring-patternmatching-healthycode#apr2015
 
Jumping-with-java8
Jumping-with-java8Jumping-with-java8
Jumping-with-java8
 
3-CodeJugalbandi-currying-pfa-healthycodemagazine#mar2015
3-CodeJugalbandi-currying-pfa-healthycodemagazine#mar20153-CodeJugalbandi-currying-pfa-healthycodemagazine#mar2015
3-CodeJugalbandi-currying-pfa-healthycodemagazine#mar2015
 
CodeJugalbandi-Sequencing-HealthyCode-Magazine-Feb-2015
CodeJugalbandi-Sequencing-HealthyCode-Magazine-Feb-2015CodeJugalbandi-Sequencing-HealthyCode-Magazine-Feb-2015
CodeJugalbandi-Sequencing-HealthyCode-Magazine-Feb-2015
 
CodeJugalbandi-Expression-Problem-HealthyCode-Magazine#Jan-2015-Issue
CodeJugalbandi-Expression-Problem-HealthyCode-Magazine#Jan-2015-IssueCodeJugalbandi-Expression-Problem-HealthyCode-Magazine#Jan-2015-Issue
CodeJugalbandi-Expression-Problem-HealthyCode-Magazine#Jan-2015-Issue
 
Grooming with Groovy
Grooming with GroovyGrooming with Groovy
Grooming with Groovy
 
Language portfolio
Language portfolioLanguage portfolio
Language portfolio
 

Kürzlich hochgeladen

Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
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
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
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
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
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
 
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
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
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
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 

Kürzlich hochgeladen (20)

Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
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...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
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
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
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
 
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
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
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...
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 

Test Driven Development

  • 1. Test Driven Development Dhaval Dalal software-artisan.com @softwareartisan
  • 2. What TDD is not? TDD is not about Testing! TDD is not Test Last or DDT (Design Driven Tests)
  • 3. What TDD is? TDD or Test First is about evolving the design of the system through Tests.
  • 5. [TestFixture] public class OddNumberFilterTest { [Test] public void FiltersOutOddNumbers() { OddNumberFilter filter = new OddNumberFilter(); int [] numbers = new int [] { 3, 4 }; int [] evenNumbers = filter.Filter(numbers); Assert.That(evenNumbers, Has.Count(1)); Assert.That(evenNumbers, Has.Member(4)); Assert.That(evenNumbers, Has.No.Member(3)); } } dhaval.dalal@software-artisan.com Specify What Software Should Do... Then When Given
  • 6. public class OddNumberFilter { public int [] Filter (params int [] numbers) { throw new NotImplementedException(); } } dhaval.dalal@software-artisan.com Write Just Enough Code To Compile
  • 8. public class OddNumberFilter { public int [] Filter (params int [] numbers) { List<int> evenNumbers = new List<int>(); foreach (int number in numbers) { if (number % 2 == 0) { evenNumbers.Add(number); } } return evenNumbers.ToArray(); } } dhaval.dalal@software-artisan.com Write Enough Code To Pass The Specification...
  • 10. dhaval.dalal@software-artisan.com General Test Structure Setup the Givens (Context in which the Test runs) Then verify the Assertions (State or Behavior verifications) Exercise the Whens (Perform the actual operation) Arrange Assert Act
  • 11. [TestFixture] public class NumberFilterTest { [Test] public void FiltersOddNumbers() { OddNumberFilter filter = new OddNumberFilter(); int [] numbers = new int [] { 3, 4 }; int [] evenNumbers = filter.Filter(numbers); Assert.That(evenNumbers, Has.Count(1)); Assert.That(evenNumbers, Has.Member(4)); Assert.That(evenNumbers, Has.No.Member(3)); } dhaval.dalal@software-artisan.com Add New Feature: Filter-out Non-Primes
  • 12. [TestFixture] public class NumberFilterTest { [Test] public void FiltersOddNumbers() { OddNumberFilter filter = new OddNumberFilter(); int [] numbers = new int [] { 3, 4 }; int [] evenNumbers = filter.Filter(numbers); Assert.That(evenNumbers, Has.Count(1)); Assert.That(evenNumbers, Has.Member(4)); Assert.That(evenNumbers, Has.No.Member(3)); } [Test] public void FiltersNonPrimes() { NonPrimesFilter filter = new NonPrimesFilter(); int [] numbers = new int [] { 4, 5 }; int [] primeNumbers = filter.Filter(numbers); Assert.That(primeNumbers, Has.Count(1)); Assert.That(primeNumbers, Has.Member(5)); Assert.That(primeNumbers, Has.No.Member(4)); } } dhaval.dalal@software-artisan.com Add New Feature: Filter-out Non-Primes
  • 13. [TestFixture] public class NumberFilterTest { [Test] public void FiltersOddNumbers() { OddNumberFilter filter = new OddNumberFilter(); int [] numbers = new int [] { 3, 4 }; int [] evenNumbers = filter.Filter(numbers); Assert.That(evenNumbers, Has.Count(1)); Assert.That(evenNumbers, Has.Member(4)); Assert.That(evenNumbers, Has.No.Member(3)); } [Test] public void FiltersNonPrimes() { NonPrimesFilter filter = new NonPrimesFilter(); int [] numbers = new int [] { 4, 5 }; int [] primeNumbers = filter.Filter(numbers); Assert.That(primeNumbers, Has.Count(1)); Assert.That(primeNumbers, Has.Member(5)); Assert.That(primeNumbers, Has.No.Member(4)); } } dhaval.dalal@software-artisan.com Add New Feature: Filter-out Non-Primes Duplication of Concept
  • 14. [TestFixture] public class NumberFilterTest { [Test] public void FiltersOddNumbers() { OddNumberFilter filter = new OddNumberFilter(); int [] numbers = new int [] { 3, 4 }; int [] evenNumbers = filter.Filter(numbers); Assert.That(evenNumbers, Has.Count(1)); Assert.That(evenNumbers, Has.Member(4)); Assert.That(evenNumbers, Has.No.Member(3)); } [Test] public void FiltersNonPrimes() { NonPrimesFilter filter = new NonPrimesFilter(); int [] numbers = new int [] { 4, 5 }; int [] primeNumbers = filter.Filter(numbers); Assert.That(primeNumbers, Has.Count(1)); Assert.That(primeNumbers, Has.Member(5)); Assert.That(primeNumbers, Has.No.Member(4)); } } dhaval.dalal@software-artisan.com Add New Feature: Filter-out Non-Primes Duplication of Concept Duplication of Concept
  • 16. public interface IFilter { int [] filter.Filter(param int [] numbers); } dhaval.dalal@software-artisan.com Introduce Abstraction
  • 17. [TestFixture] public class NumberFilterTest { [Test] public void FiltersOutOddNumbers() { IFilter filter = new OddNumberFilter(); int [] numbers = new int [] { 3, 4 }; int [] evenNumbers = filter.Filter(numbers); Assert.That(evenNumbers, Has.Count(1)); Assert.That(evenNumbers, Has.Member(4)); Assert.That(evenNumbers, Has.No.Member(3)); } [Test] public void FiltersOutNonPrimes() { IFilter filter = new NonPrimesFilter(); int [] numbers = new int [] { 4, 5 }; int [] primeNumbers = filter.Filter(numbers); Assert.That(primeNumbers, Has.Count(1)); Assert.That(primeNumbers, Has.Member(5)); Assert.That(primeNumbers, Has.No.Member(4)); } } dhaval.dalal@software-artisan.com Refactored Code
  • 21. dhaval.dalal@software-artisan.com TDD Rhythm Flowchart Write just enough Code to compile Refactor Code Pass the Test (GREEN) Fail the Test (RED) Write just enough Code to pass the test Specify what the software should do Pass the Test (GREEN)
  • 22. So, TDD is about… Analyzing what little you actually need to do and how cleanly you can do it! Carving design of your code a unit test at a time.
  • 23. dhaval.dalal@software-artisan.com Write Tests Before Writing Code ! Focuses the mind (and the development process) " Deliver only what is absolutely necessary. " System so developed does exactly what it needs to do and no more. " Need not code for future ! YAGNI (You Ain’t Gonna Need It!)...no gold plating!
  • 24. dhaval.dalal@software-artisan.com TDD is a Design Technique ! Makes you think in terms of Object behavior. " How client is going to interact with the object. ! Outside-In ! Object so created is “consumer aware” " What Object provides and needs from environment. " Traditional OOD focuses only on Object’s Implementation ! Inside-Out
  • 25. dhaval.dalal@software-artisan.com TDD results in a Decoupled Design ! Favors Composition over Inheritance. " Relies on dependency injection for collaborators. ! Avoids tight coupling to global objects " Singletons mix static and state. " Makes design untestable. ! Many small, loosely coupled classes. ! Makes you think of inter-object interactions in terms of interfaces. " Promotes Programming to Super-Types and not Concretes.
  • 26. Listening to the Tests ! A difficulty in implementing a new feature is usually a symptom that the design can be improved. " Ask why is it difficult to test? Don’t just think how do I test this? ! TDD is not just about functionality, it gives us feedback on code’s internal quality. " How is the coupling and cohesion? " Are we preserving encapsulation? " What about implicit or explicit dependencies? Source: Growing Object-Oriented Software, Guided by Tests
  • 27. Listening to the Tests ! Check continuously, observe and meditate on the tests, they might tell you something. ! The idea is to let the test drive the design. That’s why it is called Test Driven Design. Source: Growing Object-Oriented Software, Guided by Tests
  • 28. dhaval.dalal@software-artisan.com Benefits of TDD ! Test-a-little and build-a-little gives confidence " Green bar gives you confidence " Reduces fear of change ! Documentation " Provides starting point to understand code functionality ! Safety Net " Checks Regression " Supports Refactoring
  • 29. dhaval.dalal@software-artisan.com Benefits of TDD ! Effort " Reduces effort to final delivery " Writing tests is more productive ! Predictable " Tells me when am I done " Continuous Success Vs Illusive Success ! Immediate Feedback " Makes failures shine at you
  • 30. dhaval.dalal@software-artisan.com Costs of TDD Claim: It is too much work to write tests! Rebut: I’d say “are you looking to create a speculative design?”, “do you want to sleep with a debugger?” Claim: Tests themselves are code, we need to maintain them, that’s overhead! Rebut: I’d say “do you prefer maintaining a big bug list?” Claim: I have to spend time re-orient my thinking! Rebut: I’d say “Just as with any skill, you need to allow some time to apply this effectively.”
  • 32. dhaval.dalal@software-artisan.com Better Test Practices ! Tests must be Small. " Easy to understand " They do not break when other parts of the code are changed. " One behavioral-assert per test. ! Tests must be Expressive. " Test code should communicate its intent. " It should not take more than 2 minutes for readers to understand what is going on. ! Tests must be Maintainable. " When a test breaks, what it contains should be easiest to fix.
  • 33. dhaval.dalal@software-artisan.com Better Test Practices ! Tests must execute Fast. " Slow running tests increase Build Viscosity. ! Tests are Specifications, not Verifications. " Do not verify whether the code does what its supposed to do correctly. " Specify what should the code do to function correctly. ! Tests should talk the Domain Language. " Communicate the behavior under test and not how the system implements that behavior. " Improves Communication with Non-Technical Members on the team. " Developers can understand domain faster.
  • 34. dhaval.dalal@software-artisan.com Better Test Practices ! Tests must run at will. " Able to write and execute tests without worrying about how to execute them. ! Tests must be Isolated " Very little set-up and minimum collaborators. ! Tests must be Thorough " Test all behavior, not methods.
  • 35. dhaval.dalal@software-artisan.com Better Test Practices ! Tests must be Automated. " Write them such that methods on objects are invoked by code rather than by hand. ! Tests must be Self-Verifiable. " Setup test expectations and compare outcome with expectations for verdicts. ! Tests must be Repeatable. " Executing the same test several times under the same conditions must yield same results.
  • 36. dhaval.dalal@software-artisan.com JUnit/NUnit Better Test Practices ! Test anything that could possibly break. ! Make testing exceptional scenarios easy to read. ! Always explain failure reason in Assert calls. ! Test should usually improve the design of the code. ! For JUnit Tests " Make Test code reside in same packages, but different directories. ! For NUnit Tests " Make Test code reside in separate project from source project, in same namespace.
  • 37. dhaval.dalal@software-artisan.com Flavors of Tests ! Object Tests (Unit or Programmer Tests) " Tests behavior of single object at a time. ! Integration Tests " Tests collaboration of a number of objects (how they talk to each other) " Complex fixtures " More brittle ! End-to-End Tests " Thoroughly test the entire system from end-to-end.
  • 38. dhaval.dalal@software-artisan.com Unit Testing Frameworks ! xUnit framework for Unit Testing " JUnit/TestNG for Java " NUnit/MbUnit for C# " cppUnit for C++ " pyUnit for Python " …and tons of more Unit Testing frameworks.
  • 40. dhaval.dalal@software-artisan.com References ! JUnit Recipes " J. B. Rainsberger ! JUnit In Action " Vincent Massol ! Agile Java " Jeff Langr ! Test-Driven Development Rhythm " Gunjan Doshi ! Agile Principles, Patterns, and Practices in C# " Robert C. Martin, Micah Martin ! xUnit Test Patterns " Gerard Meszaros ! On TDD (InfoQ): How Do We Know When We’re Done? " Steve Freeman ! Growing Object-Oriented Software, Guided by Tests " Steve Freeman and Nat Pryce ! 10-Ways-to-Better-Code (InfoQ) " Neal Ford ! Refactoring Away Duplicated Logic Creates a Domain Specific Embedded Language for Testing " Nat Pryce ! Jay Field’s Blog Entry " http://blog.jayfields.com/ 2007/06/testing-inline-setup.html