The pytest tool presents a rapid and simple way to write tests for your Python code. This training gives an introduction with exercises to some distinguishing features. We'll also examine how to run existing non-pytest test suites and discuss migration strategies. Various plugins which extend pytest's functionality even further will be introduced.
The workshop uses Python 3.5 (or later), it'd be good if you could set it up before the workshop starts. If you know how, set up a virtualenv with pytest and hypothesis installed - if you don't, that's no problem, I'll cover it at the beginning of the workshop!
Code needed for the exercises can be found at https://t.cmpl.cc/pycon-de-2019.zip
(30 minutes) pytest feature walkthrough:
- Automatic test discovery
- Assertions without boilerplate via the assert statement
- Configuration and commandline options
- Marking and skipping tests
- Data-driven tests via parametrization
(30 minutes) pytest fixture mechanism:
- Setup and teardown via dependency injection
- Declaring and using function/module/session scoped fixtures
- Using fixtures from fixture functions
- Looking at useful built-in fixtures (managing temporary files, patching, output capturing)
(15 minutes): Running existing unittest suites with pytest:
- Discussing advantages and limitations
- Strategies for migrating to pytest
(15 minutes): Useful third-party plugins:
- Overview of several pytest plugins
Basic Python OOP knowledge (e.g. what a class/instance is) is required.
Florian Bruhin ("The Compiler") is a long-time contributor and maintainer of both the pytest framework and various plugins. In 2013, he started the qutebrowser project, a keyboard-focused web browser based on Python and Qt. In 2015, he discovered pytest - since then, he has given talks and conducted workshops about pytest at various conferences and companies.