automated unit testing for mere mortals
DESCRIPTION
TRANSCRIPT
Automated Unit Testing for Mere Mortals
Jess ChadwickArchitect / RIA Specialist
Dow Jones, Inc
blog.jesschadwick.com@jchadwick
What is an “automated unit test”?
What is a “unit”?
Automated Tests…• Ensure components work as designed• Guard against regressions• Exercise the system• Expose how the system works
– Documentation
Automated Unit Tests are…• Fast• Deterministic• Independent• Repeatable• Focused
Unit tests…• Do not cross application / process boundaries
INTEGRATION TESTS
Integration Tests…• Ensure two or more system components work
together• Cross application / process boundaries
• Are unreliable (non-deterministic)– Encourage ignoring failing tests– This is very bad!
DEMO: A SAMPLE UNIT TEST
TEST DOUBLESMocks and Stubs and Fakes… oh my!
Test Doubles• Stand in for production components• Provide deterministic behavior• Provide control over testing environment
Source: Unit Testing: Exploring The Continuum Of Test Doubles MSDN Magazine, September, 2007
CODE COVERAGE
Code Coverage is…
• The degree to which source code is tested
RETROFITTING EXISTING CODE WITH TESTS
UNIT TEST ANTI-PATTERNS
!! Warning !!
Unit Test Anti-Patterns and Smells
• Test multiple components at once
• Slow– Cross application / process boundaries
• Depend on other tests or external interaction (non-deterministic)
Unit Test Anti-Patterns and Smells
• Multiple assertions
• No assertions
• Unclear / vague assertions
• Logging / Console.WriteLine()
TEST-DRIVEN DEVELOPMENT
A different way of thinking• Part of Agile / Extreme Programming beliefs• Tests drive system design
– TDD = “Test-Driven Design”– “How am I going to test this?”
• Helps create loosely-coupled components
Test-First Development• No code can be written unless driven by a
failing test
• Red – Green - Refactor
TDD Pair Programming Game• Player 1: Write failing test• Player 2: Make failing test pass• Player 1: Refactor• … repeat!
IDEAS TO TAKE HOME
Resources• My Blog: http://blog.jesschadwick.com • Unit Testing Frameworks
– Team System / MS Test– NUnit: http://www.nunit.org
• Visual Studio Add-ins– Test-Driven.Net: http://www.testdriven.net – ReSharper: http://www.jetbrains.com
• Mock Frameworks– Moq, Rhino Mocks, NMock, TypeMock, NUnit
Mocks, DotNetMock, EasyMock.NET