Skip to content

Commit 6963d00

Browse files
committed
Code review improvements
1 parent 8b11c76 commit 6963d00

File tree

5 files changed

+79
-73
lines changed

5 files changed

+79
-73
lines changed

changelog/7425.feature.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ New :fixture:`pytester` fixture, which is identical to :fixture:`testdir` but it
22

33
This is part of the movement to use :class:`pathlib.Path` objects internally, in order to remove the dependency to ``py`` in the future.
44

5-
Internally, the old :class:`Testdir` is now a thin wrapper around :class:`PyTester`, preserving the old interface.
5+
Internally, the old :class:`Testdir` is now a thin wrapper around :class:`Pytester`, preserving the old interface.

doc/en/reference.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,7 @@ pytester
508508

509509
.. currentmodule:: _pytest.pytester
510510

511-
Provides a :class:`PyTester` instance that can be used to run and test pytest itself.
511+
Provides a :class:`Pytester` instance that can be used to run and test pytest itself.
512512

513513
It provides an empty directory where pytest can be executed in isolation, and contains facilities
514514
to write test, configuration files, and match against expected output.
@@ -521,7 +521,7 @@ To use it, include in your topmost ``conftest.py`` file:
521521
522522
523523
524-
.. autoclass:: PyTester()
524+
.. autoclass:: Pytester()
525525
:members:
526526

527527
.. autoclass:: RunResult()

src/_pytest/pytester.py

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@
5858

5959
import pexpect
6060

61+
PathLike = os.PathLike[str]
62+
else:
63+
PathLike = os.PathLike
64+
6165

6266
IGNORE_PAM = [ # filenames added when obtaining details about the current user
6367
"/var/lib/sss/mc/passwd"
@@ -434,22 +438,22 @@ def LineMatcher_fixture(request: FixtureRequest) -> Type["LineMatcher"]:
434438

435439

436440
@pytest.fixture
437-
def pytester(request: FixtureRequest, tmp_path_factory: TempPathFactory) -> "PyTester":
441+
def pytester(request: FixtureRequest, tmp_path_factory: TempPathFactory) -> "Pytester":
438442
"""
439443
Facilities to write tests/configuration files, execute pytest in isolation, and match
440444
against expected output, perfect for black-box testing of pytest plugins.
441445
442446
It attempts to isolate the test run from external factors as much as possible, modifying
443447
the current working directory to ``path`` and environment variables during initialization.
444448
445-
It attempts to isolate the test run from external factors as much as possible, modifying
446-
the current working directory to ``tmp_path`` and environment variables during initialization.
449+
It is particularly useful for testing plugins. It is similar to the :fixture:`tmp_path`
450+
fixture but provides methods which aid in testing pytest itself.
447451
"""
448-
return PyTester(request, tmp_path_factory)
452+
return Pytester(request, tmp_path_factory)
449453

450454

451455
@pytest.fixture
452-
def testdir(pytester: "PyTester") -> "Testdir":
456+
def testdir(pytester: "Pytester") -> "Testdir":
453457
"""
454458
Identical to :fixture:`test_path`, and provides an instance whose methods return
455459
legacy ``py.path.local`` objects instead when applicable.
@@ -619,7 +623,7 @@ def restore(self) -> None:
619623

620624

621625
@final
622-
class PyTester:
626+
class Pytester:
623627
"""
624628
Facilities to write tests/configuration files, execute pytest in isolation, and match
625629
against expected output, perfect for black-box testing of pytest plugins.
@@ -685,7 +689,7 @@ def path(self) -> Path:
685689
return self._path
686690

687691
def __repr__(self) -> str:
688-
return f"<PyTester {self.path!r}>"
692+
return f"<Pytester {self.path!r}>"
689693

690694
def _finalize(self) -> None:
691695
"""
@@ -888,9 +892,7 @@ def copy_example(self, name=None) -> Path:
888892
example_path = maybe_file
889893
else:
890894
raise LookupError(
891-
"{} cant be found as module or package in {}".format(
892-
func_name, example_dir.bestrelpath(self._request.config.rootdir)
893-
)
895+
f"{func_name} cant be found as module or package in {example_dir}"
894896
)
895897
else:
896898
example_path = example_dir.joinpath(name)
@@ -912,7 +914,9 @@ def copy_example(self, name=None) -> Path:
912914

913915
Session = Session
914916

915-
def getnode(self, config: Config, arg) -> Optional[Union[Collector, Item]]:
917+
def getnode(
918+
self, config: Config, arg: Union[PathLike, str]
919+
) -> Optional[Union[Collector, Item]]:
916920
"""Return the collection node of a file.
917921
918922
:param _pytest.config.Config config:
@@ -929,14 +933,15 @@ def getnode(self, config: Config, arg) -> Optional[Union[Collector, Item]]:
929933
config.hook.pytest_sessionfinish(session=session, exitstatus=ExitCode.OK)
930934
return res
931935

932-
def getpathnode(self, path):
936+
def getpathnode(self, path: Union[PathLike, str]):
933937
"""Return the collection node of a file.
934938
935939
This is like :py:meth:`getnode` but uses :py:meth:`parseconfigure` to
936940
create the (configured) pytest Config instance.
937941
938942
:param py.path.local path: Path to the file.
939943
"""
944+
path = py.path.local(path)
940945
config = self.parseconfigure(path)
941946
session = Session.from_config(config)
942947
x = session.fspath.bestrelpath(path)
@@ -1279,7 +1284,7 @@ def popen(
12791284

12801285
def run(
12811286
self,
1282-
*cmdargs: Union[str, py.path.local, Path],
1287+
*cmdargs: Union[str, PathLike],
12831288
timeout: Optional[float] = None,
12841289
stdin=CLOSE_STDIN,
12851290
) -> RunResult:
@@ -1290,10 +1295,10 @@ def run(
12901295
:param cmdargs:
12911296
The sequence of arguments to pass to `subprocess.Popen()`, with ``Path``
12921297
and ``py.path.local`` objects being converted to ``str`` automatically.
1293-
:kwarg timeout:
1298+
:param timeout:
12941299
The period in seconds after which to timeout and raise
12951300
:py:class:`Testdir.TimeoutExpired`
1296-
:kwarg stdin:
1301+
:param stdin:
12971302
Optional standard input. Bytes are being send, closing
12981303
the pipe, otherwise it is passed through to ``popen``.
12991304
Defaults to ``CLOSE_STDIN``, which translates to using a pipe
@@ -1461,19 +1466,19 @@ def assert_contains_lines(self, lines2: Sequence[str]) -> None:
14611466
@attr.s(repr=False, str=False)
14621467
class Testdir:
14631468
"""
1464-
Similar to :class:`PyTester`, but this class works with legacy py.path.local objects instead.
1469+
Similar to :class:`Pytester`, but this class works with legacy py.path.local objects instead.
14651470
1466-
All methods just forward to an internal :class:`PyTester` instance, converting results
1471+
All methods just forward to an internal :class:`Pytester` instance, converting results
14671472
to `py.path.local` objects as necessary.
14681473
"""
14691474

14701475
__test__ = False
14711476

1472-
CLOSE_STDIN = PyTester.CLOSE_STDIN
1473-
TimeoutExpired = PyTester.TimeoutExpired
1474-
Session = PyTester.Session
1477+
CLOSE_STDIN = Pytester.CLOSE_STDIN
1478+
TimeoutExpired = Pytester.TimeoutExpired
1479+
Session = Pytester.Session
14751480

1476-
_pytester = attr.ib(type=PyTester)
1481+
_pytester = attr.ib(type=Pytester)
14771482

14781483
@property
14791484
def tmpdir(self) -> py.path.local:

testing/acceptance_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from _pytest.compat import importlib_metadata
1010
from _pytest.config import ExitCode
1111
from _pytest.pathlib import symlink_or_skip
12-
from _pytest.pytester import PyTester
12+
from _pytest.pytester import Pytester
1313

1414

1515
def prepend_pythonpath(*dirs):
@@ -1276,7 +1276,7 @@ def test_simple():
12761276
sys.platform == "win32",
12771277
reason="Windows raises `OSError: [Errno 22] Invalid argument` instead",
12781278
)
1279-
def test_no_brokenpipeerror_message(pytester: PyTester) -> None:
1279+
def test_no_brokenpipeerror_message(pytester: Pytester) -> None:
12801280
"""Ensure that the broken pipe error message is supressed.
12811281
12821282
In some Python versions, it reaches sys.unraisablehook, in others

0 commit comments

Comments
 (0)