SlideShare ist ein Scribd-Unternehmen logo
1 von 25
Unit Testing Good Practices Roy Osherove Chief Architect Typemock DEV204
Team Agile - All rights reserved Real World Agenda Making your tests trustworthy Creating maintainable tests Readable tests – last but not least!
Team Agile - All rights reserved Make Your tests trust worthy Test the right thing Removing or changing tests Assuring code coverage Avoid 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 reserved Removing/Changing tests As rule – don’t change or remove. Always add new tests. Unless: It’s for clarity (functionality stays the same) Test is not needed Test is needed to run differently
Team Agile - All rights reserved Assuring code coverage [DEMO] Change production code and see what happens Make params into consts Remove “if” checks – or make into consts If all tests pass  - something is missing or something is not needed
Team Agile - All rights reserved Avoid Test Logic No Ifs, SWITCH’s or CASE’s Only Create, configure, act and ASSERT Test logic == test bugs Fail first assures your test is correct as well!
Team Agile - All rights reserved Make it easy to run Integration vs. Unit tests Configuration vs. “ClickOnce” Laziness is key If some tests fail all the time no one listens One package *always* has to work!
Team Agile - All rights reserved Creating maintainable tests Avoid testing private/protected members Re-use test code (Create, Manipulate, Assert) Enforce test isolation Avoid Multiple Asserts
Team Agile - All rights reserved Test only publics Testing a private makes your test more brittle Makes you think about the design and usability of a feature Test-First leads to private members after Refactoring, but they are all tested! But sometimes there’s not choice
Team Agile - All rights reserved Reuse 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 reserved Enforce test isolation No dependency between tests! Don’t run a test from another test! Run alone  or all together  in any order Otherwise – leads to nasty “what was that?” bugs
Team Agile - All rights reserved Avoid Multiple Asserts Like having multiple tests But the first the fails – kills the others You won’t get the big picture (some symptoms) Hard to name Can lead to more debugging work
Team Agile - All rights reserved Creating readable tests Naming a unit test Naming variables Separate Assert from action
Team Agile - All rights reserved Naming a unit test Method name State under test Expected behavior/return value Add_LessThanZero_ThrowsException() Another angle: Add_LessThanZero_ThrowsException2()
No Magic Values Assert.AreEqual(1003, calc.Parse(“-1”)); IntparseResult = calc.Parse(NEGATIVE_ILLEGAL_NUMBER); Assert.AreEqual( NEGATIVE_PARSE_RETURN_CODE,  parseResult)
Team Agile - All rights reserved Separate Assert from Action Previous example Assert is less cluttered More readable Allows handling the return value if needed It’s all about reability
Summary
Team Agile - All rights reserved Make Your tests trust worthy Test the right thing Removing or changing tests Assuring code coverage Avoid test logic Make it easy to run
Team Agile - All rights reserved Creating maintainable tests Avoid testing private/protected members Re-use test code (Create, Manipulate, Assert) Enforce test isolation Avoid Multiple Asserts
Team Agile - All rights reserved Creating readable tests Naming a unit test Naming variables Separate Assert from action
Team Agile - All rights reserved
www.ArtOfUnitTesting.com Free Chapters www.Typemock.com Typemock Isolator, IntelliTest Resources
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.

Weitere ähnliche Inhalte

Andere mochten auch

Mobile guldkorn v.2 - SIGCHI
Mobile guldkorn v.2  - SIGCHI Mobile guldkorn v.2  - SIGCHI
Mobile guldkorn v.2 - SIGCHI Henning Nielsen
 
Cubiertos Para Personas De La Tercera Edad
Cubiertos Para Personas De La Tercera EdadCubiertos Para Personas De La Tercera Edad
Cubiertos Para Personas De La Tercera EdadRichard Huett
 
Bienvenida ing.economica ene jun 2011
Bienvenida ing.economica ene jun 2011Bienvenida ing.economica ene jun 2011
Bienvenida ing.economica ene jun 2011maribel
 
Corso Versacamm E Pro Ii
Corso Versacamm E Pro IiCorso Versacamm E Pro Ii
Corso Versacamm E Pro IiGiovanni Re
 
Advportfolioreviewportfolio Presentation
Advportfolioreviewportfolio PresentationAdvportfolioreviewportfolio Presentation
Advportfolioreviewportfolio PresentationDeborah Wolff
 
