Introduction to pytest

Simple, rapid and fun testing with Python

Florian Bruhin

Best Practice TDD Test Libraries (pytest/nose/...) Testing

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. We'll finish with discussing topics and questions of participants related to their own test suites and usages.

This is the planned outline:

- (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
* Exercises

- (60 minutes) pytest fixture mechanism:
* Setup and teardown via dependency injection
* Declaring and using function/module/session scoped fixtures
* Using fixtures from fixture functions
* Parametrizing fixtures
* Looking at useful built-in fixtures (managing temporary files, patching, output capturing)
* Exercises

- (30 minutes): Running existing nose/unittest/trial/Django suites with pytest:
* Discussing advantages and limitations
* Strategies for migrating to pytest
* Using "autouse" fixtures in conjunction with XUnit-based setup/teardown methods
* Exercises

- (30 minutes): Useful third-party plugins:
* Coverage integration
* Property-based testing (automated testcase generation) via Hypothesis

- (30 minutes): Open space for questions:
* Interactively solving pytest integration problems on real-life projects as time permits

Note that the structure of the workshop is similar to the ones I've given at Europython 2015 and 2016 in Bilbao.

Basic Python OOP knowledge (e.g. what a class/instance is) is required. Knowing what an "assertion" or a "decorator" are is a bonus, but those topics will also be explained briefly during the workshop.

Type: Training (180 mins); Python level: Intermediate; Domain level: Beginner

Florian Bruhin

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.