PyCon Pittsburgh. April 15-23, 2020.

Talk: Stop Using Mocks (for a while)

Presented by:

Harry Percival


Classic testing tutorials usually present code that’s free of side-effects and external I/O dependencies, but the real world is seldom so straightforward. We find ourselves reaching for mocks to keep our tests isolated, and that often leads to tests that are hard to understand and maintain, and we start to question their value.

When we substitute integration or end-to-end tests to assure ourselves that things work in the real world, we end up with test suites getting slower and slower, and instead of a “test pyramid” we end up with an “ice cream cone”.

This talk proposes a cure: if we stop ourselves from using mocks, cold turkey, we will be forced to think more carefully about how to design our code, how to separate business logic from integration and I/O concerns. The talk will cover patterns like “Functional Core, Imperative Shell”, the use of Dependency Injection and hand-rolled fakes as an alternative to mocking and patching, and show how they can help us get better-designed code, more maintainable tests, and a healthy test pyramid.