Skip to content

Commit

Permalink
Merge pull request pytest-dev#8168 from antonblr/testdir-to-pytester-…
Browse files Browse the repository at this point in the history
…incr2

tests: Migrate to pytester - final update
  • Loading branch information
bluetech committed Dec 19, 2020
2 parents f14ab08 + 196b173 commit 89dcfbf
Show file tree
Hide file tree
Showing 12 changed files with 696 additions and 575 deletions.
2 changes: 1 addition & 1 deletion src/_pytest/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ def gethookproxy(self, fspath: "os.PathLike[str]"):
warnings.warn(FSCOLLECTOR_GETHOOKPROXY_ISINITPATH, stacklevel=2)
return self.session.gethookproxy(fspath)

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

Expand Down
2 changes: 1 addition & 1 deletion src/_pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ def gethookproxy(self, fspath: "os.PathLike[str]"):
warnings.warn(FSCOLLECTOR_GETHOOKPROXY_ISINITPATH, stacklevel=2)
return self.session.gethookproxy(fspath)

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

Expand Down
35 changes: 17 additions & 18 deletions testing/deprecated_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,23 @@
import pytest
from _pytest import deprecated
from _pytest.pytester import Pytester
from _pytest.pytester import Testdir


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


@pytest.mark.parametrize("plugin", sorted(deprecated.DEPRECATED_EXTERNAL_PLUGINS))
@pytest.mark.filterwarnings("default")
def test_external_plugins_integrated(testdir, plugin):
testdir.syspathinsert()
testdir.makepyfile(**{plugin: ""})
def test_external_plugins_integrated(pytester: Pytester, plugin) -> None:
pytester.syspathinsert()
pytester.makepyfile(**{plugin: ""})

with pytest.warns(pytest.PytestConfigWarning):
testdir.parseconfig("-p", plugin)
pytester.parseconfig("-p", plugin)


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


def test_minus_k_dash_is_deprecated(testdir) -> None:
threepass = testdir.makepyfile(
def test_minus_k_dash_is_deprecated(pytester: Pytester) -> None:
threepass = pytester.makepyfile(
test_threepass="""
def test_one(): assert 1
def test_two(): assert 1
def test_three(): assert 1
"""
)
result = testdir.runpytest("-k=-test_two", threepass)
result = pytester.runpytest("-k=-test_two", threepass)
result.stdout.fnmatch_lines(["*The `-k '-expr'` syntax*deprecated*"])


def test_minus_k_colon_is_deprecated(testdir) -> None:
threepass = testdir.makepyfile(
def test_minus_k_colon_is_deprecated(pytester: Pytester) -> None:
threepass = pytester.makepyfile(
test_threepass="""
def test_one(): assert 1
def test_two(): assert 1
def test_three(): assert 1
"""
)
result = testdir.runpytest("-k", "test_two:", threepass)
result = pytester.runpytest("-k", "test_two:", threepass)
result.stdout.fnmatch_lines(["*The `-k 'expr:'` syntax*deprecated*"])


def test_fscollector_gethookproxy_isinitpath(testdir: Testdir) -> None:
module = testdir.getmodulecol(
def test_fscollector_gethookproxy_isinitpath(pytester: Pytester) -> None:
module = pytester.getmodulecol(
"""
def test_foo(): pass
""",
Expand All @@ -85,16 +84,16 @@ def test_foo(): pass
assert isinstance(package, pytest.Package)

with pytest.warns(pytest.PytestDeprecationWarning, match="gethookproxy"):
package.gethookproxy(testdir.tmpdir)
package.gethookproxy(pytester.path)

with pytest.warns(pytest.PytestDeprecationWarning, match="isinitpath"):
package.isinitpath(testdir.tmpdir)
package.isinitpath(pytester.path)

# The methods on Session are *not* deprecated.
session = module.session
with warnings.catch_warnings(record=True) as rec:
session.gethookproxy(testdir.tmpdir)
session.isinitpath(testdir.tmpdir)
session.gethookproxy(pytester.path)
session.isinitpath(pytester.path)
assert len(rec) == 0


Expand Down
2 changes: 1 addition & 1 deletion testing/test_cacheprovider.py
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ def test_packages(self, pytester: Pytester) -> None:


class TestNewFirst:
def test_newfirst_usecase(self, pytester: Pytester, testdir) -> None:
def test_newfirst_usecase(self, pytester: Pytester) -> None:
pytester.makepyfile(
**{
"test_1/test_1.py": """
Expand Down
25 changes: 14 additions & 11 deletions testing/test_collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from pathlib import Path
from typing import List

import py.path

import pytest
from _pytest.config import ExitCode
from _pytest.fixtures import FixtureRequest
Expand All @@ -16,7 +18,6 @@
from _pytest.pathlib import symlink_or_skip
from _pytest.pytester import HookRecorder
from _pytest.pytester import Pytester
from _pytest.pytester import Testdir


def ensure_file(file_path: Path) -> Path:
Expand Down Expand Up @@ -206,15 +207,17 @@ def test_ignored_virtualenvs_norecursedirs_precedence(
"Activate.ps1",
),
)
def test__in_venv(self, testdir: Testdir, fname: str) -> None:
def test__in_venv(self, pytester: Pytester, fname: str) -> None:
"""Directly test the virtual env detection function"""
bindir = "Scripts" if sys.platform.startswith("win") else "bin"
# no bin/activate, not a virtualenv
base_path = testdir.tmpdir.mkdir("venv")
assert _in_venv(base_path) is False
base_path = pytester.mkdir("venv")
assert _in_venv(py.path.local(base_path)) is False
# with bin/activate, totally a virtualenv
base_path.ensure(bindir, fname)
assert _in_venv(base_path) is True
bin_path = base_path.joinpath(bindir)
bin_path.mkdir()
bin_path.joinpath(fname).touch()
assert _in_venv(py.path.local(base_path)) is True

def test_custom_norecursedirs(self, pytester: Pytester) -> None:
pytester.makeini(
Expand Down Expand Up @@ -264,7 +267,7 @@ def test_testpaths_ini(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> No


class TestCollectPluginHookRelay:
def test_pytest_collect_file(self, testdir: Testdir) -> None:
def test_pytest_collect_file(self, pytester: Pytester) -> None:
wascalled = []

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

testdir.makefile(".abc", "xyz")
pytest.main(testdir.tmpdir, plugins=[Plugin()])
pytester.makefile(".abc", "xyz")
pytest.main(py.path.local(pytester.path), plugins=[Plugin()])
assert len(wascalled) == 1
assert wascalled[0].ext == ".abc"

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


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

collector = MyCollector.from_parent(
parent=request.session, fspath=testdir.tmpdir / "foo", x=10
parent=request.session, fspath=py.path.local(pytester.path) / "foo", x=10
)
assert collector.x == 10

Expand Down
4 changes: 2 additions & 2 deletions testing/test_debugging.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
def pdb_env(request):
if "pytester" in request.fixturenames:
# Disable pdb++ with inner tests.
pytester = request.getfixturevalue("testdir")
pytester.monkeypatch.setenv("PDBPP_HIJACK_PDB", "0")
pytester = request.getfixturevalue("pytester")
pytester._monkeypatch.setenv("PDBPP_HIJACK_PDB", "0")


def runpdb_and_get_report(pytester: Pytester, source: str):
Expand Down

0 comments on commit 89dcfbf

Please sign in to comment.