This is the index page for a series of posts about our approach to Python unit tests at Hypothesis:
- Running the Hypothesis Python Tests
- Debugging Failing Tests with pytest
- Writing Simple Python Unit Tests
- Arrange, Act, Assert
- Python Test Factories with factory_boy
- Testing that an Exception is Raised with pytest.raises
- Parametrizing Python Tests
- Basic pytest Fixtures
- Advanced pytest Fixtures
- Python’s unittest.mock
- Hypothesis’s patch Fixture
- sentinel: Unique Objects for Tests
- The Problem with Mocks
- usefixtures as a Class Decorator
- When and When Not to Use Mocks
- Matcher Objects in Python Tests
While this tutorial is based on Hypothesis’s approach and the examples are taken from Hypothesis’s tests, most of it should be applicable to Python unit testing generally.
In a large, real-world project like Hypothesis the tests contain a lot of
things that might be unfamiliar - factories,
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.