04 seigneurs, rois, premiers etats
04 seigneurs, rois, premiers etats04 seigneurs, rois, premiers etats
04 seigneurs, rois, premiers etatsSalle212
 
Comercio electronico
Comercio electronicoComercio electronico
Comercio electronicocarvajaljose
 
E- portafolio DERLYS PAOLA ACOSTA - GRUPO No 57
E- portafolio DERLYS PAOLA ACOSTA - GRUPO No 57E- portafolio DERLYS PAOLA ACOSTA - GRUPO No 57
E- portafolio DERLYS PAOLA ACOSTA - GRUPO No 57dpacostac
 
Comercio electronico
Comercio electronicoComercio electronico
Comercio electronicocarvajaljose
 
Folleto Archivo Y Doc E,210410
Folleto Archivo Y Doc E,210410Folleto Archivo Y Doc E,210410
Folleto Archivo Y Doc E,210410AMIPCI
 
ממני והלאה משחקי מחשב ונטיות חשיבה פלג ואלגלי
ממני והלאה משחקי מחשב ונטיות חשיבה פלג ואלגליממני והלאה משחקי מחשב ונטיות חשיבה פלג ואלגלי
ממני והלאה משחקי מחשב ונטיות חשיבה פלג ואלגליKibbutzim College of Education
 

Andere mochten auch (18)

Mobile guldkorn v.2 - SIGCHI
Mobile guldkorn v.2  - SIGCHI Mobile guldkorn v.2  - SIGCHI
Mobile guldkorn v.2 - SIGCHI
 
22 De Enero
22 De Enero22 De Enero
22 De Enero
 
Cubiertos Para Personas De La Tercera Edad
Cubiertos Para Personas De La Tercera EdadCubiertos Para Personas De La Tercera Edad
Cubiertos Para Personas De La Tercera Edad
 
Bienvenida ing.economica ene jun 2011
Bienvenida ing.economica ene jun 2011Bienvenida ing.economica ene jun 2011
Bienvenida ing.economica ene jun 2011
 
Corso Versacamm E Pro Ii
Corso Versacamm E Pro IiCorso Versacamm E Pro Ii
Corso Versacamm E Pro Ii
 
Advportfolioreviewportfolio Presentation
Advportfolioreviewportfolio PresentationAdvportfolioreviewportfolio Presentation
Advportfolioreviewportfolio Presentation
 
04 seigneurs, rois, premiers etats
04 seigneurs, rois, premiers etats04 seigneurs, rois, premiers etats
04 seigneurs, rois, premiers etats
 
Comercio electronico
Comercio electronicoComercio electronico
Comercio electronico
 
Grup4
Grup4Grup4
Grup4
 
Ing p3-03
Ing p3-03Ing p3-03
Ing p3-03
 
Activitat efecte hivernacle
Activitat efecte hivernacleActivitat efecte hivernacle
Activitat efecte hivernacle
 
E- portafolio DERLYS PAOLA ACOSTA - GRUPO No 57
E- portafolio DERLYS PAOLA ACOSTA - GRUPO No 57E- portafolio DERLYS PAOLA ACOSTA - GRUPO No 57
E- portafolio DERLYS PAOLA ACOSTA - GRUPO No 57
 
2013.11
2013.112013.11
2013.11
 
Comercio electronico
Comercio electronicoComercio electronico
Comercio electronico
 
Folleto Archivo Y Doc E,210410
Folleto Archivo Y Doc E,210410Folleto Archivo Y Doc E,210410
Folleto Archivo Y Doc E,210410
 
Grupo 6
Grupo 6Grupo 6
Grupo 6
 
20
2020
20
 
ממני והלאה משחקי מחשב ונטיות חשיבה פלג ואלגלי
ממני והלאה משחקי מחשב ונטיות חשיבה פלג ואלגליממני והלאה משחקי מחשב ונטיות חשיבה פלג ואלגלי
ממני והלאה משחקי מחשב ונטיות חשיבה פלג ואלגלי
 

Ähnlich wie Dev204 Osherove

Unit testing basics with NUnit and Visual Studio
Unit testing basics with NUnit and Visual StudioUnit testing basics with NUnit and Visual Studio
Unit testing basics with NUnit and Visual StudioAmit Choudhary
 
Real Life Unit Testing
Real Life Unit TestingReal Life Unit Testing
Real Life Unit TestingDror Helper
 
