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.*"]
)
pytest has many tests for testing itself, under the
testing/directory in this repo. Many of the tests invokepytestand check its output and outcomes. pytest provides a fixture calledpytesterwhich aids in this, you can find its documentation here.The
pytesteritself is new in pytest 6.2 (unreleased yet when this is written), but its functionality is also provided by an older plugin calledtestdir. The two fixtures have almost the same API, the only difference is thattestdirtakes and returnspy.path.localobjects, whilepytesteris based onpathlib.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 usepytesterinstead. The instructions for converting are:testing/which usestestdir.tesdirand change it topytester. Beyond just replacing the name, some adjustments might be needed for usingpathlibPaths instead ofpy.path.localpaths.pytesterargument.Example:
Before:
After: