SlideShare una empresa de Scribd logo
1 de 32
Descargar para leer sin conexión
Unit Testing Guidelines
                 Why, What and How...




@jhooks | 2010 | joelhooks.com
Why?
                It’s such a pain.
                there is never enough time!




@jhooks | 2010 | joelhooks.com
Sanity.
                Developers want a productive environment where
                things work as expected.
                Unit test can help create this environment at the code level.




@jhooks | 2010 | joelhooks.com
Protect your work.
                We work hard on the functionality that we add to the
                application.
                Unit tests serve as a guard against accidental “harm” of code.




@jhooks | 2010 | joelhooks.com
Developer documentation.
                Well written unit test provide excellent documentation
                for other developers.
                Unit tests describe how a piece of logical code should work in a way that English
                language docs often can’t.




@jhooks | 2010 | joelhooks.com
Collective ownership.
                Code that is protected can be worked on by anybody
                with greater assurance that nothing will be accidentally
                broken.




@jhooks | 2010 | joelhooks.com
Fearless refactoring.
                Code can be constantly improved upon with less fear
                of accidental breakage.
                Unit tests verify that changes don’t break the existing logic.




@jhooks | 2010 | joelhooks.com
What should be tested?
                 Models
                 Service classes
                 Commands
                 Utility classes
                 any other logical code




@jhooks | 2010 | joelhooks.com
When to write tests...
                 Before you write the code?
                 After you write the code?




@jhooks | 2010 | joelhooks.com
Test Driven Development




                                 10 Ways to Improve Your Code - Neal Ford




@jhooks | 2010 | joelhooks.com
Less fear.
                Writing unit tests firsts lets you focus on the
                functionality you are adding.
                Write the test. Write the code. Tests pass. Done.




@jhooks | 2010 | joelhooks.com
Ensure test coverage.
                Tests written before production code ensures that the
                production code is tested.
                Tests are not an afterthought.




@jhooks | 2010 | joelhooks.com
Reduces tedium of testing.
                Wait... What?!
                Writing tests before the production code eliminates the need for a massive testing
                effort after the code is written.




@jhooks | 2010 | joelhooks.com
Guarantees testable code.
                Testing first ensures that the production code can be
                tested.
                Testing after often results in the discovery that the code isn’t testable without
                refactoring. Refactoring that isn’t protected by tests.




@jhooks | 2010 | joelhooks.com
TDD is not a guarantee.
                Like most practices, TDD is no guarantee of quality or
                success in development.
                It is a tool.




@jhooks | 2010 | joelhooks.com
So when should I test?
                Test driven development is not required.
                Try it out. It can be painful to start, but once you get a rhythm going the benefits
                are very real.




@jhooks | 2010 | joelhooks.com
What is a good unit test?
                     automated
                     repeatable
                     run by anybody
                     future use
                     fast
                     single push
                     easy




@jhooks | 2010 | joelhooks.com
Unit tests are by developers for developers.
             Quality over quantity please.




@jhooks | 2010 | joelhooks.com
Trustworthy
                Developers will run and use tests they trust.
                A trustworthy test is free of bugs and does what it says it does.




@jhooks | 2010 | joelhooks.com
Tests are written to last.
                Most tests are not meant to be temporary. They
                change for very specific reasons

                Bugs in production code
                Bugs in the test
                API updates in the production code
                Test is no longer valid or is not needed
                To eliminate duplication




@jhooks | 2010 | joelhooks.com
There is no logic in the test.
                Logic in a unit tests makes the test harder to read and
                understand. There is more likely to be bugs in the test.
                It can also make the test harder to name.

                There should be no switch, if, or else statements.
                There should be no for each, for, or while statements.
                The test is a series of method calls with no control flow.




@jhooks | 2010 | joelhooks.com
Only one thing is tested.
                A unit test is testing a single behavior. Testing multiple
                things makes a test hard to understand.

                Assert only one thing.
                Should be easy to name (you don’t need an and in the name).
                When it fails, it is clear what actually failed.




@jhooks | 2010 | joelhooks.com
Maintainable
                Unmaintainable tests are ignored and are often
                simply removed from the suite.
                Unmaintainable tests cannot be trusted.




@jhooks | 2010 | joelhooks.com
Test the API
                Unit tests are written against the public contract of the
                unit being tested. We shouldn’t be trying to test
                private methods.

                Private and protected methods can affect the outcome of public methods.
                Does it make sense to make a private method public?
                Test the results of the public API that uses private methods
                Is the private method pure utility? Would it makes sense as a static method of a
                utility class?




