SlideShare a Scribd company logo
Definitions of TDD
Test oriented development
• Having unit tests or integration tests in the code and write
them out either before or after writing the code. The code
has lots of tests and the value of tests is recognized. Design
probably exists before code writing.
Definitions of TDD
Test driven development or/and design
• Test Driven Development: The idea of writing your code in a
test-first manner. You may already have an existing design in
place. Tests are used to enhance the design.
• Test Driven Design: The idea of using a test-first approach as
a fully fledged design technique, where tests are a bonus but
the idea is to drive full design from little to no design
whatsoever. Tests are used to create the design.
Two levels of TDD
Acceptance TDD (ATDD)
• With ATDD you write a single acceptance test, and then just
enough production functionality/code to fulfill that test.
• The goal of ATDD is to specify detailed, executable
requirements for your solution on a just in time (JIT) basis.
ATDD is also called Behavior Driven Development (BDD).
Two levels of TDD
Developer TDD (TDD)
• With developer TDD you write a single developer test,
sometimes inaccurately referred to as a unit test, and then just
enough production code to fulfill that test.
• The goal of developer TDD is to specify a detailed,
executable design for your solution on a JIT basis.
Developer TDD is often simply called TDD.
Two levels of TDD
"Ideally, you'll write a single acceptance test, then to implement
the production code required to fulfill that test you'll take a
developer TDD approach."
Red-Green-Refactor
Red-Green-Refactor
Fail: Write a failing test to prove code or functionality is missing
• A programmer taking a TDD approach refuses to write a new
function until there is first a test that fails because that
function isn’t present (pair programming can help).
Pass: Make the test pass by writing production code that meets
the expectations
• By focusing on writing only the code necessary to pass tests,
designs can be cleaner and clearer than is often achieved by
other methods.
Refactor: Now the code can be cleaned up as necessary
• By re-running the tests, the developer can be confident that
code refactoring is not damaging any existing functionality.
DESIGN
Values of TDD
o Is about design specification not just validation
o Helps think through requirements before coding
o Helps focus on clean design and code
o Forces more testable and decoupled program structure
o Results in high code coverage
o Tests can serve as software documentation
o Tests can serve as refactoring safety net
o Automatic regression testing that can provide more specific clues of
defects (faster debugging and fixing)
o Conflicting tests can sometimes point out problems in customer
requirements
Unit Tests
A unit test should have the following properties:
• It should be automated and repeatable.
• It should be easy to implement.
• Once it’s written, it should remain for future use.
• Anyone should be able to run it.
• It should run at the push of a button.
• It should run quickly.
A unit test usually comprises 3 main actions:
• Arrange objects, creating and setting them up as necessary.
• Act on an object.
• Assert that something is as expected.
String Calculator
1. Create a simple string calculator with a method int Sum(string
numbers)
2. The method can take string of comma delimited 1, 2 or more
numbers, and will return the sum of the numbers
3. For any invalid string input it will return 0
First failing test
Both SHOULD & SHOULD NOT
So far…
• Had the chance to think about requirements and behaviors
• Didn’t need to worry about errors I’d see if I needed to run
this program to check the results
• Could focus on the design of overall structure and first
method.
• Designed and implemented the first method without
depending on the concrete implementation of the other 2
methods it calls (decoupling)
Parser
Adder
Refactor Parser > Run tests
Refactor Calculator > Run tests
Values of TDD, again
• Is about design specification not just validation
• Helps think through requirements before coding
• Helps focus on clean design and code
• Forces more testable and decoupled program structure
• Results in high code coverage
• Tests can serve as software documentation
• Tests can serve as refactoring safety net
• Automatic regression testing that can provide more specific clues of
defects (faster debugging and fixing)
• Conflicting tests can sometimes point out problems in customer
requirements
Pillars of good unit tests
Trustworthiness
• Trustworthy tests don’t have bugs and test the right things.
Developers can accept the test results with confidence.
Maintainability
• Ruin project schedule. Risk loosing the tests when the project
is put on a more aggressive schedule.
Readability
• Foundation of the other 2 pillars.
Trustworthiness: Test only one thing
Multiple asserts
• Harder to gave the test a more specific name
• After a failure, subsequent asserts aren’t executed; other
possible symptoms that could discover the problem are
missing
Multiple mocks (verifying whether the object under test
interacted as expected)
• There should be no more than one mock
• All other fake objects will act as stubs
Trustworthiness: Test only one thing
Solutions
• Create a separate test for each assert
• Use parameterized tests (EXAMPLE)
Fail and stop
Separate tests
Data-driven unit tests
Parameterized test
Extending MSTest > VS2012
Trustworthiness: Avoid test logic
switch, if-else, foreach, for, while, try-catch...
• The chances of having bugs in the tests increase
• A test that contains logic is usually testing more than one
thing at a time, which isn’t recommended, because the test is
less readable and more fragile.
Trustworthiness…
Make tests easy to run
• Better organized (unit test green zone, categories, test lists),
consistent results...
Assure code coverage
• In TDD because no more code is written than necessary to
pass a failing test case, automated tests tend to cover every
code path
Maintainability: Avoid testing private
or protected methods
• Private method tells developers that they can change the
implementation without worrying much about unknown
code that uses it.
• Testing private methods may lead to breaking tests, even
though the overall functionality is correct.
• Wanting to test a method means that it has a known behavior
or contract against the calling code.
• Making it public, extracting to class, making it static, making
it internal (least favorite)
Maintainability: Removing test code
duplication
Solutions
• Arrange with shared factory method, which can take
parameters and return values
• Initialize method
• Initialize methods should only contain code that applies to all the
tests in the current test class, or it will be harder to understand.
• Refactor to avoid long and hard to understand initialize method
• Create shared mocks/stubs in initialize method? Sometimes let
each test to call the shared helper method to create them for
better readability.
Maintainability: Enforcing test
isolation
Anti-patterns
• Hidden test call: Tests contain direct calls to other tests, which
causes tests to depend on one another
• Constrained test order: Tests are coded to expect a specific
state created by running other tests
• Shared-state corruption: Tests touch shared resources without
first initializing the resources or cleaning those up after testing
Solutions
• Flow testing - do integration testing instead
• Initialize and cleanup for each test
Maintainability: Avoiding testing
multiple aspects of the same object
• If we need to validate a BOX object, we may need one assert
for width, one assert to height, one assert for length, one
assert for material...
• Override Equals() method
Readability
Asserting with meaning
• Custom message
Separating asserts from actions
• “NO” Assert.AreEqual(COULD_NOT_READ_FILE,
log.GetLineCount("abc.txt"));
Initialize and cleanup
• Developers may not realize that there are mock objects set
up in initialize method. It’s more readable to initialize mock
objects directly in the test, with all their expectations. Can be
refactored into factory/helper methods.
Readability
Naming unit tests, basically 3 parts
• The name of the method being tested
• The scenario under which it’s being tested
• The expected behavior when the scenario is invoked
Naming variables
Each class stands
for a context
Use attributes to categorize tests
Use test lists
“TDD should be treated as an an approach (design aide), not just
as a task”
“Test Driven Development, done right, should make developers
highly aware of design pitfalls like tight coupling, violations of
SRP (Single Responsibility Principle), etc.”
“TDD = Refactoring + TFD”
References
http://artofunittesting.com/
http://en.wikipedia.org/wiki/Test-driven_development
http://www.agiledata.org/essays/tdd.html
http://www.pathfindersolns.com/resources/industry-glossary/tdd/
http://osherove.com/blog/2007/10/8/the-various-meanings-of-tdd.html
http://stackoverflow.com/questions/7538744/is-test-driven-development-the-same-as-test-
driven-design
http://stackoverflow.com/questions/80243/does-test-driven-development-take-the-focus-from-
design
http://stephenwalther.com/archive/2009/04/11/tdd-tests-are-not-unit-tests.aspx
http://biblio.gdinwiddie.com/biblio/StudiesOfTestDrivenDevelopment
http://elegantcode.com/2008/09/08/bdd-test-naming-experiment/
http://mrclyfar.blogspot.com/2010/02/amazing-mapping-demo-at-ted-2010.html
http://www.softwaretestinghelp.com/how-to-test-software-requirements-specification-srs/
http://davesquared.net/2009/10/calculators-and-tale-of-two-tdds-pt-1.html
http://davesquared.net/2009/10/calculators-and-tale-of-two-tdds-pt-2.html
http://www.shirmanov.com/2011/06/mstest-expectedexception-and-code.html

