Test Doubles


Published on

GMU SWE-795 Test Driven Development Seminar. Spring 2011.

This is my initial contribution to the class.

Published in: Technology, Education
  • Be the first to comment

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • What are they?
  • What are test doubles?
  • Example dependencies: Databases, File I/O, network services, email …Show what dependecies look like, and how a simple design change can make code more testable (Dependency Injection vs. Instrumentation)
  • Show Emailer.java and BetterEmailer.java
  • Test Doubles

    1. 1. Test Doubles Mock Objects & Design Principles<br />SWE-795, Test Driven Development, GMU Spring 2011 – Bill Shelton<br />
    2. 2. “Once”, the Mock Turtle said at last, with a deep sigh, “I was a real turtle.”<br />Lewis Carroll, Alice’s Adventure in Wonderland<br />
    3. 3. Types of Test Doubles<br />Gerard Meszaros, xUnit Test Patterns, http://xunitpatterns.com/Test%20Double%20Patterns.html<br />Fakes, Stubs, Mocks, Spies, Dummies, …<br />Fowler draws lines between mocks and stubs, yet “mocking” tools do both.<br />
    4. 4. Problems Mocks solve<br />Test time dependency-related problems<br />Slow tests<br />Unavailable services or objects<br />*Isolation and control over object under test<br />*Side Effects (Koskella does not emphasize this)<br />Verifying interaction between SUT and collaborators (aka, object interactions)<br />Can drive the definition of unknown types<br />Need Driven Development (Nat Pryce, Steve Feeman)<br />#gooshttp://www.growing-object-oriented-software.com/<br />
    5. 5. Departure Into Dependency Land<br />Example code: https://gist.github.com/840494<br />
    6. 6. Ways to build Mocks<br />Code by hand (Fakes)<br />Extend the depended-upon object and override methods<br />Build lightweight representations<br />What does this smell like?<br />On-the-fly with a mocking framework<br />Lots to choose from<br />Almost all have some limitations<br />
    7. 7. State and Behavior Verification<br />State-based tests assert the resultant state of an operation on the object under test<br />Behavior-based tests verify how the object under test interacts with its collaborators<br />These probably should not be considered mutually exclusive activities<br />
    8. 8. <code/><br />
    9. 9. Classicists vs. Mockists<br /> Coupling to Implementation: Classicists think it’s important to only think about the external interface, whereas Mockistsmust consider the implementation …<br />What about considering various paths in state-based tests?<br />Test Isolation: Fowler seems to imply that this is less important to state-based tests <br />* Test Driven Design Idioms<br />Outside-in vs. Middle-out<br />“Tell Don’t Ask”<br />Law of Demeter, or nearest neighbor principle<br />
    10. 10. Discovering New Types<br />Freeman and Pryce describe Need-Driven Development as an outside-in approach<br />Thinking in terms of layers, starting with the outside first, build inwards using Mock Objects to discover interface needs<br />An interesting Agile idea, but in practice how does it work? <br />
    11. 11. Other Applications for Mocks?<br />Software simulation ?<br />Simulate faulty behavior or state …<br />Mutant behavior ?<br />Given a known behavior of the application of a specific mutant operator, could we simulate that using Mock Objects?<br />
    12. 12. Some mocking frameworks<br />Java<br />jMock, EasyMock, PowerMock, jMockit, Mockito<br />.NET<br />NMock, moq, EasyMock.NET, Rhino Mocks<br />Python<br />Pymox, dingus <br />ColdFusion<br />MightyMock (part of MXUnit), MockBox, ColdMock, CFEasyMock<br />Ruby<br />rr, mocha, flexmock, stump, facon<br />
    13. 13. Geeky Chuck Norris Jokes<br />Chuck Norris can divide by Zero.<br />Chuck Norris can count to infinity … twice!<br />Chuck Norris doesn’t do TDD. Bugs are too damn scared to go anywhere near his code.<br />
    14. 14. Complete Path Coverage<br />CPC<br />Prime Path Coverage<br />PPC<br />Edge-Pair Coverage<br />EPC<br />All-DU-Paths Coverage<br />ADUP<br />All-uses Coverage<br />AUC<br />All-defs Coverage<br />ADC<br />Edge Coverage<br />EC<br />Simple Round Trip Coverage<br />SRTC<br />Complete Round Trip Coverage<br />CRTC<br />Node Coverage<br />NC<br />Introduction to Software Testing (Ch 2)<br />© Ammann & Offutt<br />
    15. 15. Complete Path Coverage<br />CPC<br />Prime Path Coverage<br />PPC<br />Edge-Pair Coverage<br />EPC<br />All-DU-Paths Coverage<br />ADUP<br />All-uses Coverage<br />AUC<br />All-defs Coverage<br />ADC<br />Edge Coverage<br />EC<br />Simple Round Trip Coverage<br />SRTC<br />Complete Round Trip Coverage<br />CRTC<br />Node Coverage<br />NC<br />The<br />Chuck Norris<br />Criterion <br />subsumes ALL other<br />criteria. Period. The End.<br />Chuck NorrisCoverageCNC<br />Introduction to Software Testing (Ch 2)<br />© Ammann & Offutt<br />