@jhooks | 2010 | joelhooks.com
Avoid duplication (DRY)
                Avoiding the duplication of code in unit tests is as, if
                not more, important than it is with production code.

                Create helper methods like factories to assist in setup of common items
                Use setup methods for setup of items common to all test methods in the case
                Setup should be as short, simple, and as easy to read as possible




@jhooks | 2010 | joelhooks.com
Tests should be isolated.
                Don’t try to use Parsley, configure and access remote
                services, or otherwise try to simulate the broader
                application in a unit test.

                Are test methods constrained to a linear order of execution?
                Is the test calling other test methods?
                Do tests share global state?
                Test behaviors, not workflows.
                If shared state can’t be avoid, be sure to reset it in the teardown.




@jhooks | 2010 | joelhooks.com
Avoid multiple asserts.
                Unit tests are testing specific single behaviors. It seems
                like more work because results might be related, but
                the results should be verified independently.

                Give each assertion its own test
                Give each test a meaningful name
                This makes failures easy to understand and fix




@jhooks | 2010 | joelhooks.com
Avoid over-specification.
                Tests shouldn’t make assumptions about the
                implementation of behavior, instead they should focus
                on the results of the behavior.

                Does the test specify purely internal behavior of the unit?
                Is the test using complicated mock objects when simple stubs would be enough?
                Does the test assume specific results when it isn’t required?




@jhooks | 2010 | joelhooks.com
Readable
                Code in tests is easy to understand quickly. What is
                being tested is recognizable instantly without
                deciphering or translation.
                Readable tests are more trustworthy and maintainable.




@jhooks | 2010 | joelhooks.com
Standard test names.
                If all tests are named in the same pattern they will be
                easy to read..



              methodUnderTest_descriptionOfState_expectedBehavior()




@jhooks | 2010 | joelhooks.com
Good variable names.
                As with all code, good variable names can help
                greatly in making the code readable.

                Avoid abbreviations
                Use static constants instead of hardcoded values




@jhooks | 2010 | joelhooks.com
Consistent test structure.
                Setup -> Action -> Assert


         [Test]
         public function doSomeWork_workWasDone_isTrue()
         {
             //setup
             var aDependency:ISomeDependency = new SomeDependency();

                  //execute behavior
                  aDependency.doSomeWork();

                  //verify expected state is valid
                  assertThat(allTestsNeedThis.workWasDone, isTrue());
         }




@jhooks | 2010 | joelhooks.com

Más contenido relacionado

La actualidad más candente

Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDDDror Helper
 
Clean Unit Test Patterns
Clean Unit Test PatternsClean Unit Test Patterns
Clean Unit Test PatternsFrank Appel
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit TestingJoe Tremblay
 
Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practicesnickokiss
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testingikhwanhayat
 
Unit Testing Best Practices
Unit Testing Best PracticesUnit Testing Best Practices
Unit Testing Best PracticesTomaš Maconko
 
Benefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real WorldBenefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real WorldDror Helper
 
Unit and integration Testing
Unit and integration TestingUnit and integration Testing
Unit and integration TestingDavid Berliner
 
Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing FundamentalsRichard Paul
 
Unit Tests And Automated Testing
Unit Tests And Automated TestingUnit Tests And Automated Testing
Unit Tests And Automated TestingLee Englestone
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit TestingMike Lively
 
Software Quality via Unit Testing
Software Quality via Unit TestingSoftware Quality via Unit Testing
Software Quality via Unit TestingShaun Abram
 
Test driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + EclipseTest driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + EclipseUTC Fire & Security
 
Unit testing best practices with JUnit
Unit testing best practices with JUnitUnit testing best practices with JUnit
Unit testing best practices with JUnitinTwentyEight Minutes
 

La actualidad más candente (20)

Unit tests & TDD
Unit tests & TDDUnit tests & TDD
Unit tests & TDD
 
Clean Unit Test Patterns
Clean Unit Test PatternsClean Unit Test Patterns
Clean Unit Test Patterns
 
An Introduction to Unit Testing
An Introduction to Unit TestingAn Introduction to Unit Testing
An Introduction to Unit Testing
 
Unit testing best practices
Unit testing best practicesUnit testing best practices
Unit testing best practices
 
Workshop unit test
Workshop   unit testWorkshop   unit test
Workshop unit test
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testing
 
Unit testing
Unit testingUnit testing
Unit testing
 
Unit Testing Best Practices
Unit Testing Best PracticesUnit Testing Best Practices
Unit Testing Best Practices
 
Benefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real WorldBenefit From Unit Testing In The Real World
Benefit From Unit Testing In The Real World
 
