Python Unit Tests at Hypothesis
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.
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 Hypothesis the tests contain a lot of
things that might be unfamiliar - factories, parametrize
, mocks, 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.
All posts tagged “Hypothesis”:
-
Nov 2018
An Opinionated tox.ini File -
Nov 2017
PDF Fingerprinting -
Jan 2017
Python Unit Tests at Hypothesis -
Oct 2016
Code Review
All posts tagged “Python Unit Tests at Hypothesis”:
-
May 2017
Matcher Objects in Python Tests -
Apr 2017
When and When Not to Use Mocks -
Mar 2017
usefixtures as a Class Decorator -
Mar 2017
The Problem with Mocks -
Mar 2017
sentinel: Unique Objects for Tests -
Mar 2017
Hypothesis’s patch Fixture -
Mar 2017
Python’s unittest.mock -
Feb 2017
Advanced pytest Fixtures -
Feb 2017
Basic pytest Fixtures -
Jan 2017
Parametrizing Python Tests -
Jan 2017
Testing that an Exception is Raised with pytest.raises -
Jan 2017
Arrange, Act, Assert -
Jan 2017
Writing Simple Python Unit Tests -
Jan 2017
Debugging Failing Tests with pytest -
Jan 2017
Running the Hypothesis Python Tests -
Jan 2017
Python Unit Tests at Hypothesis