SlideShare a Scribd company logo
Think Agile - Practice TDD
Unit Testing Good PracticesRoy OsheroveChief ArchitectTypemockDEV204
Team Agile - All rights reservedReal World AgendaMaking your tests trustworthyCreating maintainable testsReadable tests – last but not least!
Team Agile - All rights reservedMake Your tests trust worthyTest the right thingRemoving or changing testsAssuring code coverageAvoid test logic Make it easy to run
Test the right thing[Test]Public void Sum(){ int result = calculator.Sum(1,2);Assert.AreEqual(4,result, “bad sum”);}Team Agile - All rights reserved
Team Agile - All rights reservedRemoving/Changing testsAs rule – don’t change or remove. Always add new tests.Unless:It’s for clarity (functionality stays the same)Test is not neededTest is needed to run differently
Team Agile - All rights reservedAssuring code coverage[DEMO]Change production code and see what happensMake params into constsRemove “if” checks – or make into constsIf all tests pass  - something is missing or something is not needed
Team Agile - All rights reservedAvoid Test LogicNo Ifs, SWITCH’s or CASE’sOnly Create, configure, act and ASSERTTest logic == test bugsFail first assures your test is correct as well!
Team Agile - All rights reservedMake it easy to runIntegration vs. Unit testsConfiguration vs. “ClickOnce”Laziness is keyIf some tests fail all the time no one listensOne package *always* has to work!
Team Agile - All rights reservedCreating maintainable testsAvoid testing private/protected membersRe-use test code (Create, Manipulate, Assert)Enforce test isolationAvoid Multiple Asserts
Team Agile - All rights reservedTest only publicsTesting a private makes your test more brittleMakes you think about the design and usability of a featureTest-First leads to private members after Refactoring, but they are all tested!But sometimes there’s not choice
Team Agile - All rights reservedReuse test code[DEMO]Create objects using common methods (factories) [make_XX]Manipulate and configure initial state using common methods [init_XX]Run common tests in common methods [verify_XX]
Team Agile - All rights reservedEnforce test isolationNo dependency between tests!Don’t run a test from another test!Run alone or all together in any orderOtherwise – leads to nasty “what was that?” bugs
Team Agile - All rights reservedAvoid Multiple AssertsLike having multiple testsBut the first the fails – kills the othersYou won’t get the big picture (some symptoms)Hard to nameCan lead to more debugging work
Team Agile - All rights reservedCreating readable testsNaming a unit testNaming variablesSeparate Assert from action
Team Agile - All rights reservedNaming a unit testMethod nameState under testExpected behavior/return valueAdd_LessThanZero_ThrowsException()Another angle:Add_LessThanZero_ThrowsException2()
No Magic ValuesAssert.AreEqual(1003, calc.Parse(“-1”));IntparseResult = calc.Parse(NEGATIVE_ILLEGAL_NUMBER);Assert.AreEqual(NEGATIVE_PARSE_RETURN_CODE, parseResult)
Team Agile - All rights reservedSeparate Assert from ActionPrevious exampleAssert is less clutteredMore readableAllows handling the return value if neededIt’s all about reability
Summary
Team Agile - All rights reservedMake Your tests trust worthyTest the right thingRemoving or changing testsAssuring code coverageAvoid test logicMake it easy to run
Team Agile - All rights reservedCreating maintainable testsAvoid testing private/protected membersRe-use test code (Create, Manipulate, Assert)Enforce test isolationAvoid Multiple Asserts
Team Agile - All rights reservedCreating readable testsNaming a unit testNaming variablesSeparate Assert from action
Team Agile - All rights reserved
www.ArtOfUnitTesting.comFree Chapterswww.Typemock.comTypemock Isolator, IntelliTestResources
Required Slide© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation.  Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation.  MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.

More Related Content

PPTX
Unit Testing
PPT
An insight to test driven development and unit testing
PPT
Agile In Practice
PDF
Unit Testing Best Practices
PPT
Unit testing
PDF
Unit Testing Standards - Recommended Best Practices
PDF
Unit Testing for Great Justice
PDF
Configuration Management Camp 2015
Unit Testing
An insight to test driven development and unit testing
Agile In Practice
Unit Testing Best Practices
Unit testing
Unit Testing Standards - Recommended Best Practices
Unit Testing for Great Justice
Configuration Management Camp 2015

Similar to Think Agile - Practice TDD (20)

PPTX
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
PPTX
Building unit tests correctly with visual studio 2013
PPTX
Testes? Mas isso não aumenta o tempo de projecto? Não quero...
PPTX
Rc2010 tdd
PDF
Essential Test-Driven Development
PPTX
TDD Best Practices
PPTX
Unit testing
PPTX
Why does agile need unit testing
PDF
Workshop unit test
PPTX
Unit Testing and TDD 2017
PPTX
Unit Testing in .NET Core 7.0 with XUnit.pptx
PPTX
Unit Testing Concepts and Best Practices
PDF
Agile testing
PPTX
Building unit tests correctly
PPTX
Unit testing
PPTX
Tester’s fate in an agile
PDF
An Introduction to Unit Test Using NUnit
PPTX
Unit testing
PPTX
Unit testing basics with NUnit and Visual Studio
PDF
[Rakuten TechConf2014] [G-4] Beyond Agile Testing to Lean Development
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
Building unit tests correctly with visual studio 2013
Testes? Mas isso não aumenta o tempo de projecto? Não quero...
Rc2010 tdd
Essential Test-Driven Development
TDD Best Practices
Unit testing
Why does agile need unit testing
Workshop unit test
Unit Testing and TDD 2017
Unit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing Concepts and Best Practices
Agile testing
Building unit tests correctly
Unit testing
Tester’s fate in an agile
An Introduction to Unit Test Using NUnit
Unit testing
Unit testing basics with NUnit and Visual Studio
[Rakuten TechConf2014] [G-4] Beyond Agile Testing to Lean Development
Ad

