the path to unit testing
DESCRIPTION
The Path to Unit Testing. Contact Information. Wes McClure [email protected] [email protected] @g0t4. Poll – How do you test?. Manual Testing Automated Testing QA? Deploy and unplug the phone!. Agenda. Before Testing Unit Testing Frameworks Dependencies Legacy Testing - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/1.jpg)
The Path to Unit Testing
![Page 2: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/2.jpg)
Contact Information
Wes McClure
@g0t4
![Page 3: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/3.jpg)
Poll – How do you test?
Manual Testing Automated Testing QA? Deploy and unplug the phone!
![Page 4: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/4.jpg)
Agenda
Before Testing Unit Testing Frameworks Dependencies Legacy Testing Good Testing
![Page 5: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/5.jpg)
Before Testing
![Page 6: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/6.jpg)
Scenario
Weather on company portal. NOAA web service Input zip code Get latitude / longitude Show weather forecast
![Page 7: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/7.jpg)
Where to start?
Build a Prototype? Console app
Move into web app
![Page 8: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/8.jpg)
Ok Done!
What is done? Hanselminutes #119
Tested?
Time Detected
Requirements
Architecture
Construction
System Test
Post-Release
Time Introduced
Requirements 1× 3× 5–10× 10× 10–100×Architecture - 1× 10× 15× 25–100×Construction - - 1× 10× 10–25×
![Page 9: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/9.jpg)
It was tested … manually How?
Run program Provide input Execute action Subjectively verify output
![Page 10: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/10.jpg)
Problems with Manual
Thrown away In the noggin
Not documented Easily forgotten
Documented Refactorability
Easy paths 1 bug fixed = 2 new
![Page 11: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/11.jpg)
How can I fix this?
Automate
![Page 12: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/12.jpg)
Unit Testing
Definition (wikipedia) Is a software verification and validation
method in which a programmer tests if individual units of source code are fit for use. A unit is the smallest testable part of an application. … in object-oriented programming, the smallest unit is a class…
Testing the smallest piece
![Page 13: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/13.jpg)
Unit Test Composition
AAA Arrange (Given) Act (When) Assert (Then)
Same as manual template
![Page 14: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/14.jpg)
States
Passing Failing Not Run
![Page 15: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/15.jpg)
Questions?
Before Testing
![Page 16: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/16.jpg)
Unit Testing Frameworks
![Page 17: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/17.jpg)
Test Discovery
Inheritance Naming ConvetionsAttributes
![Page 18: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/18.jpg)
NUnit Test Discovery
Basic Attributes TestFixture Test Ignore Category
![Page 19: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/19.jpg)
Test Execution
Exectuion Selection Run Results (State)
Types Console GUI IDE Integrated
![Page 20: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/20.jpg)
NUnit Test Execution
Runners Console GUI MiniGUI
IDE Integrated TestDriven.Net R# Test Runner Gallio
![Page 21: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/21.jpg)
Assertions
Classic Fluent
“Fluent is the new xml”
![Page 22: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/22.jpg)
NUnit Assertion Syntax
Classic Assertions Fluent Constraints AssertionHelper
Expect Constraints
Is Has Throws
![Page 23: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/23.jpg)
Putting it all together
Hello World Test
![Page 24: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/24.jpg)
More NUnit Attributes
![Page 25: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/25.jpg)
SetUp & TearDown
Per Test Once for Test Fixture Once for Namespace
Cautious
![Page 26: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/26.jpg)
Parameterized Tests
TestCase Result TestName
Theory DataPoint
Caution
![Page 27: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/27.jpg)
Attributes to Avoid
Random Tests should be repeatable!
Sequential Tests shouldn’t be interdependent!
Repeat Win the lottery?
![Page 28: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/28.jpg)
Other attributes Localization
Culture SetCulture
Labeling Description Suite Property
Parametric Attributes▪ Values▪ ValueSource▪ Range
Combinatorial Pairwise TestCaseSource
Timing (async) MaxTime Timeout
Threading RequiresMTA RequiresSTA RequiresThread
Others Explicit RequiredAddIn Suite ExpectedException Platform
![Page 29: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/29.jpg)
Questions?
Unit Testing Frameworks (with NUnit)
![Page 30: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/30.jpg)
Dependenciesanother word for "testable" is "decoupled"
![Page 31: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/31.jpg)
Dependencies
![Page 32: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/32.jpg)
Weather Dependencies
Input to SUT Acted upon
![Page 33: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/33.jpg)
Unit Testing with Dependencies Unit Testing => Isolated Unit Stubs
Simulate Dependencies Return Dummy Data
Mocks Record Calls
![Page 34: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/34.jpg)
Manual Stubs & Mocks
Roll our own
Cons Low reuse Boilerplate Complex
logic
![Page 35: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/35.jpg)
Poll
Who has used an isolation framework?
![Page 36: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/36.jpg)
Isolation Frameworks
Dynamic stubs Proxies Setup results
Dynamic mocks Record calls Verification syntax
Example
![Page 37: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/37.jpg)
Isolation Frameworks
Rhino.Mocks TypeMock Moq NMock2
![Page 38: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/38.jpg)
Questions?
![Page 39: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/39.jpg)
Legacy Testing
![Page 40: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/40.jpg)
Define Legacy
Old Untested – Michael Feathers Missing seams
![Page 41: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/41.jpg)
Why Change Legacy Code? Fix Bugs
Repeats Add Features
Integrated Isolated
Improve Design 80/20
Improve Moral
![Page 42: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/42.jpg)
Fixing Bugs
Example
![Page 43: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/43.jpg)
Legacy Testing@martinfowler
@ JoshuaKerievsky
![Page 44: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/44.jpg)
Questions?
Legacy Testing
![Page 45: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/45.jpg)
Good Unit Tests
![Page 46: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/46.jpg)
But…
Anything can be done but it takes effort to do it well!
Consistency in choices Best PracticesTry alternatives
![Page 47: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/47.jpg)
Good Unit Tests
From “The Art of Unit Testing” Readable Maintainable Trustworthy
For us and others
@RoyOsherovehttp://www.artofunittesting.com/http://weblogs.asp.net/ROsherove/
![Page 48: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/48.jpg)
Readability
Naming Convention Method_Scenario_Expectation Explicit Variables
▪ Call stubs & mocks as such! Fluent Assertions
var
![Page 49: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/49.jpg)
Readability
AAA Physical Separate
![Page 50: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/50.jpg)
Readability
Single Assertion SRP methods
Transfer? Credit Debit Insufficient funds Transactional Success / Failure Auditing
![Page 51: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/51.jpg)
Readability - Organized
Separate Tests project. Per app or project
1+ TestFixture Per class
1+ Test Per method
![Page 52: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/52.jpg)
Maintainable
Fast Automated Zero Configuration IDE Integration Continuous Integration
DRY Factories
Independent
![Page 53: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/53.jpg)
Trustworthy
RepeatableRetainedVersionedConfident
![Page 54: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/54.jpg)
Resources
Uncle Bob Martin @unclebobmartin butunclebob.com
Podcasts (search for terms in archives) Hanselminutes▪ 169 - Roy Osherove▪ 119 – Unit Test Frameworks
DotNetRocks Test_Review_Guidelines (The Art of Unit
Testing)
![Page 55: The Path to Unit Testing](https://reader030.vdocuments.mx/reader030/viewer/2022033102/5681611b550346895dd0740d/html5/thumbnails/55.jpg)
Questions?
Good Unit Tests