Skip to content

Commit 89dcfbf

Browse files
authored
Merge pull request #8168 from antonblr/testdir-to-pytester-incr2
tests: Migrate to pytester - final update
2 parents f14ab08 + 196b173 commit 89dcfbf

12 files changed

+696
-575
lines changed

src/_pytest/nodes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ def gethookproxy(self, fspath: "os.PathLike[str]"):
528528
warnings.warn(FSCOLLECTOR_GETHOOKPROXY_ISINITPATH, stacklevel=2)
529529
return self.session.gethookproxy(fspath)
530530

531-
def isinitpath(self, path: py.path.local) -> bool:
531+
def isinitpath(self, path: Union[str, "os.PathLike[str]"]) -> bool:
532532
warnings.warn(FSCOLLECTOR_GETHOOKPROXY_ISINITPATH, stacklevel=2)
533533
return self.session.isinitpath(path)
534534

src/_pytest/python.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -660,7 +660,7 @@ def gethookproxy(self, fspath: "os.PathLike[str]"):
660660
warnings.warn(FSCOLLECTOR_GETHOOKPROXY_ISINITPATH, stacklevel=2)
661661
return self.session.gethookproxy(fspath)
662662

663-
def isinitpath(self, path: py.path.local) -> bool:
663+
def isinitpath(self, path: Union[str, "os.PathLike[str]"]) -> bool:
664664
warnings.warn(FSCOLLECTOR_GETHOOKPROXY_ISINITPATH, stacklevel=2)
665665
return self.session.isinitpath(path)
666666

testing/deprecated_test.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,23 @@
55
import pytest
66
from _pytest import deprecated
77
from _pytest.pytester import Pytester
8-
from _pytest.pytester import Testdir
98

109

1110
@pytest.mark.parametrize("attribute", pytest.collect.__all__) # type: ignore
1211
# false positive due to dynamic attribute
13-
def test_pytest_collect_module_deprecated(attribute):
12+
def test_pytest_collect_module_deprecated(attribute) -> None:
1413
with pytest.warns(DeprecationWarning, match=attribute):
1514
getattr(pytest.collect, attribute)
1615

1716

1817
@pytest.mark.parametrize("plugin", sorted(deprecated.DEPRECATED_EXTERNAL_PLUGINS))
1918
@pytest.mark.filterwarnings("default")
20-
def test_external_plugins_integrated(testdir, plugin):
21-
testdir.syspathinsert()
22-
testdir.makepyfile(**{plugin: ""})
19+
def test_external_plugins_integrated(pytester: Pytester, plugin) -> None:
20+
pytester.syspathinsert()
21+
pytester.makepyfile(**{plugin: ""})
2322

2423
with pytest.warns(pytest.PytestConfigWarning):
25-
testdir.parseconfig("-p", plugin)
24+
pytester.parseconfig("-p", plugin)
2625

2726

2827
def test_fillfuncargs_is_deprecated() -> None:
@@ -49,32 +48,32 @@ def test_fillfixtures_is_deprecated() -> None:
4948
_pytest.fixtures.fillfixtures(mock.Mock())
5049

5150

