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
The Path to Unit Testing
Contact Information
Wes McClure
@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 Good Testing
Before Testing
Scenario
Weather on company portal. NOAA web service Input zip code Get latitude / longitude Show weather forecast
Where to start?
Build a Prototype? Console app
Move into web app
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×
It was tested … manually How?
Run program Provide input Execute action Subjectively verify output
Problems with Manual
Thrown away In the noggin
Not documented Easily forgotten
Documented Refactorability
Easy paths 1 bug fixed = 2 new
How can I fix this?
Automate
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
Unit Test Composition
AAA Arrange (Given) Act (When) Assert (Then)
Same as manual template
States
Passing Failing Not Run
Questions?
Before Testing
Unit Testing Frameworks
Test Discovery
Inheritance Naming ConvetionsAttributes
NUnit Test Discovery
Basic Attributes TestFixture Test Ignore Category
Test Execution
Exectuion Selection Run Results (State)
Types Console GUI IDE Integrated
NUnit Test Execution
Runners Console GUI MiniGUI
IDE Integrated TestDriven.Net R# Test Runner Gallio
Assertions
Classic Fluent
“Fluent is the new xml”
NUnit Assertion Syntax
Classic Assertions Fluent Constraints AssertionHelper
Expect Constraints
Is Has Throws
Putting it all together
Hello World Test
More NUnit Attributes
SetUp & TearDown
Per Test Once for Test Fixture Once for Namespace
Cautious
Parameterized Tests
TestCase Result TestName
Theory DataPoint
Caution
Attributes to Avoid
Random Tests should be repeatable!
Sequential Tests shouldn’t be interdependent!
Repeat Win the lottery?
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
Questions?
Unit Testing Frameworks (with NUnit)
Dependenciesanother word for "testable" is "decoupled"
Dependencies
Weather Dependencies
Input to SUT Acted upon
Unit Testing with Dependencies Unit Testing => Isolated Unit Stubs
Simulate Dependencies Return Dummy Data
Mocks Record Calls
Manual Stubs & Mocks
Roll our own
Cons Low reuse Boilerplate Complex
logic
Poll
Who has used an isolation framework?
Isolation Frameworks
Dynamic stubs Proxies Setup results
Dynamic mocks Record calls Verification syntax
Example
Isolation Frameworks
Rhino.Mocks TypeMock Moq NMock2
Questions?
Legacy Testing
Define Legacy
Old Untested – Michael Feathers Missing seams
Why Change Legacy Code? Fix Bugs
Repeats Add Features
Integrated Isolated
Improve Design 80/20
Improve Moral
Fixing Bugs
Example
Legacy Testing@martinfowler
@ JoshuaKerievsky
Questions?
Legacy Testing
Good Unit Tests
But…
Anything can be done but it takes effort to do it well!
Consistency in choices Best PracticesTry alternatives
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/
Readability
Naming Convention Method_Scenario_Expectation Explicit Variables
▪ Call stubs & mocks as such! Fluent Assertions
var
Readability
AAA Physical Separate
Readability
Single Assertion SRP methods
Transfer? Credit Debit Insufficient funds Transactional Success / Failure Auditing
Readability - Organized
Separate Tests project. Per app or project
1+ TestFixture Per class
1+ Test Per method
Maintainable
Fast Automated Zero Configuration IDE Integration Continuous Integration
DRY Factories
Independent
Trustworthy
RepeatableRetainedVersionedConfident
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)
Questions?
Good Unit Tests