Python Unit Testing Notes

This series of posts is a tutorial for an approach to unit testing in Python - the approach used by Hypothesis’s Python web app h (yes, the app has a single-letter name, its code can be found at using tox, pytest, mock and factory_boy.

  1. Running the Tests
  2. Debugging a Failing Test
  3. Writing Simple Tests
  4. Testing that an Exception is Raised
  5. Arrange, Act, Assert
  6. Factories
  7. Parametrize
  8. Fixtures
  9. Advanced Fixtures
  10. Mock
  11. The Problem with Mocks
  12. When and When Not to Use Mocks
  13. Sentinel
  14. Patch
  15. usefixtures as a Class Decorator
  16. Matcher Objects

While this tutorial is based on h’s approach and the examples are taken from h’s tests, most of it should be applicable to Python unit testing generally.

These posts are aimed at intermediate Python developers, not complete beginners. Basic knowledge of Python programming and virtualenv are assumed, for example.

In a large, real-world project like h the tests contain a lot of things that might be unfamiliar - factories, parametrize, mock, sentinel, patch, usefixtures and test method arguments that seem to come from nowhere. One of the aims of this tutorial is to introduce you to all these, so that you can read the Hypothesis test code and understand what’s going on.

Knowing how and when to use all of these tools will help you to write better tests. We’ll also cover a few more advanced aspects of writing good tests, as opposed to bad ones, particularly when we talk about how to organize your tests, the arrange, act, assert pattern and test naming, fixtures, and how and when to use mocks.

Finally, we’ll touch even less on the important but difficult topic of how to design good code that works well and is easy to test.

I also hope that these posts will serve as a reference for myself, that I can update as my thoughts on testing evolve, and that I can use when I start a new codebase (Python or otherwise) and want to write good tests for it.

To get started, the first post is Running the Tests…