13522.bugfix.rst

Overview

This file documents a bug fix related to the `pytester` fixture in the pytest testing framework, specifically addressing issues with its behavior in subprocess mode when handling plugins. The fix resolves two main problems:

  1. The pytester fixture in subprocess mode was ignoring all plugins except the first one specified in the pytester.plugins attribute.

  2. The fixture silently ignored plugins specified as non-string types in pytester.plugins, which could lead to unexpected behavior without raising errors.

After the fix, the `pytester` fixture properly respects all plugins specified as strings and raises an error if a plugin is specified with a non-string type in subprocess mode. The documentation also advises users affected by this change to specify plugins by name (string) or alternatively use the `pytester.runpytest_inprocess` function for in-process test runs where plugin handling differs.

Detailed Explanation

Context: pytester Fixture and Plugins

Bugs Fixed

  1. Ignoring all but the first plugin

    • Before the fix, when running pytester in subprocess mode, only the first plugin in pytester.plugins was loaded.

    • This caused tests relying on multiple plugins to fail or behave incorrectly.

  2. Silent ignoring of non-string plugins

    • Plugins not specified as strings (for example, plugin objects or other types) were silently ignored.

    • This behavior was problematic because it masked configuration errors and led to unexpected test behavior.

Behavior After Fix

Usage Guidance

Example:

def test_with_plugins(pytester):
    # Correct: specify plugins by name (string)
    pytester.plugins = ["pytest_cov", "pytest_mock"]
    result = pytester.runpytest_subprocess()
    assert result.ret == 0
    
def test_with_object_plugins(pytester):
    # For plugin objects, use in-process run
    pytester.plugins = [MyPlugin()]
    result = pytester.runpytest_inprocess()
    assert result.ret == 0

Important Implementation Details

Interaction With Other Parts of the System

Visual Diagram

The following class diagram illustrates the relevant components related to `pytester` and plugin handling in the context of this fix:

classDiagram
    class Pytester {
        +plugins: List[str or object]
        +runpytest_subprocess()
        +runpytest_inprocess()
    }
    Pytester : +runpytest_subprocess() - subprocess mode runner, expects plugins as strings
    Pytester : +runpytest_inprocess() - in-process runner, accepts plugin objects
    class Plugin {
        <<interface>>
    }
    Pytester --> Plugin : uses plugins

Summary

This bug fix improves the robustness and correctness of the `pytester` fixture's subprocess mode by:

This enhances plugin testing fidelity and prevents subtle errors in plugin configuration during pytest plugin development and testing.