Talks - Paul Ganssle: pytest for unittesters

Learn how pytest improves on unittest with better error messages, simpler assertions, powerful fixtures, and an active plugin ecosystem - while allowing gradual migration.

Key takeaways
  • PyTest is now considered the standard way to do testing in Python, with broad ecosystem support and active development

  • Allows incremental migration from unittest - can mix PyTest and unittest code in the same codebase while transitioning

  • Key advantages over unittest:

    • Uses plain assert statements instead of assertion methods
    • Provides detailed error messages with actual vs expected values
    • Optional class usage - tests can be simple functions
    • Powerful fixture system for test setup/teardown
    • Built-in parameterized testing support
    • Extensive plugin ecosystem
  • Fixtures provide modular, composable test setup:

    • Can be used across multiple tests
    • Support setup and teardown via yield
    • Can be stacked and combined
    • Allow parameter passing
  • Strong debugging capabilities:

    • –pdb flag drops into debugger on test failure
    • -L flag shows local variables
    • –stepwise mode to run from last failure
    • xdist plugin for parallel test execution
  • Configuration flexibility:

    • Highly customizable through plugins
    • Supports various output formats
    • Can maintain existing unittest conventions while adopting PyTest features
  • Common concerns about “magic” behavior haven’t been problematic in practice:

    • Test discovery conventions are configurable
    • Assertion rewriting provides better error messages
    • Core functionality is stable and well-tested
  • Testing Django:

    • Works well with django-pytest plugin
    • Some database fixture scenarios need special handling
    • Active community support for Django integration