UNIT TESTING
UNIT TESTINGUNIT TESTING
UNIT TESTING
 
Unit and integration Testing
Unit and integration TestingUnit and integration Testing
Unit and integration Testing
 
Best practices unit testing
Best practices unit testing Best practices unit testing
Best practices unit testing
 
Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing Fundamentals
 
Unit Tests And Automated Testing
Unit Tests And Automated TestingUnit Tests And Automated Testing
Unit Tests And Automated Testing
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit Testing
 
Software Quality via Unit Testing
Software Quality via Unit TestingSoftware Quality via Unit Testing
Software Quality via Unit Testing
 
Unit testing
Unit testingUnit testing
Unit testing
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Test driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + EclipseTest driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + Eclipse
 
Unit testing best practices with JUnit
Unit testing best practices with JUnitUnit testing best practices with JUnit
Unit testing best practices with JUnit
 

Destacado

Robotlegs AS3 from Flash and the City 2010
Robotlegs AS3 from Flash and the City 2010Robotlegs AS3 from Flash and the City 2010
Robotlegs AS3 from Flash and the City 2010Joel Hooks
 
Misconceptions Of Unit Testing
Misconceptions Of Unit TestingMisconceptions Of Unit Testing
Misconceptions Of Unit TestingTerry Yin
 
Behavior Driven Development with AngularJS & Jasmine
Behavior Driven Development with AngularJS & JasmineBehavior Driven Development with AngularJS & Jasmine
Behavior Driven Development with AngularJS & JasmineRemus Langu
 
Testing check list
Testing check listTesting check list
Testing check listAtul Pant
 
TESTING Checklist
TESTING Checklist TESTING Checklist
TESTING Checklist Febin Chacko
 
A Second Look at Unit Testing by Roy Osherove
A Second Look at Unit Testing by Roy OsheroveA Second Look at Unit Testing by Roy Osherove
A Second Look at Unit Testing by Roy OsheroveRoy Osherove
 
Matching test items
Matching test itemsMatching test items
Matching test itemsaelnogab
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPTsuhasreddy1
 

Destacado (15)

Robotlegs AS3 from Flash and the City 2010
Robotlegs AS3 from Flash and the City 2010Robotlegs AS3 from Flash and the City 2010
Robotlegs AS3 from Flash and the City 2010
 
Misconceptions Of Unit Testing
Misconceptions Of Unit TestingMisconceptions Of Unit Testing
Misconceptions Of Unit Testing
 
Behavior Driven Development with AngularJS & Jasmine
Behavior Driven Development with AngularJS & JasmineBehavior Driven Development with AngularJS & Jasmine
Behavior Driven Development with AngularJS & Jasmine
 
Jasmine BDD for Javascript
Jasmine BDD for JavascriptJasmine BDD for Javascript
Jasmine BDD for Javascript
 
Testing check list
Testing check listTesting check list
Testing check list
 
TDD Basics with Angular.js and Jasmine
TDD Basics with Angular.js and JasmineTDD Basics with Angular.js and Jasmine
TDD Basics with Angular.js and Jasmine
 
Testcase Preparation Checklist
Testcase Preparation ChecklistTestcase Preparation Checklist
Testcase Preparation Checklist
 
TESTING Checklist
TESTING Checklist TESTING Checklist
TESTING Checklist
 
A Second Look at Unit Testing by Roy Osherove
A Second Look at Unit Testing by Roy OsheroveA Second Look at Unit Testing by Roy Osherove
A Second Look at Unit Testing by Roy Osherove
 
Clean code
Clean codeClean code
Clean code
 
Security testing
Security testingSecurity testing
Security testing
 
Checklist for website testing
Checklist for website testingChecklist for website testing
Checklist for website testing
 
Matching test items
Matching test itemsMatching test items
Matching test items
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPT
 
Checklist
ChecklistChecklist
Checklist
 

Similar a Unit Testing Guidelines

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
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017Xavi Hidalgo
 
Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)Abhijeet Vaikar
 
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
 
An Introduction to unit testing
An Introduction to unit testingAn Introduction to unit testing
An Introduction to unit testingSteven Casey
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven DevelopmentJohn Blum
 
Increasing Quality with DevOps
Increasing Quality with DevOpsIncreasing Quality with DevOps
Increasing Quality with DevOpsCoveros, Inc.
 
Agile Testing 20021015
Agile Testing 20021015Agile Testing 20021015
Agile Testing 20021015Raghu Karnati
 
Testing and TDD - KoJUG
Testing and TDD - KoJUGTesting and TDD - KoJUG
Testing and TDD - KoJUGlburdz
 
5-Ways-to-Revolutionize-Your-Software-Testing
5-Ways-to-Revolutionize-Your-Software-Testing5-Ways-to-Revolutionize-Your-Software-Testing
5-Ways-to-Revolutionize-Your-Software-TestingMary Clemons
 
Improving developer tester collaboration with microsoft visual studio 2010
Improving developer tester collaboration with microsoft visual studio 2010Improving developer tester collaboration with microsoft visual studio 2010
Improving developer tester collaboration with microsoft visual studio 2010Mohamed Samy
 
.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
 

Similar a Unit Testing Guidelines (20)

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
 
TDD Best Practices
TDD Best PracticesTDD Best Practices
TDD Best Practices
 
Unit testing, principles
Unit testing, principlesUnit testing, principles
Unit testing, principles
 
JavaScript Unit Testing
JavaScript Unit TestingJavaScript Unit Testing
JavaScript Unit Testing
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017
 
Test driven development(tdd)
Test driven development(tdd)Test driven development(tdd)
Test driven development(tdd)
 
Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)Unit testing (Exploring the other side as a tester)
Unit testing (Exploring the other side as a tester)
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development
 
An Introduction to unit testing
An Introduction to unit testingAn Introduction to unit testing
An Introduction to unit testing
 
Android testing
Android testingAndroid testing
Android testing
 
Test-Driven Development
Test-Driven DevelopmentTest-Driven Development
Test-Driven Development
 
Increasing Quality with DevOps
Increasing Quality with DevOpsIncreasing Quality with DevOps
Increasing Quality with DevOps
 
TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
 
Agile Testing 20021015
Agile Testing 20021015Agile Testing 20021015
Agile Testing 20021015
 
Testing and TDD - KoJUG
Testing and TDD - KoJUGTesting and TDD - KoJUG
Testing and TDD - KoJUG
 
5-Ways-to-Revolutionize-Your-Software-Testing
5-Ways-to-Revolutionize-Your-Software-Testing5-Ways-to-Revolutionize-Your-Software-Testing
5-Ways-to-Revolutionize-Your-Software-Testing
 
Improving developer tester collaboration with microsoft visual studio 2010
Improving developer tester collaboration with microsoft visual studio 2010Improving developer tester collaboration with microsoft visual studio 2010
Improving developer tester collaboration with microsoft visual studio 2010
 
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
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 

Último

Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
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
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
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
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
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
 
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
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
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
 

Último (20)

Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
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
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
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
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
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
 
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...
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 