.NET Fest 2019. Arnon Axelrod. Test automation for developers
.NET Fest 2019. Arnon Axelrod. Test automation for developers.NET Fest 2019. Arnon Axelrod. Test automation for developers
.NET Fest 2019. Arnon Axelrod. Test automation for developersNETFest
 
An introduction to unit testing
An introduction to unit testingAn introduction to unit testing
An introduction to unit testingAdam Stephensen
 
Unit testing php-unit - phing - selenium_v2
Unit testing   php-unit - phing - selenium_v2Unit testing   php-unit - phing - selenium_v2
Unit testing php-unit - phing - selenium_v2Tricode (part of Dept)
 
Simple test drupal7_presentation_la_drupal_jul21-2010
Simple test drupal7_presentation_la_drupal_jul21-2010Simple test drupal7_presentation_la_drupal_jul21-2010
Simple test drupal7_presentation_la_drupal_jul21-2010Miguel Hernandez
 
Unit Testing, TDD and the Walking Skeleton
Unit Testing, TDD and the Walking SkeletonUnit Testing, TDD and the Walking Skeleton
Unit Testing, TDD and the Walking SkeletonSeb Rose
 
03 test specification and execution
03   test specification and execution03   test specification and execution
03 test specification and executionClemens Reijnen
 
Tdd Ugialtnet Jan2010
Tdd Ugialtnet Jan2010Tdd Ugialtnet Jan2010
Tdd Ugialtnet Jan2010guestcff805
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesDerek Smith
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven DevelopmentJohn Blum
 
Test Driven Development with Sql Server
Test Driven Development with Sql ServerTest Driven Development with Sql Server
Test Driven Development with Sql ServerDavid P. Moore
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017Xavi Hidalgo
 
Unit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptxUnit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptxKnoldus Inc.
 
Peter Zimmerer - Passion For Testing, By Examples - EuroSTAR 2010
Peter Zimmerer - Passion For Testing, By Examples - EuroSTAR 2010Peter Zimmerer - Passion For Testing, By Examples - EuroSTAR 2010
Peter Zimmerer - Passion For Testing, By Examples - EuroSTAR 2010TEST Huddle
 
Testing and TDD - KoJUG
Testing and TDD - KoJUGTesting and TDD - KoJUG
Testing and TDD - KoJUGlburdz
 

Ähnlich wie Dev204 Osherove (20)

Unit testing basics with NUnit and Visual Studio
Unit testing basics with NUnit and Visual StudioUnit testing basics with NUnit and Visual Studio
Unit testing basics with NUnit and Visual Studio
 
Unit testing, principles
Unit testing, principlesUnit testing, principles
Unit testing, principles
 
Real Life Unit Testing
Real Life Unit TestingReal Life Unit Testing
Real Life Unit Testing
 
Tdd dev session
Tdd dev sessionTdd dev session
Tdd dev session
 
.NET Fest 2019. Arnon Axelrod. Test automation for developers
.NET Fest 2019. Arnon Axelrod. Test automation for developers.NET Fest 2019. Arnon Axelrod. Test automation for developers
.NET Fest 2019. Arnon Axelrod. Test automation for developers
 
An introduction to unit testing
An introduction to unit testingAn introduction to unit testing
An introduction to unit testing
 
Unit testing php-unit - phing - selenium_v2
Unit testing   php-unit - phing - selenium_v2Unit testing   php-unit - phing - selenium_v2
Unit testing php-unit - phing - selenium_v2
 
Simple test drupal7_presentation_la_drupal_jul21-2010
Simple test drupal7_presentation_la_drupal_jul21-2010Simple test drupal7_presentation_la_drupal_jul21-2010
Simple test drupal7_presentation_la_drupal_jul21-2010
 
Unit Testing, TDD and the Walking Skeleton
Unit Testing, TDD and the Walking SkeletonUnit Testing, TDD and the Walking Skeleton
Unit Testing, TDD and the Walking Skeleton
 
03 test specification and execution
03   test specification and execution03   test specification and execution
03 test specification and execution
 
Unit testing
Unit testingUnit testing
Unit testing
 
Tdd Ugialtnet Jan2010
Tdd Ugialtnet Jan2010Tdd Ugialtnet Jan2010
Tdd Ugialtnet Jan2010
 
Unit Testing Concepts and Best Practices
Unit Testing Concepts and Best PracticesUnit Testing Concepts and Best Practices
Unit Testing Concepts and Best Practices
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 
TDD Best Practices
TDD Best PracticesTDD Best Practices
TDD Best Practices
 
