Skip to content

testing: migrate pytest's own tests from the testdir fixture to the pytester fixture #7942

@bluetech

Description

@bluetech

pytest has many tests for testing itself, under the testing/ directory in this repo. Many of the tests invoke pytest and check its output and outcomes. pytest provides a fixture called pytester which aids in this, you can find its documentation here.

The pytester itself is new in pytest 6.2 (unreleased yet when this is written), but its functionality is also provided by an older plugin called testdir. The two fixtures have almost the same API, the only difference is that testdir takes and returns py.path.local objects, while pytester is based on pathlib.Path, which we are trying to transition to.

Almost all of pytest's test are currently written using testdir. This issue is about migrating to use pytester instead. The instructions for converting are:

  • Pick a file under testing/ which uses testdir.
  • Go over each test which uses tesdir and change it to pytester. Beyond just replacing the name, some adjustments might be needed for using pathlib Paths instead of py.path.local paths.
  • Bonus points: add a type annotation for the pytester argument.

Example:

Before:

def test_rootdir_wrong_option_arg(testdir):
    result = testdir.runpytest("--rootdir=wrong_dir")
    result.stderr.fnmatch_lines(
        ["*Directory *wrong_dir* not found. Check your '--rootdir' option.*"]
    )

After:

# At this import at the top of the file.
from _pytest.pytester import Pytester

def test_rootdir_wrong_option_arg(pytester: Pytester) -> None:
    result = pytester.runpytest("--rootdir=wrong_dir")
    result.stderr.fnmatch_lines(
        ["*Directory *wrong_dir* not found. Check your '--rootdir' option.*"]
    )

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueeasy issue that is friendly to new contributortype: selftestsa problem in the tests of pytest

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions