unit testing - the star wars version
DESCRIPTION
Introduction to unit testing and mocking as seen in a galaxy far away...TRANSCRIPT
![Page 1: Unit testing - The Star Wars version](https://reader036.vdocument.in/reader036/viewer/2022062300/554fb736b4c9050e7d8b4627/html5/thumbnails/1.jpg)
The Star Wars Version
Gil Zilberfeld
Unit Testing
![Page 2: Unit testing - The Star Wars version](https://reader036.vdocument.in/reader036/viewer/2022062300/554fb736b4c9050e7d8b4627/html5/thumbnails/2.jpg)
Bugs
How do we deal with them?
![Page 3: Unit testing - The Star Wars version](https://reader036.vdocument.in/reader036/viewer/2022062300/554fb736b4c9050e7d8b4627/html5/thumbnails/3.jpg)
xUnit Frameworks
• Test Identification• Test Runner• Asserts
![Page 4: Unit testing - The Star Wars version](https://reader036.vdocument.in/reader036/viewer/2022062300/554fb736b4c9050e7d8b4627/html5/thumbnails/4.jpg)
.NET xUnit Frameworks
• NUnit• MSTest• xUnit• MbUnit
![Page 5: Unit testing - The Star Wars version](https://reader036.vdocument.in/reader036/viewer/2022062300/554fb736b4c9050e7d8b4627/html5/thumbnails/5.jpg)
Demo
![Page 6: Unit testing - The Star Wars version](https://reader036.vdocument.in/reader036/viewer/2022062300/554fb736b4c9050e7d8b4627/html5/thumbnails/6.jpg)
Why Unit Test?
• Low TCO• Small setup
overhead• It’s not a bug if
it’s caught before QA
• Go faster
• Quick feedback• Knowing your
code works• Confidence to
change your code• Quicker than
debugging
![Page 7: Unit testing - The Star Wars version](https://reader036.vdocument.in/reader036/viewer/2022062300/554fb736b4c9050e7d8b4627/html5/thumbnails/7.jpg)
The death spiral
• Tests are slow• Stop running tests• Stop writing tests• Back to square one
![Page 8: Unit testing - The Star Wars version](https://reader036.vdocument.in/reader036/viewer/2022062300/554fb736b4c9050e7d8b4627/html5/thumbnails/8.jpg)
People write millions of lines of legacy code
every day!
![Page 9: Unit testing - The Star Wars version](https://reader036.vdocument.in/reader036/viewer/2022062300/554fb736b4c9050e7d8b4627/html5/thumbnails/9.jpg)
Testing legacy code
• Tests are slow• Require big setup• Maybe impossible to run• Slow to debug
![Page 10: Unit testing - The Star Wars version](https://reader036.vdocument.in/reader036/viewer/2022062300/554fb736b4c9050e7d8b4627/html5/thumbnails/10.jpg)
Isolation and Mocking
• Let you test your code for different scenarios, by faking the dependencies
• As long as your code is ready for it
![Page 11: Unit testing - The Star Wars version](https://reader036.vdocument.in/reader036/viewer/2022062300/554fb736b4c9050e7d8b4627/html5/thumbnails/11.jpg)
Demo: Hand-Rolled Mocks
![Page 12: Unit testing - The Star Wars version](https://reader036.vdocument.in/reader036/viewer/2022062300/554fb736b4c9050e7d8b4627/html5/thumbnails/12.jpg)
.NET Mocking Frameworks
• Open-source• Commercial
![Page 13: Unit testing - The Star Wars version](https://reader036.vdocument.in/reader036/viewer/2022062300/554fb736b4c9050e7d8b4627/html5/thumbnails/13.jpg)
Demo: Mocking Framework
![Page 14: Unit testing - The Star Wars version](https://reader036.vdocument.in/reader036/viewer/2022062300/554fb736b4c9050e7d8b4627/html5/thumbnails/14.jpg)
Coverage…
… and other Jedi mind
tricks
![Page 15: Unit testing - The Star Wars version](https://reader036.vdocument.in/reader036/viewer/2022062300/554fb736b4c9050e7d8b4627/html5/thumbnails/15.jpg)
Test Driven Development
![Page 16: Unit testing - The Star Wars version](https://reader036.vdocument.in/reader036/viewer/2022062300/554fb736b4c9050e7d8b4627/html5/thumbnails/16.jpg)
Demo
![Page 17: Unit testing - The Star Wars version](https://reader036.vdocument.in/reader036/viewer/2022062300/554fb736b4c9050e7d8b4627/html5/thumbnails/17.jpg)
Benefits
• Same Tools • Better Coverage• Less debugging• Better Design
Think before your code!
![Page 18: Unit testing - The Star Wars version](https://reader036.vdocument.in/reader036/viewer/2022062300/554fb736b4c9050e7d8b4627/html5/thumbnails/18.jpg)
But…
• Requires discipline• Works better for greenfield
projects• Not for everyone
![Page 19: Unit testing - The Star Wars version](https://reader036.vdocument.in/reader036/viewer/2022062300/554fb736b4c9050e7d8b4627/html5/thumbnails/19.jpg)
![Page 20: Unit testing - The Star Wars version](https://reader036.vdocument.in/reader036/viewer/2022062300/554fb736b4c9050e7d8b4627/html5/thumbnails/20.jpg)
Agile Adoption
![Page 21: Unit testing - The Star Wars version](https://reader036.vdocument.in/reader036/viewer/2022062300/554fb736b4c9050e7d8b4627/html5/thumbnails/21.jpg)
![Page 22: Unit testing - The Star Wars version](https://reader036.vdocument.in/reader036/viewer/2022062300/554fb736b4c9050e7d8b4627/html5/thumbnails/22.jpg)
Working software
• Automated builds• Automatic build• Run unit tests• Run integration tests
• Unit tests and TDD
Over comprehensive documentation
![Page 23: Unit testing - The Star Wars version](https://reader036.vdocument.in/reader036/viewer/2022062300/554fb736b4c9050e7d8b4627/html5/thumbnails/23.jpg)
Guidelines
• Unit testing is a team
sport
• Where to start?
• Scaffolding
![Page 24: Unit testing - The Star Wars version](https://reader036.vdocument.in/reader036/viewer/2022062300/554fb736b4c9050e7d8b4627/html5/thumbnails/24.jpg)
Guidelines
• Naming
• Reviewing
• AAA
![Page 25: Unit testing - The Star Wars version](https://reader036.vdocument.in/reader036/viewer/2022062300/554fb736b4c9050e7d8b4627/html5/thumbnails/25.jpg)
Guidelines
• Refactoring
• Readability
• Coverage
• Organization
![Page 26: Unit testing - The Star Wars version](https://reader036.vdocument.in/reader036/viewer/2022062300/554fb736b4c9050e7d8b4627/html5/thumbnails/26.jpg)
www.typemock.com
typemock.com/blog
@typemock
www.gilzilberfeld.com
@gil_zilberfeld
Questions?
![Page 27: Unit testing - The Star Wars version](https://reader036.vdocument.in/reader036/viewer/2022062300/554fb736b4c9050e7d8b4627/html5/thumbnails/27.jpg)
Death star explosion probability calculator• Only Luke can shoot correctly• Explode if core temperature > 500• 1000 X-Wings• Probability of Luke being hit depends on the
other X-Wingsp(success) = 0.3(X-Wings)
• Probability of X-Wing surviving degrades in time
p(survival) = 1/t2
• Calculate Luke's hitting probability every 100 time units