Test Driven Development with Sql Server
Test Driven Development with Sql ServerTest Driven Development with Sql Server
Test Driven Development with Sql Server
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017
 
Unit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptxUnit Testing in .NET Core 7.0 with XUnit.pptx
Unit Testing in .NET Core 7.0 with XUnit.pptx
 
Peter Zimmerer - Passion For Testing, By Examples - EuroSTAR 2010
Peter Zimmerer - Passion For Testing, By Examples - EuroSTAR 2010Peter Zimmerer - Passion For Testing, By Examples - EuroSTAR 2010
Peter Zimmerer - Passion For Testing, By Examples - EuroSTAR 2010
 
Testing and TDD - KoJUG
Testing and TDD - KoJUGTesting and TDD - KoJUG
Testing and TDD - KoJUG
 

Kürzlich hochgeladen

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
 
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
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
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
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
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
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
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
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 

Kürzlich hochgeladen (20)

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
 
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
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
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
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
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
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
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...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 

Dev204 Osherove

  • 1.
  • 2. Unit Testing Good Practices Roy Osherove Chief Architect Typemock DEV204
  • 3. Team Agile - All rights reserved Real World Agenda Making your tests trustworthy Creating maintainable tests Readable tests – last but not least!
  • 4. Team Agile - All rights reserved Make Your tests trust worthy Test the right thing Removing or changing tests Assuring code coverage Avoid 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 reserved Removing/Changing tests As rule – don’t change or remove. Always add new tests. Unless: It’s for clarity (functionality stays the same) Test is not needed Test is needed to run differently
  • 7. Team Agile - All rights reserved Assuring code coverage [DEMO] Change production code and see what happens Make params into consts Remove “if” checks – or make into consts If all tests pass - something is missing or something is not needed
  • 8. Team Agile - All rights reserved Avoid Test Logic No Ifs, SWITCH’s or CASE’s Only Create, configure, act and ASSERT Test logic == test bugs Fail first assures your test is correct as well!
  • 9. Team Agile - All rights reserved Make it easy to run Integration vs. Unit tests Configuration vs. “ClickOnce” Laziness is key If some tests fail all the time no one listens One package *always* has to work!
  • 10. Team Agile - All rights reserved Creating maintainable tests Avoid testing private/protected members Re-use test code (Create, Manipulate, Assert) Enforce test isolation Avoid Multiple Asserts
  • 11. Team Agile - All rights reserved Test only publics Testing a private makes your test more brittle Makes you think about the design and usability of a feature Test-First leads to private members after Refactoring, but they are all tested! But sometimes there’s not choice
  • 12. Team Agile - All rights reserved Reuse 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 reserved Enforce test isolation No dependency between tests! Don’t run a test from another test! Run alone or all together in any order Otherwise – leads to nasty “what was that?” bugs
  • 14. Team Agile - All rights reserved Avoid Multiple Asserts Like having multiple tests But the first the fails – kills the others You won’t get the big picture (some symptoms) Hard to name Can lead to more debugging work
  • 15. Team Agile - All rights reserved Creating readable tests Naming a unit test Naming variables Separate Assert from action
  • 16. Team Agile - All rights reserved Naming a unit test Method name State under test Expected behavior/return value Add_LessThanZero_ThrowsException() Another angle: Add_LessThanZero_ThrowsException2()
  • 17. No Magic Values Assert.AreEqual(1003, calc.Parse(“-1”)); IntparseResult = calc.Parse(NEGATIVE_ILLEGAL_NUMBER); Assert.AreEqual( NEGATIVE_PARSE_RETURN_CODE, parseResult)
  • 18. Team Agile - All rights reserved Separate Assert from Action Previous example Assert is less cluttered More readable Allows handling the return value if needed It’s all about reability
  • 20. Team Agile - All rights reserved Make Your tests trust worthy Test the right thing Removing or changing tests Assuring code coverage Avoid test logic Make it easy to run
  • 21. Team Agile - All rights reserved Creating maintainable tests Avoid testing private/protected members Re-use test code (Create, Manipulate, Assert) Enforce test isolation Avoid Multiple Asserts
  • 22. Team Agile - All rights reserved Creating readable tests Naming a unit test Naming variables Separate Assert from action
  • 23. Team Agile - All rights reserved
  • 24. www.ArtOfUnitTesting.com Free Chapters www.Typemock.com Typemock Isolator, IntelliTest Resources
  • 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.