Testing with pytest
9 steps in 4 sets of Python.
pytest with the assert statement doing the work, which is most of why people use it. Writing tests, parametrizing them, and the three ways to check that something raised what it should have.
Then fixtures, which are the actual skill: scope, teardown, and composing them. Selecting and faking closes it out with marks, skips, monkeypatch and tmp_path. Nine steps, and it covers most of what you need to write a test suite you will not resent later.
Writing tests
- pytest assertionsA test is a function whose name starts with test and whose body asserts.
- Testing failurespytest.raises asserts that the call fails, and how.
- parametrizeOne test body, a row of cases, and a separate result for each.
Fixtures
- FixturesA fixture builds what a test needs, and tears it down afterwards.
- Built-in fixturestmp_path, monkeypatch and capsys cover most of what a test needs.
Selecting & faking
- Marks and skippingMarks label tests so a run can select or expect them.
- unittest.mockReplacing a collaborator so a test never touches the network.
- doctestExamples in the docstring that run as tests.
Encore
- test_suite.pyA pytest file as it looks in a real project: fixtures, parametrize, marks.