Test Doubles


Unleash the Doppelgängers


Franck Arrecot & Kevin Ottens

Akademy 2015

knarf

UPS ~ Zanshin ~ KDAB

ervin

KDE ~ PhD ~ UPS ~ KDAB

TDD !!!

Test Doubles

In automated unit testing, it may be necessary to use objects or procedures that look and behave like their release-intended counterparts, but are actually simplified versions that reduce the complexity and facilitate testing.

(Wikipedia)

continuum

Doppelgängers?

In fiction and folklore, a doppelgänger is a look-alike or double of a living person, sometimes portrayed as a paranormal phenomenon […]

(Wikipedia)

Dummies

Dummies are used when a parameter is needed for the tested method but without actually needing to use the parameter

Kevin? You’re boring…

Franck? Please show some code!

  • ✔ Can be as simple as `nullptr`
  • ✔ Reduce the amount of collaborators in tests
  • ✘ Not enforcing anything
  • ✘ Code under test has to cope with dummies

Stubs

Stubs are used for providing the tested code with indirect input

Kevin? Still here? really??

Franck? The stage is your!

  • ✔ Fosters simple readable tests
  • ✔ Can be done with mocking frameworks
  • ✘ No clue on how results are obtained
  • ✘ Might push to write extra classes

Mocks

Mocks are used for verifying indirect output of the tested code

Kevin? *yawn*

Go Franck! Go!

  • ✔ Better test isolation
  • ✔ Verify protocols between objects
  • ✘ More verbose test code
  • ✘ Makes refactoring harder

Fakes

Fakes are used as a simpler implementation of a system (e.g. in-memory database)

Kevin? Please… make it stop…

Franck? Impress us with a last example!

  • ✔ Simplifies test code
  • ✔ Potential test speed up
  • ✘ Lots of extra code to maintain
  • ✘ Generally has its own test suite

No silver-bullet??

Nope…

Proposed approach

Use Dummies…

Otherwise:

prefer Stubs

Mock temporarily

Fakes are investments

Thank you!