Recently uploaded (20)

PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Empathic Computing: Creating Shared Understanding
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
DOCX
The AUB Centre for AI in Media Proposal.docx
PPT
Teaching material agriculture food technology
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
sap open course for s4hana steps from ECC to s4
PDF
KodekX | Application Modernization Development
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
cuic standard and advanced reporting.pdf
Review of recent advances in non-invasive hemoglobin estimation
20250228 LYD VKU AI Blended-Learning.pptx
Chapter 3 Spatial Domain Image Processing.pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Unlocking AI with Model Context Protocol (MCP)
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Empathic Computing: Creating Shared Understanding
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
The AUB Centre for AI in Media Proposal.docx
Teaching material agriculture food technology
Spectral efficient network and resource selection model in 5G networks
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
sap open course for s4hana steps from ECC to s4
KodekX | Application Modernization Development
The Rise and Fall of 3GPP – Time for a Sabbatical?
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
cuic standard and advanced reporting.pdf
Ad

Think Agile - Practice TDD

  • 2. Unit Testing Good PracticesRoy OsheroveChief ArchitectTypemockDEV204
  • 3. Team Agile - All rights reservedReal World AgendaMaking your tests trustworthyCreating maintainable testsReadable tests – last but not least!
  • 4. Team Agile - All rights reservedMake Your tests trust worthyTest the right thingRemoving or changing testsAssuring code coverageAvoid test logic Make it easy to run
  • 5. Test the right thing[Test]Public void Sum(){ int result = calculator.Sum(1,2);Assert.AreEqual(4,result, “bad sum”);}Team Agile - All rights reserved
  • 6. Team Agile - All rights reservedRemoving/Changing testsAs rule – don’t change or remove. Always add new tests.Unless:It’s for clarity (functionality stays the same)Test is not neededTest is needed to run differently
  • 7. Team Agile - All rights reservedAssuring code coverage[DEMO]Change production code and see what happensMake params into constsRemove “if” checks – or make into constsIf all tests pass - something is missing or something is not needed
  • 8. Team Agile - All rights reservedAvoid Test LogicNo Ifs, SWITCH’s or CASE’sOnly Create, configure, act and ASSERTTest logic == test bugsFail first assures your test is correct as well!
  • 9. Team Agile - All rights reservedMake it easy to runIntegration vs. Unit testsConfiguration vs. “ClickOnce”Laziness is keyIf some tests fail all the time no one listensOne package *always* has to work!
  • 10. Team Agile - All rights reservedCreating maintainable testsAvoid testing private/protected membersRe-use test code (Create, Manipulate, Assert)Enforce test isolationAvoid Multiple Asserts
  • 11. Team Agile - All rights reservedTest only publicsTesting a private makes your test more brittleMakes you think about the design and usability of a featureTest-First leads to private members after Refactoring, but they are all tested!But sometimes there’s not choice
  • 12. Team Agile - All rights reservedReuse test code[DEMO]Create objects using common methods (factories) [make_XX]Manipulate and configure initial state using common methods [init_XX]Run common tests in common methods [verify_XX]
  • 13. Team Agile - All rights reservedEnforce test isolationNo dependency between tests!Don’t run a test from another test!Run alone or all together in any orderOtherwise – leads to nasty “what was that?” bugs
  • 14. Team Agile - All rights reservedAvoid Multiple AssertsLike having multiple testsBut the first the fails – kills the othersYou won’t get the big picture (some symptoms)Hard to nameCan lead to more debugging work
  • 15. Team Agile - All rights reservedCreating readable testsNaming a unit testNaming variablesSeparate Assert from action
  • 16. Team Agile - All rights reservedNaming a unit testMethod nameState under testExpected behavior/return valueAdd_LessThanZero_ThrowsException()Another angle:Add_LessThanZero_ThrowsException2()
  • 17. No Magic ValuesAssert.AreEqual(1003, calc.Parse(“-1”));IntparseResult = calc.Parse(NEGATIVE_ILLEGAL_NUMBER);Assert.AreEqual(NEGATIVE_PARSE_RETURN_CODE, parseResult)
  • 18. Team Agile - All rights reservedSeparate Assert from ActionPrevious exampleAssert is less clutteredMore readableAllows handling the return value if neededIt’s all about reability
  • 20. Team Agile - All rights reservedMake Your tests trust worthyTest the right thingRemoving or changing testsAssuring code coverageAvoid test logicMake it easy to run
  • 21. Team Agile - All rights reservedCreating maintainable testsAvoid testing private/protected membersRe-use test code (Create, Manipulate, Assert)Enforce test isolationAvoid Multiple Asserts
  • 22. Team Agile - All rights reservedCreating readable testsNaming a unit testNaming variablesSeparate Assert from action
  • 23. Team Agile - All rights reserved
  • 25. Required Slide© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.