52-
def test_minus_k_dash_is_deprecated(testdir) -> None:
53-
threepass = testdir.makepyfile(
51+
def test_minus_k_dash_is_deprecated(pytester: Pytester) -> None:
52+
threepass = pytester.makepyfile(
5453
test_threepass="""
5554
def test_one(): assert 1
5655
def test_two(): assert 1
5756
def test_three(): assert 1
5857
"""
5958
)
60-
result = testdir.runpytest("-k=-test_two", threepass)
59+
result = pytester.runpytest("-k=-test_two", threepass)
6160
result.stdout.fnmatch_lines(["*The `-k '-expr'` syntax*deprecated*"])
6261

6362

64-
def test_minus_k_colon_is_deprecated(testdir) -> None:
65-
threepass = testdir.makepyfile(
63+
def test_minus_k_colon_is_deprecated(pytester: Pytester) -> None:
64+
threepass = pytester.makepyfile(
6665
test_threepass="""
6766
def test_one(): assert 1
6867
def test_two(): assert 1
6968
def test_three(): assert 1
7069
"""
7170
)
72-
result = testdir.runpytest("-k", "test_two:", threepass)
71+
result = pytester.runpytest("-k", "test_two:", threepass)
7372
result.stdout.fnmatch_lines(["*The `-k 'expr:'` syntax*deprecated*"])
7473

7574

76-
def test_fscollector_gethookproxy_isinitpath(testdir: Testdir) -> None:
77-
module = testdir.getmodulecol(
75+
def test_fscollector_gethookproxy_isinitpath(pytester: Pytester) -> None:
76+
module = pytester.getmodulecol(
7877
"""
7978
def test_foo(): pass
8079
""",
@@ -85,16 +84,16 @@ def test_foo(): pass
8584
assert isinstance(package, pytest.Package)
8685

8786
with pytest.warns(pytest.PytestDeprecationWarning, match="gethookproxy"):
88-
package.gethookproxy(testdir.tmpdir)
87+
package.gethookproxy(pytester.path)
8988

9089
with pytest.warns(pytest.PytestDeprecationWarning, match="isinitpath"):
91-
package.isinitpath(testdir.tmpdir)
90+
package.isinitpath(pytester.path)
9291

9392
# The methods on Session are *not* deprecated.
9493
session = module.session
9594
with warnings.catch_warnings(record=True) as rec:
96-
session.gethookproxy(testdir.tmpdir)
97-
session.isinitpath(testdir.tmpdir)
95+
session.gethookproxy(pytester.path)
96+
session.isinitpath(pytester.path)
9897
assert len(rec) == 0
9998

10099

testing/test_cacheprovider.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,7 @@ def test_packages(self, pytester: Pytester) -> None:
10501050

10511051

10521052
class TestNewFirst:
1053-
def test_newfirst_usecase(self, pytester: Pytester, testdir) -> None:
1053+
def test_newfirst_usecase(self, pytester: Pytester) -> None:
10541054
pytester.makepyfile(
10551055
**{
10561056
"test_1/test_1.py": """

testing/test_collection.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
from pathlib import Path
77
from typing import List
88

9+
import py.path
10+
911
import pytest
1012
from _pytest.config import ExitCode
1113
from _pytest.fixtures import FixtureRequest
@@ -16,7 +18,6 @@
1618
from _pytest.pathlib import symlink_or_skip
1719
from _pytest.pytester import HookRecorder
1820
from _pytest.pytester import Pytester
19-
from _pytest.pytester import Testdir
2021

2122

2223
def ensure_file(file_path: Path) -> Path:
@@ -206,15 +207,17 @@ def test_ignored_virtualenvs_norecursedirs_precedence(
206207
"Activate.ps1",
207208
),
208209
)
209-
def test__in_venv(self, testdir: Testdir, fname: str) -> None:
210+
def test__in_venv(self, pytester: Pytester, fname: str) -> None:
210211
"""Directly test the virtual env detection function"""
211212
bindir = "Scripts" if sys.platform.startswith("win") else "bin"
212213
# no bin/activate, not a virtualenv
213-
base_path = testdir.tmpdir.mkdir("venv")
214-
assert _in_venv(base_path) is False
214+
base_path = pytester.mkdir("venv")
215+
assert _in_venv(py.path.local(base_path)) is False
215216
# with bin/activate, totally a virtualenv
216-
base_path.ensure(bindir, fname)
217-
assert _in_venv(base_path) is True
217+
bin_path = base_path.joinpath(bindir)
218+
bin_path.mkdir()
219+
bin_path.joinpath(fname).touch()
220+
assert _in_venv(py.path.local(base_path)) is True
218221

219222
def test_custom_norecursedirs(self, pytester: Pytester) -> None:
220223
pytester.makeini(
@@ -264,7 +267,7 @@ def test_testpaths_ini(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> No
264267

265268

266269
class TestCollectPluginHookRelay:
267-
def test_pytest_collect_file(self, testdir: Testdir) -> None:
270+
def test_pytest_collect_file(self, pytester: Pytester) -> None:
268271
wascalled = []
269272

270273
class Plugin:
@@ -273,8 +276,8 @@ def pytest_collect_file(self, path):
273276
# Ignore hidden files, e.g. .testmondata.
274277
wascalled.append(path)
275278

276-
testdir.makefile(".abc", "xyz")
277-
pytest.main(testdir.tmpdir, plugins=[Plugin()])
279+
pytester.makefile(".abc", "xyz")
280+
pytest.main(py.path.local(pytester.path), plugins=[Plugin()])
278281
assert len(wascalled) == 1
279282
assert wascalled[0].ext == ".abc"
280283

@@ -1336,7 +1339,7 @@ def test_does_not_put_src_on_path(pytester: Pytester) -> None:
13361339
assert result.ret == ExitCode.OK
13371340

13381341

1339-
def test_fscollector_from_parent(testdir: Testdir, request: FixtureRequest) -> None:
1342+
def test_fscollector_from_parent(pytester: Pytester, request: FixtureRequest) -> None:
13401343
"""Ensure File.from_parent can forward custom arguments to the constructor.
13411344
13421345
Context: https://github.com/pytest-dev/pytest-cpp/pull/47
@@ -1352,7 +1355,7 @@ def from_parent(cls, parent, *, fspath, x):
13521355
return super().from_parent(parent=parent, fspath=fspath, x=x)
13531356

13541357
collector = MyCollector.from_parent(
1355-
parent=request.session, fspath=testdir.tmpdir / "foo", x=10
1358+
parent=request.session, fspath=py.path.local(pytester.path) / "foo", x=10
13561359
)
13571360
assert collector.x == 10
13581361

testing/test_debugging.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
def pdb_env(request):
2525
if "pytester" in request.fixturenames:
2626
# Disable pdb++ with inner tests.
27-
pytester = request.getfixturevalue("testdir")
28-
pytester.monkeypatch.setenv("PDBPP_HIJACK_PDB", "0")
27+
pytester = request.getfixturevalue("pytester")
28+
pytester._monkeypatch.setenv("PDBPP_HIJACK_PDB", "0")
2929

3030

3131
def runpdb_and_get_report(pytester: Pytester, source: str):

0 commit comments

Comments
 (0)