550 likes | 798 Views
The Path to Unit Testing. Contact Information. Wes McClure wes.mcclure@gmail.com wmcclure@phxwg.com @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
E N D
Contact Information • Wes McClure • wes.mcclure@gmail.com • wmcclure@phxwg.com • @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
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?
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
Test Discovery • Inheritance • Naming Convetions • Attributes
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
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)
Dependencies another word for "testable" is "decoupled"
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
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
But… • Anything can be done but it takes effort to do it well! • Consistency in choices • Best Practices • Try alternatives
Good Unit Tests • From “The Art of Unit Testing” • Readable • Maintainable • Trustworthy • For us and others @RoyOsherove http://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