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 https://github.com/hypothesis/h) using tox, pytest, mock and factory_boy.
- Running the Tests
- Debugging a Failing Test
- Writing Simple Tests
- Testing that an Exception is Raised
- Arrange, Act, Assert
- Advanced Fixtures
- The Problem with Mocks
- When and When Not to Use Mocks
- usefixtures as a Class Decorator
- 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.
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…
— Sean Hammond,