automated unit testing for mere mortals

30
Automated Unit Testing for Mere Mortals Jess Chadwick Architect / RIA Specialist Dow Jones, Inc blog.jesschadwick.com @jchadwick

Upload: jess-chadwick

Post on 25-Jan-2015

486 views

Category:

Technology


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Automated Unit Testing for Mere Mortals

Automated Unit Testing for Mere Mortals

Jess ChadwickArchitect / RIA Specialist

Dow Jones, Inc

blog.jesschadwick.com@jchadwick

Page 2: Automated Unit Testing for Mere Mortals
Page 3: Automated Unit Testing for Mere Mortals
Page 4: Automated Unit Testing for Mere Mortals

What is an “automated unit test”?

What is a “unit”?

Page 5: Automated Unit Testing for Mere Mortals

Automated Tests…• Ensure components work as designed• Guard against regressions• Exercise the system• Expose how the system works

– Documentation

Page 6: Automated Unit Testing for Mere Mortals

Automated Unit Tests are…• Fast• Deterministic• Independent• Repeatable• Focused

Page 7: Automated Unit Testing for Mere Mortals

Unit tests…• Do not cross application / process boundaries

Page 8: Automated Unit Testing for Mere Mortals

INTEGRATION TESTS

Page 9: Automated Unit Testing for Mere Mortals

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!

Page 10: Automated Unit Testing for Mere Mortals

DEMO: A SAMPLE UNIT TEST

Page 11: Automated Unit Testing for Mere Mortals

TEST DOUBLESMocks and Stubs and Fakes… oh my!

Page 12: Automated Unit Testing for Mere Mortals

Test Doubles• Stand in for production components• Provide deterministic behavior• Provide control over testing environment

Page 13: Automated Unit Testing for Mere Mortals

Source: Unit Testing: Exploring The Continuum Of Test Doubles MSDN Magazine, September, 2007

Page 14: Automated Unit Testing for Mere Mortals

CODE COVERAGE

Page 15: Automated Unit Testing for Mere Mortals

Code Coverage is…

• The degree to which source code is tested

Page 16: Automated Unit Testing for Mere Mortals

RETROFITTING EXISTING CODE WITH TESTS

Page 17: Automated Unit Testing for Mere Mortals
Page 18: Automated Unit Testing for Mere Mortals

UNIT TEST ANTI-PATTERNS

Page 19: Automated Unit Testing for Mere Mortals

!! Warning !!

Page 20: Automated Unit Testing for Mere Mortals

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)

Page 21: Automated Unit Testing for Mere Mortals

Unit Test Anti-Patterns and Smells

• Multiple assertions

• No assertions

• Unclear / vague assertions

• Logging / Console.WriteLine()

Page 22: Automated Unit Testing for Mere Mortals

TEST-DRIVEN DEVELOPMENT

Page 23: Automated Unit Testing for Mere Mortals

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

Page 24: Automated Unit Testing for Mere Mortals

Test-First Development• No code can be written unless driven by a

failing test

• Red – Green - Refactor

Page 25: Automated Unit Testing for Mere Mortals

TDD Pair Programming Game• Player 1: Write failing test• Player 2: Make failing test pass• Player 1: Refactor• … repeat!

Page 26: Automated Unit Testing for Mere Mortals

IDEAS TO TAKE HOME

Page 27: Automated Unit Testing for Mere Mortals
Page 28: Automated Unit Testing for Mere Mortals
Page 29: Automated Unit Testing for Mere Mortals

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

Page 30: Automated Unit Testing for Mere Mortals