More Related Content

What's hot

Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Mireia Sangalo
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
Derek Smith
 
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
Roy Osherove on Unit Testing Good Practices and Horrible MistakesRoy Osherove on Unit Testing Good Practices and Horrible Mistakes
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
Roy Osherove
 
Moq presentation
Moq presentationMoq presentation
Moq presentation
LynxStar
 
Effective unit testing
Effective unit testingEffective unit testing
Effective unit testing
Roberto Casadei
 
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
 
TDD Best Practices
TDD Best PracticesTDD Best Practices
TDD Best Practices
Attila Bertók
 
Unit Testing Guidelines
Unit Testing GuidelinesUnit Testing Guidelines
Unit Testing Guidelines
Joel Hooks
 
White box & Black box testing
White box & Black box testingWhite box & Black box testing
White box & Black box testing
NitishMhaske1
 
Unit Testing (C#)
Unit Testing (C#)Unit Testing (C#)
Unit Testing (C#)
Prashant Cholachagudd
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testing
ikhwanhayat
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
Joe Wilson
 
Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing Fundamentals
Richard Paul
 
Assessing Unit Test Quality
Assessing Unit Test QualityAssessing Unit Test Quality
Assessing Unit Test Quality
guest268ee8
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
Joe Tremblay
 
Unit Test Presentation
Unit Test PresentationUnit Test Presentation
Unit Test Presentation
Sayedur Rahman
 
UNIT TESTING
UNIT TESTINGUNIT TESTING
UNIT TESTING
Marius Crisan
 
Unit testing - the hard parts
Unit testing - the hard partsUnit testing - the hard parts
Unit testing - the hard parts
Shaun Abram
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
Dror Helper
 
Beginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NETBeginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NET
Baskar K
 

What's hot (20)

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
 
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
Roy Osherove on Unit Testing Good Practices and Horrible MistakesRoy Osherove on Unit Testing Good Practices and Horrible Mistakes
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
 
Moq presentation
Moq presentationMoq presentation
Moq presentation
 
Effective unit testing
Effective unit testingEffective unit testing
Effective unit testing
 
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
 
TDD Best Practices
TDD Best PracticesTDD Best Practices
TDD Best Practices
 
Unit Testing Guidelines
Unit Testing GuidelinesUnit Testing Guidelines
Unit Testing Guidelines
 
White box & Black box testing
White box & Black box testingWhite box & Black box testing
White box & Black box testing
 
Unit Testing (C#)
Unit Testing (C#)Unit Testing (C#)
Unit Testing (C#)
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testing
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
 
Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing Fundamentals
 
Assessing Unit Test Quality
Assessing Unit Test QualityAssessing Unit Test Quality
Assessing Unit Test Quality
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
Unit Test Presentation
Unit Test PresentationUnit Test Presentation
Unit Test Presentation
 
UNIT TESTING
UNIT TESTINGUNIT TESTING
UNIT TESTING
 
Unit testing - the hard parts
Unit testing - the hard partsUnit testing - the hard parts
Unit testing - the hard parts
 
Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
 
Beginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NETBeginners - Get Started With Unit Testing in .NET
Beginners - Get Started With Unit Testing in .NET
 

Viewers also liked

Few minutes To better Code - Refactoring
Few minutes To better Code - RefactoringFew minutes To better Code - Refactoring
Few minutes To better Code - Refactoring
Diaa Al-Salehi
 
Being Lean Agile
Being Lean AgileBeing Lean Agile
Being Lean Agile
prakashmpujar
 
Improve your development skills with Test Driven Development
Improve your development skills with Test Driven DevelopmentImprove your development skills with Test Driven Development
Improve your development skills with Test Driven Development
John Stevenson
 
Refactoring
RefactoringRefactoring
Refactoring
Marcus Denker
 
Automating UI testing
Automating UI testingAutomating UI testing
Automating UI testing
Adam Siton
 
Clean code and refactoring
Clean code and refactoringClean code and refactoring
Clean code and refactoring
Yuriy Gerasimov
 
Agile korea 2013 유석문
Agile korea 2013 유석문Agile korea 2013 유석문
Agile korea 2013 유석문
Sangcheol Hwang
 

Viewers also liked (7)

Few minutes To better Code - Refactoring
Few minutes To better Code - RefactoringFew minutes To better Code - Refactoring
Few minutes To better Code - Refactoring
 
Being Lean Agile
Being Lean AgileBeing Lean Agile
Being Lean Agile
 
Improve your development skills with Test Driven Development
Improve your development skills with Test Driven DevelopmentImprove your development skills with Test Driven Development
Improve your development skills with Test Driven Development
 
Refactoring
RefactoringRefactoring
Refactoring
 
Automating UI testing
Automating UI testingAutomating UI testing
Automating UI testing
 
Clean code and refactoring
Clean code and refactoringClean code and refactoring
Clean code and refactoring
 
Agile korea 2013 유석문
Agile korea 2013 유석문Agile korea 2013 유석문
Agile korea 2013 유석문
 

Similar to Test-Driven Development

Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Md. Enamul Haque Chowdhury
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
Sahar Nofal
 
Test driven development v1.0
Test driven development v1.0Test driven development v1.0
Test driven development v1.0
Ganesh Kondal
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@
Alex Borsuk
 
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
Agile Mumbai 2020 Conference |  How to get the best ROI on Your Test Automati...Agile Mumbai 2020 Conference |  How to get the best ROI on Your Test Automati...
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
AgileNetwork
 
Tdd
TddTdd
Test driven development
Test driven developmentTest driven development
Test driven development
Fraboni Ec
 
Test driven development
Test driven developmentTest driven development
Test driven development
Harry Potter
 
Test driven development
Test driven developmentTest driven development
Test driven development
Luis Goldster
 
Test driven development
Test driven developmentTest driven development
Test driven development
Tony Nguyen
 
Test driven development
Test driven developmentTest driven development
Test driven development
Young Alista
 
Test driven development
Test driven developmentTest driven development
Test driven development
James Wong
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Sergey Aganezov
 
Unit Testing Best Practices
Unit Testing Best PracticesUnit Testing Best Practices
Unit Testing Best Practices
Tomaš Maconko
 
Lecture 21
Lecture 21Lecture 21
Lecture 21
Skillspire LLC
 
Testing, a pragmatic approach
Testing, a pragmatic approachTesting, a pragmatic approach
Testing, a pragmatic approach
Enrico Da Ros
 
White box testing
White box testingWhite box testing
White box testing
Neethu Tressa
 
TDD In Practice
TDD In PracticeTDD In Practice
TDD In Practice
Alan Christensen
 
Week 14 Unit Testing.pptx
Week 14  Unit Testing.pptxWeek 14  Unit Testing.pptx
Week 14 Unit Testing.pptx
mianshafa
 
Unit testing
Unit testingUnit testing
Unit testing
Vinod Wilson
 

Similar to Test-Driven Development (20)

Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
Test driven development v1.0
Test driven development v1.0Test driven development v1.0
Test driven development v1.0
 
Unit Testing Full@
Unit Testing Full@Unit Testing Full@
Unit Testing Full@
 
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
Agile Mumbai 2020 Conference |  How to get the best ROI on Your Test Automati...Agile Mumbai 2020 Conference |  How to get the best ROI on Your Test Automati...
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
 
Tdd
TddTdd
Tdd
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Unit Testing Best Practices
Unit Testing Best PracticesUnit Testing Best Practices
Unit Testing Best Practices
 
Lecture 21
Lecture 21Lecture 21
Lecture 21
 
Testing, a pragmatic approach
Testing, a pragmatic approachTesting, a pragmatic approach
Testing, a pragmatic approach
 
White box testing
White box testingWhite box testing
White box testing
 
TDD In Practice
TDD In PracticeTDD In Practice
TDD In Practice
 
Week 14 Unit Testing.pptx
Week 14  Unit Testing.pptxWeek 14  Unit Testing.pptx
Week 14 Unit Testing.pptx
 
Unit testing
Unit testingUnit testing
Unit testing
 

Test-Driven Development

  • 1. Definitions of TDD Test oriented development • Having unit tests or integration tests in the code and write them out either before or after writing the code. The code has lots of tests and the value of tests is recognized. Design probably exists before code writing.
  • 2. Definitions of TDD Test driven development or/and design • Test Driven Development: The idea of writing your code in a test-first manner. You may already have an existing design in place. Tests are used to enhance the design. • Test Driven Design: The idea of using a test-first approach as a fully fledged design technique, where tests are a bonus but the idea is to drive full design from little to no design whatsoever. Tests are used to create the design.
  • 3. Two levels of TDD Acceptance TDD (ATDD) • With ATDD you write a single acceptance test, and then just enough production functionality/code to fulfill that test. • The goal of ATDD is to specify detailed, executable requirements for your solution on a just in time (JIT) basis. ATDD is also called Behavior Driven Development (BDD).
  • 4. Two levels of TDD Developer TDD (TDD) • With developer TDD you write a single developer test, sometimes inaccurately referred to as a unit test, and then just enough production code to fulfill that test. • The goal of developer TDD is to specify a detailed, executable design for your solution on a JIT basis. Developer TDD is often simply called TDD.
  • 5. Two levels of TDD "Ideally, you'll write a single acceptance test, then to implement the production code required to fulfill that test you'll take a developer TDD approach."
  • 7. Red-Green-Refactor Fail: Write a failing test to prove code or functionality is missing • A programmer taking a TDD approach refuses to write a new function until there is first a test that fails because that function isn’t present (pair programming can help). Pass: Make the test pass by writing production code that meets the expectations • By focusing on writing only the code necessary to pass tests, designs can be cleaner and clearer than is often achieved by other methods. Refactor: Now the code can be cleaned up as necessary • By re-running the tests, the developer can be confident that code refactoring is not damaging any existing functionality. DESIGN
  • 8. Values of TDD o Is about design specification not just validation o Helps think through requirements before coding o Helps focus on clean design and code o Forces more testable and decoupled program structure o Results in high code coverage o Tests can serve as software documentation o Tests can serve as refactoring safety net o Automatic regression testing that can provide more specific clues of defects (faster debugging and fixing) o Conflicting tests can sometimes point out problems in customer requirements
  • 9. Unit Tests A unit test should have the following properties: • It should be automated and repeatable. • It should be easy to implement. • Once it’s written, it should remain for future use. • Anyone should be able to run it. • It should run at the push of a button. • It should run quickly. A unit test usually comprises 3 main actions: • Arrange objects, creating and setting them up as necessary. • Act on an object. • Assert that something is as expected.
  • 10. String Calculator 1. Create a simple string calculator with a method int Sum(string numbers) 2. The method can take string of comma delimited 1, 2 or more numbers, and will return the sum of the numbers 3. For any invalid string input it will return 0
  • 12.
  • 13.
  • 14.
  • 15. Both SHOULD & SHOULD NOT
  • 16.
  • 17. So far… • Had the chance to think about requirements and behaviors • Didn’t need to worry about errors I’d see if I needed to run this program to check the results • Could focus on the design of overall structure and first method. • Designed and implemented the first method without depending on the concrete implementation of the other 2 methods it calls (decoupling)
  • 19.
  • 20.
  • 21. Refactor Parser > Run tests Refactor Calculator > Run tests
  • 22. Values of TDD, again • Is about design specification not just validation • Helps think through requirements before coding • Helps focus on clean design and code • Forces more testable and decoupled program structure • Results in high code coverage • Tests can serve as software documentation • Tests can serve as refactoring safety net • Automatic regression testing that can provide more specific clues of defects (faster debugging and fixing) • Conflicting tests can sometimes point out problems in customer requirements
  • 23. Pillars of good unit tests Trustworthiness • Trustworthy tests don’t have bugs and test the right things. Developers can accept the test results with confidence. Maintainability • Ruin project schedule. Risk loosing the tests when the project is put on a more aggressive schedule. Readability • Foundation of the other 2 pillars.
  • 24. Trustworthiness: Test only one thing Multiple asserts • Harder to gave the test a more specific name • After a failure, subsequent asserts aren’t executed; other possible symptoms that could discover the problem are missing Multiple mocks (verifying whether the object under test interacted as expected) • There should be no more than one mock • All other fake objects will act as stubs
  • 25. Trustworthiness: Test only one thing Solutions • Create a separate test for each assert • Use parameterized tests (EXAMPLE)
  • 29.
  • 31. Trustworthiness: Avoid test logic switch, if-else, foreach, for, while, try-catch... • The chances of having bugs in the tests increase • A test that contains logic is usually testing more than one thing at a time, which isn’t recommended, because the test is less readable and more fragile.
  • 32. Trustworthiness… Make tests easy to run • Better organized (unit test green zone, categories, test lists), consistent results... Assure code coverage • In TDD because no more code is written than necessary to pass a failing test case, automated tests tend to cover every code path
  • 33. Maintainability: Avoid testing private or protected methods • Private method tells developers that they can change the implementation without worrying much about unknown code that uses it. • Testing private methods may lead to breaking tests, even though the overall functionality is correct. • Wanting to test a method means that it has a known behavior or contract against the calling code. • Making it public, extracting to class, making it static, making it internal (least favorite)
  • 34. Maintainability: Removing test code duplication Solutions • Arrange with shared factory method, which can take parameters and return values • Initialize method • Initialize methods should only contain code that applies to all the tests in the current test class, or it will be harder to understand. • Refactor to avoid long and hard to understand initialize method • Create shared mocks/stubs in initialize method? Sometimes let each test to call the shared helper method to create them for better readability.
  • 35. Maintainability: Enforcing test isolation Anti-patterns • Hidden test call: Tests contain direct calls to other tests, which causes tests to depend on one another • Constrained test order: Tests are coded to expect a specific state created by running other tests • Shared-state corruption: Tests touch shared resources without first initializing the resources or cleaning those up after testing Solutions • Flow testing - do integration testing instead • Initialize and cleanup for each test
  • 36. Maintainability: Avoiding testing multiple aspects of the same object • If we need to validate a BOX object, we may need one assert for width, one assert to height, one assert for length, one assert for material... • Override Equals() method
  • 37. Readability Asserting with meaning • Custom message Separating asserts from actions • “NO” Assert.AreEqual(COULD_NOT_READ_FILE, log.GetLineCount("abc.txt")); Initialize and cleanup • Developers may not realize that there are mock objects set up in initialize method. It’s more readable to initialize mock objects directly in the test, with all their expectations. Can be refactored into factory/helper methods.
  • 38. Readability Naming unit tests, basically 3 parts • The name of the method being tested • The scenario under which it’s being tested • The expected behavior when the scenario is invoked Naming variables
  • 39.
  • 41. Use attributes to categorize tests
  • 43. “TDD should be treated as an an approach (design aide), not just as a task” “Test Driven Development, done right, should make developers highly aware of design pitfalls like tight coupling, violations of SRP (Single Responsibility Principle), etc.” “TDD = Refactoring + TFD”
  • 44. References http://artofunittesting.com/ http://en.wikipedia.org/wiki/Test-driven_development http://www.agiledata.org/essays/tdd.html http://www.pathfindersolns.com/resources/industry-glossary/tdd/ http://osherove.com/blog/2007/10/8/the-various-meanings-of-tdd.html http://stackoverflow.com/questions/7538744/is-test-driven-development-the-same-as-test- driven-design http://stackoverflow.com/questions/80243/does-test-driven-development-take-the-focus-from- design http://stephenwalther.com/archive/2009/04/11/tdd-tests-are-not-unit-tests.aspx http://biblio.gdinwiddie.com/biblio/StudiesOfTestDrivenDevelopment http://elegantcode.com/2008/09/08/bdd-test-naming-experiment/ http://mrclyfar.blogspot.com/2010/02/amazing-mapping-demo-at-ted-2010.html http://www.softwaretestinghelp.com/how-to-test-software-requirements-specification-srs/ http://davesquared.net/2009/10/calculators-and-tale-of-two-tdds-pt-1.html http://davesquared.net/2009/10/calculators-and-tale-of-two-tdds-pt-2.html http://www.shirmanov.com/2011/06/mstest-expectedexception-and-code.html