Testing with Python

Tests are not a chore you do at the end, they are what lets you change code without fear. We start with the why, move from unittest to pytest, learn to isolate code with mocks and to generate realistic data, then industrialise the whole suite.

Duration: 3 days · Level: intermediate

For: Python developers who want to master automated testing.

Prerequisites: a basic knowledge of Python and programming.

Testing with Python

Day 1 — Testing fundamentals

  • Why test? Regressions, quality, specs, responsibility, trust
  • A first contact with unittest: asserts, setUp and tearDown
  • Moving to pytest: plain assert and fixtures
  • Useful command line options
  • Organising a test project: layout, pyproject.toml, conftest.py
  • Unit tests vs integration tests
  • End-to-end tests with Playwright
  • Testing forms and user interfaces
  • Waiting for elements without sleep()
  • Choosing the right level of test

Day 2 — Advanced techniques

  • Mocks and test doubles: Mock, MagicMock and patch
  • When and how to mock
  • Realistic data with Mimesis
  • Freezing time with freezegun
  • An in-memory file system with pyfakefs
  • Recording HTTP with VCR.py
  • Snapshots with inline-snapshot
  • Design for testability: dependency injection
  • pytest.mark.parametrize
  • Property-based testing with Hypothesis

Day 3 — Industrialising your tests

  • Measuring coverage with pytest-cov
  • Setting realistic coverage goals
  • Several Python versions and dependencies with nox
  • Parallel tests with pytest-xdist
  • Selecting tests and caching fixtures
  • Profiling a slow test suite
  • CI: GitHub Actions / GitLab CI
  • Pre-commit hooks
  • Testing as a team, and maintaining the suite
  • For less technical users: Robot Framework

Elevate Your Python Proficiency!