On the Pros and Cons of Different Types of Test,
and Decoupling ORM code
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.
Decoupling our application from ORM code
When striving to write isolated tests, one of the consequences is that we find our‐
selves forced to remove ORM code from places like views and forms, by hiding it
behind helper functions or methods. This can be beneficial in terms of decoupling
your application from the ORM, but also just because it makes your code more
readable. As with all things, it’s a judgement call as to whether the additional effort
is worth it in particular circumstances.
364
|
Chapter 19: Test Isolation, and “Listening to Your Tests”