Unit Testing Guidelines

  • 1. Unit Testing Guidelines Why, What and How... @jhooks | 2010 | joelhooks.com
  • 2. Why? It’s such a pain. there is never enough time! @jhooks | 2010 | joelhooks.com
  • 3. Sanity. Developers want a productive environment where things work as expected. Unit test can help create this environment at the code level. @jhooks | 2010 | joelhooks.com
  • 4. Protect your work. We work hard on the functionality that we add to the application. Unit tests serve as a guard against accidental “harm” of code. @jhooks | 2010 | joelhooks.com
  • 5. Developer documentation. Well written unit test provide excellent documentation for other developers. Unit tests describe how a piece of logical code should work in a way that English language docs often can’t. @jhooks | 2010 | joelhooks.com
  • 6. Collective ownership. Code that is protected can be worked on by anybody with greater assurance that nothing will be accidentally broken. @jhooks | 2010 | joelhooks.com
  • 7. Fearless refactoring. Code can be constantly improved upon with less fear of accidental breakage. Unit tests verify that changes don’t break the existing logic. @jhooks | 2010 | joelhooks.com
  • 8. What should be tested? Models Service classes Commands Utility classes any other logical code @jhooks | 2010 | joelhooks.com
  • 9. When to write tests... Before you write the code? After you write the code? @jhooks | 2010 | joelhooks.com
  • 10. Test Driven Development 10 Ways to Improve Your Code - Neal Ford @jhooks | 2010 | joelhooks.com
  • 11. Less fear. Writing unit tests firsts lets you focus on the functionality you are adding. Write the test. Write the code. Tests pass. Done. @jhooks | 2010 | joelhooks.com
  • 12. Ensure test coverage. Tests written before production code ensures that the production code is tested. Tests are not an afterthought. @jhooks | 2010 | joelhooks.com
  • 13. Reduces tedium of testing. Wait... What?! Writing tests before the production code eliminates the need for a massive testing effort after the code is written. @jhooks | 2010 | joelhooks.com
  • 14. Guarantees testable code. Testing first ensures that the production code can be tested. Testing after often results in the discovery that the code isn’t testable without refactoring. Refactoring that isn’t protected by tests. @jhooks | 2010 | joelhooks.com
  • 15. TDD is not a guarantee. Like most practices, TDD is no guarantee of quality or success in development. It is a tool. @jhooks | 2010 | joelhooks.com
  • 16. So when should I test? Test driven development is not required. Try it out. It can be painful to start, but once you get a rhythm going the benefits are very real. @jhooks | 2010 | joelhooks.com
  • 17. What is a good unit test? automated repeatable run by anybody future use fast single push easy @jhooks | 2010 | joelhooks.com
  • 18. Unit tests are by developers for developers. Quality over quantity please. @jhooks | 2010 | joelhooks.com
  • 19. Trustworthy Developers will run and use tests they trust. A trustworthy test is free of bugs and does what it says it does. @jhooks | 2010 | joelhooks.com
  • 20. Tests are written to last. Most tests are not meant to be temporary. They change for very specific reasons Bugs in production code Bugs in the test API updates in the production code Test is no longer valid or is not needed To eliminate duplication @jhooks | 2010 | joelhooks.com
  • 21. There is no logic in the test. Logic in a unit tests makes the test harder to read and understand. There is more likely to be bugs in the test. It can also make the test harder to name. There should be no switch, if, or else statements. There should be no for each, for, or while statements. The test is a series of method calls with no control flow. @jhooks | 2010 | joelhooks.com
  • 22. Only one thing is tested. A unit test is testing a single behavior. Testing multiple things makes a test hard to understand. Assert only one thing. Should be easy to name (you don’t need an and in the name). When it fails, it is clear what actually failed. @jhooks | 2010 | joelhooks.com
  • 23. Maintainable Unmaintainable tests are ignored and are often simply removed from the suite. Unmaintainable tests cannot be trusted. @jhooks | 2010 | joelhooks.com
  • 24. Test the API Unit tests are written against the public contract of the unit being tested. We shouldn’t be trying to test private methods. Private and protected methods can affect the outcome of public methods. Does it make sense to make a private method public? Test the results of the public API that uses private methods Is the private method pure utility? Would it makes sense as a static method of a utility class? @jhooks | 2010 | joelhooks.com
  • 25. Avoid duplication (DRY) Avoiding the duplication of code in unit tests is as, if not more, important than it is with production code. Create helper methods like factories to assist in setup of common items Use setup methods for setup of items common to all test methods in the case Setup should be as short, simple, and as easy to read as possible @jhooks | 2010 | joelhooks.com
  • 26. Tests should be isolated. Don’t try to use Parsley, configure and access remote services, or otherwise try to simulate the broader application in a unit test. Are test methods constrained to a linear order of execution? Is the test calling other test methods? Do tests share global state? Test behaviors, not workflows. If shared state can’t be avoid, be sure to reset it in the teardown. @jhooks | 2010 | joelhooks.com
  • 27. Avoid multiple asserts. Unit tests are testing specific single behaviors. It seems like more work because results might be related, but the results should be verified independently. Give each assertion its own test Give each test a meaningful name This makes failures easy to understand and fix @jhooks | 2010 | joelhooks.com
  • 28. Avoid over-specification. Tests shouldn’t make assumptions about the implementation of behavior, instead they should focus on the results of the behavior. Does the test specify purely internal behavior of the unit? Is the test using complicated mock objects when simple stubs would be enough? Does the test assume specific results when it isn’t required? @jhooks | 2010 | joelhooks.com
  • 29. Readable Code in tests is easy to understand quickly. What is being tested is recognizable instantly without deciphering or translation. Readable tests are more trustworthy and maintainable. @jhooks | 2010 | joelhooks.com
  • 30. Standard test names. If all tests are named in the same pattern they will be easy to read.. methodUnderTest_descriptionOfState_expectedBehavior() @jhooks | 2010 | joelhooks.com
  • 31. Good variable names. As with all code, good variable names can help greatly in making the code readable. Avoid abbreviations Use static constants instead of hardcoded values @jhooks | 2010 | joelhooks.com
  • 32. Consistent test structure. Setup -> Action -> Assert [Test] public function doSomeWork_workWasDone_isTrue() { //setup var aDependency:ISomeDependency = new SomeDependency(); //execute behavior aDependency.doSomeWork(); //verify expected state is valid assertThat(allTestsNeedThis.workWasDone, isTrue()); } @jhooks | 2010 | joelhooks.com