-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Description
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 usestestdir. - Go over each test which uses
tesdirand change it topytester. Beyond just replacing the name, some adjustments might be needed for usingpathlibPaths instead ofpy.path.localpaths. - Bonus points: add a type annotation for the
pytesterargument.
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.*"]
)