the path to unit testing

55
The Path to Unit Testing

Upload: calvin

Post on 22-Feb-2016

69 views

Category:

Documents


0 download

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 Presentation

TRANSCRIPT

Page 1: The Path to Unit Testing

The Path to Unit Testing

Page 3: The Path to Unit Testing

Poll – How do you test?

Manual Testing Automated Testing QA? Deploy and unplug the phone!

Page 4: The Path to Unit Testing

Agenda

Before Testing Unit Testing Frameworks Dependencies Legacy Testing Good Testing

Page 5: The Path to Unit Testing

Before Testing

Page 6: The Path to Unit Testing

Scenario

Weather on company portal. NOAA web service Input zip code Get latitude / longitude Show weather forecast

Page 7: The Path to Unit Testing

Where to start?

Build a Prototype? Console app

Move into web app

Page 8: The Path to Unit Testing

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

It was tested … manually How?

Run program Provide input Execute action Subjectively verify output

Page 10: The Path to Unit Testing

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

How can I fix this?

Automate

Page 12: The Path to Unit Testing

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

Unit Test Composition

AAA Arrange (Given) Act (When) Assert (Then)

Same as manual template

Page 14: The Path to Unit Testing

States

Passing Failing Not Run

Page 15: The Path to Unit Testing

Questions?

Before Testing

Page 16: The Path to Unit Testing

Unit Testing Frameworks

Page 17: The Path to Unit Testing

Test Discovery

Inheritance Naming ConvetionsAttributes

Page 18: The Path to Unit Testing

NUnit Test Discovery

Basic Attributes TestFixture Test Ignore Category

Page 19: The Path to Unit Testing

Test Execution

Exectuion Selection Run Results (State)

Types Console GUI IDE Integrated

Page 20: The Path to Unit Testing

NUnit Test Execution

Runners Console GUI MiniGUI

IDE Integrated TestDriven.Net R# Test Runner Gallio

Page 21: The Path to Unit Testing

Assertions

Classic Fluent

“Fluent is the new xml”

Page 22: The Path to Unit Testing

NUnit Assertion Syntax

Classic Assertions Fluent Constraints AssertionHelper

Expect Constraints

Is Has Throws

Page 23: The Path to Unit Testing

Putting it all together

Hello World Test

Page 24: The Path to Unit Testing

More NUnit Attributes

Page 25: The Path to Unit Testing

SetUp & TearDown

Per Test Once for Test Fixture Once for Namespace

Cautious

Page 26: The Path to Unit Testing

Parameterized Tests

TestCase Result TestName

Theory DataPoint

Caution

Page 27: The Path to Unit Testing

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

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

Questions?

Unit Testing Frameworks (with NUnit)

Page 30: The Path to Unit Testing

Dependenciesanother word for "testable" is "decoupled"

Page 31: The Path to Unit Testing

Dependencies

Page 32: The Path to Unit Testing

Weather Dependencies

Input to SUT Acted upon

Page 33: The Path to Unit Testing

Unit Testing with Dependencies Unit Testing => Isolated Unit Stubs

Simulate Dependencies Return Dummy Data

Mocks Record Calls

Page 34: The Path to Unit Testing

Manual Stubs & Mocks

Roll our own

Cons Low reuse Boilerplate Complex

logic

Page 35: The Path to Unit Testing

Poll

Who has used an isolation framework?

Page 36: The Path to Unit Testing

Isolation Frameworks

Dynamic stubs Proxies Setup results

Dynamic mocks Record calls Verification syntax

Example

Page 37: The Path to Unit Testing

Isolation Frameworks

Rhino.Mocks TypeMock Moq NMock2

Page 38: The Path to Unit Testing

Questions?

Page 39: The Path to Unit Testing

Legacy Testing

Page 40: The Path to Unit Testing

Define Legacy

Old Untested – Michael Feathers Missing seams

Page 41: The Path to Unit Testing

Why Change Legacy Code? Fix Bugs

Repeats Add Features

Integrated Isolated

Improve Design 80/20

Improve Moral

Page 42: The Path to Unit Testing

Fixing Bugs

Example

Page 43: The Path to Unit Testing

Legacy Testing@martinfowler

@ JoshuaKerievsky

Page 44: The Path to Unit Testing

Questions?

Legacy Testing

Page 45: The Path to Unit Testing

Good Unit Tests

Page 46: The Path to Unit Testing

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

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

Readability

Naming Convention Method_Scenario_Expectation Explicit Variables

▪ Call stubs & mocks as such! Fluent Assertions

var

Page 49: The Path to Unit Testing

Readability

AAA Physical Separate

Page 50: The Path to Unit Testing

Readability

Single Assertion SRP methods

Transfer? Credit Debit Insufficient funds Transactional Success / Failure Auditing

Page 51: The Path to Unit Testing

Readability - Organized

Separate Tests project. Per app or project

1+ TestFixture Per class

1+ Test Per method

Page 52: The Path to Unit Testing

Maintainable

Fast Automated Zero Configuration IDE Integration Continuous Integration

DRY Factories

Independent

Page 53: The Path to Unit Testing

Trustworthy

RepeatableRetainedVersionedConfident

Page 54: The Path to Unit Testing

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

Questions?

Good Unit Tests