xunit_setup.rst

Overview

This document explains how to implement **xunit-style setup and teardown** methods within test modules, classes, and functions when using the pytest testing framework. It covers a classic and widely recognized pattern for managing test fixtures on a per-module, per-class, and per-function/method basis.

The xunit-style setup/teardown approach is particularly familiar to developers coming from `unittest` or `nose` testing frameworks. Although pytest provides a more powerful and flexible fixture mechanism leveraging dependency injection, xunit-style setup remains useful for simple and direct test state management or for gradual migration between testing styles.

This file describes:

It explains their expected behavior, parameters, and interaction with pytest’s fixture system.


Module Level Setup/Teardown

Functions

def setup_module(module):
    """Setup any state specific to the execution of the given module."""
def teardown_module(module):
    """Teardown any state previously setup by setup_module."""

Class Level Setup/Teardown

Methods

@classmethod
def setup_class(cls):
    """Setup any state specific to the execution of the given test class."""
@classmethod
def teardown_class(cls):
    """Teardown any state previously setup by setup_class."""

Method and Function Level Setup/Teardown

For Test Classes

def setup_method(self, method):
    """Setup any state tied to the specific test method."""
def teardown_method(self, method):
    """Teardown any state previously setup by setup_method."""

For Module-level Test Functions

def setup_function(function):
    """Setup any state tied to the specific test function."""
def teardown_function(function):
    """Teardown any state previously setup by setup_function."""

Important Implementation Details and Remarks


Interaction with Other Parts of the System


Visual Diagram: xUnit-Style Setup/Teardown Structure

flowchart TD
    subgraph Module
        A[setup_module(module)?] -->|runs once before module tests| B[Test functions/classes]
        B --> C[teardown_module(module)?]
    end

    subgraph Class
        D[setup_class(cls)?] -->|runs once before class tests| E[Test methods]
        E --> F[teardown_class(cls)?]
    end

    subgraph Method/Function
        G[setup_method(self, method)?] -->|runs before each test method| H[Test method]
        H --> I[teardown_method(self, method)?]

        J[setup_function(function)?] -->|runs before each test function| K[Test function]
        K --> L[teardown_function(function)?]
    end

    B -- contains --> D
    B -- contains --> J
    E -- contains --> G
    H -- is --> L

Summary

This file documents the classic xunit-style setup and teardown methods available in pytest to manage test resources and states at different scopes:

While pytest fixtures offer more flexibility, these traditional hooks remain useful for straightforward test setups and for teams transitioning from other test frameworks.

Use these hooks to prepare and cleanup test state, ensuring test isolation and resource management, with full compatibility and integration into pytest’s test lifecycle from version 4.2 onwards.