Test Doubles

16
Test Doubles Mock Objects & Design Principles SWE-795, Test Driven Development, GMU Spring 2011 – Bill Shelton

description

GMU SWE-795 Test Driven Development Seminar. Spring 2011. This is my initial contribution to the class.

Transcript of Test Doubles

Page 1: Test Doubles

Test Doubles Mock Objects & Design Principles

SWE-795, Test Driven Development, GMU Spring 2011 – Bill Shelton

Page 2: Test Doubles

“Once”, the Mock Turtle said at last, with

a deep sigh, “I was a real

turtle.”

Lewis Carroll, Alice’s Adventure in Wonderland

Page 3: Test Doubles

Types of Test Doubles

• Gerard Meszaros, xUnit Test Patterns, http://xunitpatterns.com/Test%20Double%20Patterns.html

• Fakes, Stubs, Mocks, Spies, Dummies, etc.• Fowler draws lines between mocks and stubs,

yet “mocking” tools do both.• Let’s just call these “Mocks” and classify how

they behave and how they’re used

Page 4: Test Doubles

Problems Mocks solve

• Test-time dependency related problems– Slow tests– Unavailable services or objects– Lack of isolation and control over collaborating objects– *Side Effects (Koskela or Fowler don’t emphasize this)

• Verifying interaction between object under test (or SUT) and collaborators

• Can drive the definition of unknown types– Need Driven Development (Nat Pryce, Steve Feeman)– #goos http://www.growing-object-oriented-software.com/

Page 5: Test Doubles

Dependency

Example code: https://gist.github.com/840494

Page 6: Test Doubles

Ways to build Mocks

• Code by hand (Fakes)– Extend the depended-upon object and override

methods– Build lightweight representations; e.g.,

anonymous classes– What does this smell like?

• On-the-fly with a mocking framework– Lots to choose from– Almost all have some limitations

Page 7: Test Doubles

State and Behavior Verification

• State-based tests assert the resultant state of an operation on the object under test

• Behavior-based tests verify how the object under test interacts with its collaborators

• Should these be considered mutually exclusive activities?

Page 8: Test Doubles

<code/>: https://gist.github.com/840494

Page 9: Test Doubles

Classicists and Mockists(Fowler)

• Coupling to Implementation: Classicists think it’s important to only think about the external interface, whereas Mockists must consider the implementation …– What about considering various paths in state-based tests?

Classicist do need to look beyond the interface. This is a design activity.

• Test Isolation: Fowler seems to imply that this is less important to state-based tests

• * Test Driven Design Idioms (Let’s see some case studies)– Outside-in vs. Middle-out– “Tell Don’t Ask”– Law of Demeter, or nearest neighbor principle

Page 10: Test Doubles

Discovering New Types

• Freeman and Pryce describe Need-Driven Development as an outside-in approach

• Thinking in terms of layers, starting with the outside first, build inwards using Mock Objects to discover interface needs– An interesting Agile idea, but in practice how does

it work? Case study?• Process: Refer to earlier Dependency Slide– Collaborators evolve and become SUT

Page 11: Test Doubles

Summary

• Mocks can be used for:– Managing test-time dependencies (databases, network

services, email, etc.)– Verifying how the object under test interacts with its

collaborators– Driving an outside-in design process

• Discovery of new types

• Other points– Decouple dependencies in code to allow injection of

mocks; e.g., setCollaborator(Collaborator c) – Consider control and isloation

Page 12: Test Doubles

Other Applications for Mocks?

• It appears that Mocks have only been considered as an aid to testing, design, and TDD. If Mocks can simulate behavior, could they be used for other purposes?

• Software simulation– Simulate faulty behavior or state …

• Mutant behavior– Given a known behavior of the application of a specific

mutant operator, could we simulate that using Mock Objects?

Page 13: Test Doubles

Some mocking frameworks

• Java– jMock, EasyMock, PowerMock, jMockit, Mockito

• .NET– NMock, moq, EasyMock.NET, Rhino Mocks

• Python– Pymox, dingus

• ColdFusion– MightyMock (part of MXUnit), MockBox, ColdMock,

CFEasyMock• Ruby

– rr, mocha, flexmock, stump, facon

Page 14: Test Doubles

Geeky Chuck Norris Jokes

• Chuck Norris can divide by Zero.

• Chuck Norris can count to infinity … twice!

• Chuck Norris doesn’t do TDD. Bugs are too damn scared to go anywhere near his code.

Page 15: Test Doubles

Introduction to Software Testing (Ch 2) © Ammann & Offutt

Simple Round Trip Coverage

SRTCNode Coverage

NC

Edge Coverage

EC

Edge-Pair Coverage

EPC

Prime Path Coverage

PPC

Complete Path Coverage

CPC

Complete Round Trip Coverage

CRTC

All-DU-Paths Coverage

ADUP

All-uses Coverage

AUC

All-defs Coverage

ADC

Page 16: Test Doubles

Introduction to Software Testing (Ch 2) © Ammann & Offutt

Simple Round Trip Coverage

SRTCNode Coverage

NC

Edge Coverage

EC

Edge-Pair Coverage

EPC

Prime Path Coverage

PPC

Complete Path Coverage

CPC

Complete Round Trip Coverage

CRTC

All-DU-Paths Coverage

ADUP

All-uses Coverage

AUC

All-defs Coverage

ADC

Chuck NorrisCoverage

CNC

TheChuck Norris

Criterion subsumes ALL othercriteria. Period. The End.