Outside-In, Test Isolation Versus Integrated Tests, and
Mocking
Be reminded of the reason we write tests in the first place:
• To ensure correctness, and prevent regressions
• To help us to write clean, maintainable code
• To enable a fast, productive workflow
And with those objectives in mind, think of different types of tests, and the tradeoffs
between them:
Functional tests
• Provide the best guarantee that your application really works correctly, from
the point of view of the user.
• But: it’s a slower feedback cycle,
• And they don’t necessarily help you write clean code.
Integrated tests (reliant on, eg, the ORM or the Django Test Client)
• Are quick to write,
• Easy to understand,
• Will warn you of any integration issues,
• But may not always drive good design (that’s up to you!).
• And are usually slower than isolated tests.
Isolated (“mocky”) tests
• These involve the most hard work.
• They can be harder to read and understand,
• But: these are the best ones for guiding you towards better design.
• And they run the fastest.
If you do find yourself writing tests with lots of mocks, and they feel painful, remember
“listen to your tests”
—ugly, mocky tests may be trying to tell you that your code could
be simplified.
Relevant chapters:
Chapter 18,
Chapter 19 , Chapter 22440
|
Appendix F: Cheat Sheet