2019-09-12 07:09:08 +08:00
|
|
|
import os
|
2018-08-24 00:06:17 +08:00
|
|
|
import textwrap
|
2020-10-03 23:08:14 +08:00
|
|
|
from pathlib import Path
|
2020-11-09 12:23:31 +08:00
|
|
|
from typing import cast
|
|
|
|
from typing import Dict
|
2020-12-11 19:40:37 +08:00
|
|
|
from typing import Generator
|
2020-11-09 12:23:31 +08:00
|
|
|
from typing import List
|
|
|
|
from typing import Optional
|
2023-06-21 01:59:22 +08:00
|
|
|
from typing import Sequence
|
|
|
|
from typing import Union
|
2015-11-27 22:43:01 +08:00
|
|
|
|
|
|
|
import pytest
|
2020-02-11 05:43:30 +08:00
|
|
|
from _pytest.config import ExitCode
|
2019-08-27 22:16:45 +08:00
|
|
|
from _pytest.config import PytestPluginManager
|
2020-11-09 12:23:31 +08:00
|
|
|
from _pytest.monkeypatch import MonkeyPatch
|
2020-06-08 21:56:40 +08:00
|
|
|
from _pytest.pathlib import symlink_or_skip
|
2020-11-09 12:23:31 +08:00
|
|
|
from _pytest.pytester import Pytester
|
2020-12-11 19:40:37 +08:00
|
|
|
from _pytest.tmpdir import TempPathFactory
|
2009-05-19 00:59:45 +08:00
|
|
|
|
2015-02-27 04:56:44 +08:00
|
|
|
|
2020-11-09 12:23:31 +08:00
|
|
|
def ConftestWithSetinitial(path) -> PytestPluginManager:
|
2015-04-22 20:15:42 +08:00
|
|
|
conftest = PytestPluginManager()
|
2014-04-02 17:29:23 +08:00
|
|
|
conftest_setinitial(conftest, [path])
|
2009-12-29 00:49:46 +08:00
|
|
|
return conftest
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-11-09 12:23:31 +08:00
|
|
|
def conftest_setinitial(
|
2023-06-21 01:59:22 +08:00
|
|
|
conftest: PytestPluginManager,
|
|
|
|
args: Sequence[Union[str, Path]],
|
|
|
|
confcutdir: Optional[Path] = None,
|
2020-11-09 12:23:31 +08:00
|
|
|
) -> None:
|
2023-06-21 01:59:22 +08:00
|
|
|
conftest._set_initial_conftests(
|
|
|
|
args=args,
|
|
|
|
pyargs=False,
|
|
|
|
noconftest=False,
|
|
|
|
rootpath=Path(args[0]),
|
|
|
|
confcutdir=confcutdir,
|
|
|
|
importmode="prepend",
|
|
|
|
)
|
2014-04-02 17:29:23 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2019-04-05 17:31:02 +08:00
|
|
|
@pytest.mark.usefixtures("_sys_snapshot")
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestConftestValueAccessGlobal:
|
2019-04-04 23:07:15 +08:00
|
|
|
@pytest.fixture(scope="module", params=["global", "inpackage"])
|
2020-12-11 19:40:37 +08:00
|
|
|
def basedir(
|
|
|
|
self, request, tmp_path_factory: TempPathFactory
|
|
|
|
) -> Generator[Path, None, None]:
|
2021-02-21 21:10:00 +08:00
|
|
|
tmp_path = tmp_path_factory.mktemp("basedir", numbered=True)
|
|
|
|
tmp_path.joinpath("adir/b").mkdir(parents=True)
|
2023-06-20 19:55:39 +08:00
|
|
|
tmp_path.joinpath("adir/conftest.py").write_text(
|
|
|
|
"a=1 ; Directory = 3", encoding="utf-8"
|
|
|
|
)
|
|
|
|
tmp_path.joinpath("adir/b/conftest.py").write_text(
|
|
|
|
"b=2 ; a = 1.5", encoding="utf-8"
|
|
|
|
)
|
2019-04-04 23:07:15 +08:00
|
|
|
if request.param == "inpackage":
|
2021-02-21 21:10:00 +08:00
|
|
|
tmp_path.joinpath("adir/__init__.py").touch()
|
|
|
|
tmp_path.joinpath("adir/b/__init__.py").touch()
|
2019-04-04 23:07:15 +08:00
|
|
|
|
2021-02-21 21:10:00 +08:00
|
|
|
yield tmp_path
|
2019-04-04 23:07:15 +08:00
|
|
|
|
2020-12-11 19:40:37 +08:00
|
|
|
def test_basic_init(self, basedir: Path) -> None:
|
2015-04-22 20:15:42 +08:00
|
|
|
conftest = PytestPluginManager()
|
2020-12-11 19:40:37 +08:00
|
|
|
p = basedir / "adir"
|
2022-01-09 19:58:29 +08:00
|
|
|
conftest._loadconftestmodules(p, importmode="prepend", rootpath=basedir)
|
|
|
|
assert conftest._rget_with_confmod("a", p)[1] == 1
|
2009-02-27 18:18:27 +08:00
|
|
|
|
2020-12-11 19:40:37 +08:00
|
|
|
def test_immediate_initialiation_and_incremental_are_the_same(
|
|
|
|
self, basedir: Path
|
|
|
|
) -> None:
|
2015-04-22 20:15:42 +08:00
|
|
|
conftest = PytestPluginManager()
|
2019-04-05 16:59:08 +08:00
|
|
|
assert not len(conftest._dirpath2confmods)
|
2022-01-09 19:58:29 +08:00
|
|
|
conftest._loadconftestmodules(basedir, importmode="prepend", rootpath=basedir)
|
2018-10-25 00:50:59 +08:00
|
|
|
snap1 = len(conftest._dirpath2confmods)
|
2019-04-05 16:59:08 +08:00
|
|
|
assert snap1 == 1
|
2022-01-09 19:58:29 +08:00
|
|
|
conftest._loadconftestmodules(
|
2021-04-06 04:10:03 +08:00
|
|
|
basedir / "adir", importmode="prepend", rootpath=basedir
|
|
|
|
)
|
2018-10-25 00:50:59 +08:00
|
|
|
assert len(conftest._dirpath2confmods) == snap1 + 1
|
2022-01-09 19:58:29 +08:00
|
|
|
conftest._loadconftestmodules(
|
2021-04-06 04:10:03 +08:00
|
|
|
basedir / "b", importmode="prepend", rootpath=basedir
|
|
|
|
)
|
2018-10-25 00:50:59 +08:00
|
|
|
assert len(conftest._dirpath2confmods) == snap1 + 2
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2020-12-11 19:40:37 +08:00
|
|
|
def test_value_access_not_existing(self, basedir: Path) -> None:
|
2009-12-29 00:49:46 +08:00
|
|
|
conftest = ConftestWithSetinitial(basedir)
|
2014-04-03 04:30:45 +08:00
|
|
|
with pytest.raises(KeyError):
|
2022-01-09 19:58:29 +08:00
|
|
|
conftest._rget_with_confmod("a", basedir)
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2020-12-11 19:40:37 +08:00
|
|
|
def test_value_access_by_path(self, basedir: Path) -> None:
|
2009-12-29 00:49:46 +08:00
|
|
|
conftest = ConftestWithSetinitial(basedir)
|
2020-12-11 19:40:37 +08:00
|
|
|
adir = basedir / "adir"
|
2022-01-09 19:58:29 +08:00
|
|
|
conftest._loadconftestmodules(adir, importmode="prepend", rootpath=basedir)
|
|
|
|
assert conftest._rget_with_confmod("a", adir)[1] == 1
|
|
|
|
conftest._loadconftestmodules(
|
|
|
|
adir / "b", importmode="prepend", rootpath=basedir
|
2020-06-13 22:29:01 +08:00
|
|
|
)
|
2022-01-09 19:58:29 +08:00
|
|
|
assert conftest._rget_with_confmod("a", adir / "b")[1] == 1.5
|
2008-08-16 23:26:59 +08:00
|
|
|
|
2020-12-11 19:40:37 +08:00
|
|
|
def test_value_access_with_confmod(self, basedir: Path) -> None:
|
|
|
|
startdir = basedir / "adir" / "b"
|
|
|
|
startdir.joinpath("xx").mkdir()
|
2010-09-15 16:30:50 +08:00
|
|
|
conftest = ConftestWithSetinitial(startdir)
|
2022-01-09 19:58:29 +08:00
|
|
|
mod, value = conftest._rget_with_confmod("a", startdir)
|
2017-07-17 07:25:09 +08:00
|
|
|
assert value == 1.5
|
2021-12-30 06:52:16 +08:00
|
|
|
assert mod.__file__ is not None
|
2020-12-15 19:02:32 +08:00
|
|
|
path = Path(mod.__file__)
|
|
|
|
assert path.parent == basedir / "adir" / "b"
|
|
|
|
assert path.stem == "conftest"
|
2009-12-31 22:10:32 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-11-09 12:23:31 +08:00
|
|
|
def test_conftest_in_nonpkg_with_init(tmp_path: Path, _sys_snapshot) -> None:
|
|
|
|
tmp_path.joinpath("adir-1.0/b").mkdir(parents=True)
|
2023-06-20 19:55:39 +08:00
|
|
|
tmp_path.joinpath("adir-1.0/conftest.py").write_text(
|
|
|
|
"a=1 ; Directory = 3", encoding="utf-8"
|
|
|
|
)
|
|
|
|
tmp_path.joinpath("adir-1.0/b/conftest.py").write_text(
|
|
|
|
"b=2 ; a = 1.5", encoding="utf-8"
|
|
|
|
)
|
2020-11-09 12:23:31 +08:00
|
|
|
tmp_path.joinpath("adir-1.0/b/__init__.py").touch()
|
|
|
|
tmp_path.joinpath("adir-1.0/__init__.py").touch()
|
|
|
|
ConftestWithSetinitial(tmp_path.joinpath("adir-1.0", "b"))
|
2010-04-27 21:49:13 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-12-11 19:40:37 +08:00
|
|
|
def test_doubledash_considered(pytester: Pytester) -> None:
|
|
|
|
conf = pytester.mkdir("--option")
|
|
|
|
conf.joinpath("conftest.py").touch()
|
2015-04-22 20:15:42 +08:00
|
|
|
conftest = PytestPluginManager()
|
2020-12-11 19:40:37 +08:00
|
|
|
conftest_setinitial(conftest, [conf.name, conf.name])
|
2022-01-09 19:58:29 +08:00
|
|
|
values = conftest._getconftestmodules(conf)
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 1
|
2010-07-02 01:27:40 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-11-09 12:23:31 +08:00
|
|
|
def test_issue151_load_all_conftests(pytester: Pytester) -> None:
|
2012-06-27 03:56:03 +08:00
|
|
|
names = "code proj src".split()
|
|
|
|
for name in names:
|
2020-11-09 12:23:31 +08:00
|
|
|
p = pytester.mkdir(name)
|
|
|
|
p.joinpath("conftest.py").touch()
|
2012-06-27 03:56:03 +08:00
|
|
|
|
2022-01-09 06:38:40 +08:00
|
|
|
pm = PytestPluginManager()
|
|
|
|
conftest_setinitial(pm, names)
|
|
|
|
assert len(set(pm.get_plugins()) - {pm}) == len(names)
|
2012-06-27 03:56:03 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-11-09 12:23:31 +08:00
|
|
|
def test_conftest_global_import(pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest("x=3")
|
|
|
|
p = pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2020-12-11 19:40:37 +08:00
|
|
|
from pathlib import Path
|
|
|
|
import pytest
|
2015-04-22 20:15:42 +08:00
|
|
|
from _pytest.config import PytestPluginManager
|
|
|
|
conf = PytestPluginManager()
|
2021-04-06 04:10:03 +08:00
|
|
|
mod = conf._importconftest(Path("conftest.py"), importmode="prepend", rootpath=Path.cwd())
|
2010-10-14 00:45:07 +08:00
|
|
|
assert mod.x == 3
|
|
|
|
import conftest
|
|
|
|
assert conftest is mod, (conftest, mod)
|
2020-12-11 19:40:37 +08:00
|
|
|
sub = Path("sub")
|
|
|
|
sub.mkdir()
|
|
|
|
subconf = sub / "conftest.py"
|
2023-06-20 19:55:39 +08:00
|
|
|
subconf.write_text("y=4", encoding="utf-8")
|
2021-04-06 04:10:03 +08:00
|
|
|
mod2 = conf._importconftest(subconf, importmode="prepend", rootpath=Path.cwd())
|
2010-10-14 00:45:07 +08:00
|
|
|
assert mod != mod2
|
|
|
|
assert mod2.y == 4
|
|
|
|
import conftest
|
|
|
|
assert conftest is mod2, (conftest, mod)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-11-09 12:23:31 +08:00
|
|
|
res = pytester.runpython(p)
|
2010-10-14 00:45:07 +08:00
|
|
|
assert res.ret == 0
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-12-11 19:40:37 +08:00
|
|
|
def test_conftestcutdir(pytester: Pytester) -> None:
|
|
|
|
conf = pytester.makeconftest("")
|
|
|
|
p = pytester.mkdir("x")
|
2015-04-22 20:15:42 +08:00
|
|
|
conftest = PytestPluginManager()
|
2020-12-11 19:40:37 +08:00
|
|
|
conftest_setinitial(conftest, [pytester.path], confcutdir=p)
|
2022-01-09 19:58:29 +08:00
|
|
|
conftest._loadconftestmodules(p, importmode="prepend", rootpath=pytester.path)
|
|
|
|
values = conftest._getconftestmodules(p)
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 0
|
2022-01-09 19:58:29 +08:00
|
|
|
conftest._loadconftestmodules(
|
2021-04-06 04:10:03 +08:00
|
|
|
conf.parent, importmode="prepend", rootpath=pytester.path
|
|
|
|
)
|
2022-01-09 19:58:29 +08:00
|
|
|
values = conftest._getconftestmodules(conf.parent)
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 0
|
2022-01-09 06:38:40 +08:00
|
|
|
assert not conftest.has_plugin(str(conf))
|
2010-07-27 03:15:15 +08:00
|
|
|
# but we can still import a conftest directly
|
2021-04-06 04:10:03 +08:00
|
|
|
conftest._importconftest(conf, importmode="prepend", rootpath=pytester.path)
|
2022-01-09 19:58:29 +08:00
|
|
|
values = conftest._getconftestmodules(conf.parent)
|
2021-12-30 06:52:16 +08:00
|
|
|
assert values[0].__file__ is not None
|
2019-09-12 06:37:42 +08:00
|
|
|
assert values[0].__file__.startswith(str(conf))
|
2009-12-31 22:10:32 +08:00
|
|
|
# and all sub paths get updated properly
|
2022-01-09 19:58:29 +08:00
|
|
|
values = conftest._getconftestmodules(p)
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 1
|
2021-12-30 06:52:16 +08:00
|
|
|
assert values[0].__file__ is not None
|
2019-09-12 06:37:42 +08:00
|
|
|
assert values[0].__file__.startswith(str(conf))
|
2009-12-31 22:10:32 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-11-09 12:23:31 +08:00
|
|
|
def test_conftestcutdir_inplace_considered(pytester: Pytester) -> None:
|
|
|
|
conf = pytester.makeconftest("")
|
2015-04-22 20:15:42 +08:00
|
|
|
conftest = PytestPluginManager()
|
2020-12-11 19:40:37 +08:00
|
|
|
conftest_setinitial(conftest, [conf.parent], confcutdir=conf.parent)
|
2022-01-09 19:58:29 +08:00
|
|
|
values = conftest._getconftestmodules(conf.parent)
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 1
|
2021-12-30 06:52:16 +08:00
|
|
|
assert values[0].__file__ is not None
|
2019-09-12 06:37:42 +08:00
|
|
|
assert values[0].__file__.startswith(str(conf))
|
2009-12-31 22:10:32 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize("name", "test tests whatever .dotdir".split())
|
2020-11-09 12:23:31 +08:00
|
|
|
def test_setinitial_conftest_subdirs(pytester: Pytester, name: str) -> None:
|
|
|
|
sub = pytester.mkdir(name)
|
|
|
|
subconftest = sub.joinpath("conftest.py")
|
|
|
|
subconftest.touch()
|
2022-01-09 06:38:40 +08:00
|
|
|
pm = PytestPluginManager()
|
|
|
|
conftest_setinitial(pm, [sub.parent], confcutdir=pytester.path)
|
2020-11-09 12:23:31 +08:00
|
|
|
key = subconftest.resolve()
|
2018-05-23 22:48:46 +08:00
|
|
|
if name not in ("whatever", ".dotdir"):
|
2022-01-09 06:38:40 +08:00
|
|
|
assert pm.has_plugin(str(key))
|
|
|
|
assert len(set(pm.get_plugins()) - {pm}) == 1
|
2010-02-07 05:37:04 +08:00
|
|
|
else:
|
2022-01-09 06:38:40 +08:00
|
|
|
assert not pm.has_plugin(str(key))
|
|
|
|
assert len(set(pm.get_plugins()) - {pm}) == 0
|
2010-10-10 19:48:48 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-11-09 12:23:31 +08:00
|
|
|
def test_conftest_confcutdir(pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest("assert 0")
|
|
|
|
x = pytester.mkdir("x")
|
|
|
|
x.joinpath("conftest.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption("--xyz", action="store_true")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-11-09 12:23:31 +08:00
|
|
|
result = pytester.runpytest("-h", "--confcutdir=%s" % x, x)
|
2010-10-10 19:48:48 +08:00
|
|
|
result.stdout.fnmatch_lines(["*--xyz*"])
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*warning: could not load initial*")
|
2013-04-18 19:24:53 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2022-03-17 04:27:56 +08:00
|
|
|
def test_installed_conftest_is_picked_up(pytester: Pytester, tmp_path: Path) -> None:
|
|
|
|
"""When using `--pyargs` to run tests in an installed packages (located e.g.
|
|
|
|
in a site-packages in the PYTHONPATH), conftest files in there are picked
|
|
|
|
up.
|
|
|
|
|
|
|
|
Regression test for #9767.
|
|
|
|
"""
|
|
|
|
# pytester dir - the source tree.
|
|
|
|
# tmp_path - the simulated site-packages dir (not in source tree).
|
|
|
|
|
|
|
|
pytester.syspathinsert(tmp_path)
|
|
|
|
pytester.makepyprojecttoml("[tool.pytest.ini_options]")
|
|
|
|
tmp_path.joinpath("foo").mkdir()
|
|
|
|
tmp_path.joinpath("foo", "__init__.py").touch()
|
|
|
|
tmp_path.joinpath("foo", "conftest.py").write_text(
|
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def fix(): return None
|
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
|
|
|
)
|
|
|
|
tmp_path.joinpath("foo", "test_it.py").write_text(
|
|
|
|
"def test_it(fix): pass", encoding="utf-8"
|
2022-03-17 04:27:56 +08:00
|
|
|
)
|
|
|
|
result = pytester.runpytest("--pyargs", "foo")
|
|
|
|
assert result.ret == 0
|
|
|
|
|
|
|
|
|
2020-11-09 12:23:31 +08:00
|
|
|
def test_conftest_symlink(pytester: Pytester) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""`conftest.py` discovery follows normal path resolution and does not resolve symlinks."""
|
2020-06-08 21:56:40 +08:00
|
|
|
# Structure:
|
|
|
|
# /real
|
|
|
|
# /real/conftest.py
|
|
|
|
# /real/app
|
|
|
|
# /real/app/tests
|
|
|
|
# /real/app/tests/test_foo.py
|
|
|
|
|
|
|
|
# Links:
|
|
|
|
# /symlinktests -> /real/app/tests (running at symlinktests should fail)
|
|
|
|
# /symlink -> /real (running at /symlink should work)
|
|
|
|
|
2020-11-09 12:23:31 +08:00
|
|
|
real = pytester.mkdir("real")
|
|
|
|
realtests = real.joinpath("app/tests")
|
|
|
|
realtests.mkdir(parents=True)
|
|
|
|
symlink_or_skip(realtests, pytester.path.joinpath("symlinktests"))
|
|
|
|
symlink_or_skip(real, pytester.path.joinpath("symlink"))
|
|
|
|
pytester.makepyfile(
|
2018-10-10 22:56:18 +08:00
|
|
|
**{
|
|
|
|
"real/app/tests/test_foo.py": "def test1(fixture): pass",
|
|
|
|
"real/conftest.py": textwrap.dedent(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
print("conftest_loaded")
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fixture():
|
|
|
|
print("fixture_used")
|
|
|
|
"""
|
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
2020-06-08 21:56:40 +08:00
|
|
|
|
|
|
|
# Should fail because conftest cannot be found from the link structure.
|
2020-11-09 12:23:31 +08:00
|
|
|
result = pytester.runpytest("-vs", "symlinktests")
|
2020-06-08 21:56:40 +08:00
|
|
|
result.stdout.fnmatch_lines(["*fixture 'fixture' not found*"])
|
|
|
|
assert result.ret == ExitCode.TESTS_FAILED
|
2018-10-10 22:56:18 +08:00
|
|
|
|
2018-10-22 19:57:01 +08:00
|
|
|
# Should not cause "ValueError: Plugin already registered" (#4174).
|
2020-11-09 12:23:31 +08:00
|
|
|
result = pytester.runpytest("-vs", "symlink")
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.OK
|
2018-10-22 19:57:01 +08:00
|
|
|
|
2018-10-10 22:56:18 +08:00
|
|
|
|
2020-11-09 12:23:31 +08:00
|
|
|
def test_conftest_symlink_files(pytester: Pytester) -> None:
|
2020-06-08 21:56:40 +08:00
|
|
|
"""Symlinked conftest.py are found when pytest is executed in a directory with symlinked
|
|
|
|
files."""
|
2020-11-09 12:23:31 +08:00
|
|
|
real = pytester.mkdir("real")
|
2019-02-07 09:04:06 +08:00
|
|
|
source = {
|
|
|
|
"app/test_foo.py": "def test1(fixture): pass",
|
|
|
|
"app/__init__.py": "",
|
|
|
|
"app/conftest.py": textwrap.dedent(
|
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
print("conftest_loaded")
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def fixture():
|
|
|
|
print("fixture_used")
|
|
|
|
"""
|
|
|
|
),
|
|
|
|
}
|
2020-11-09 12:23:31 +08:00
|
|
|
pytester.makepyfile(**{"real/%s" % k: v for k, v in source.items()})
|
2019-02-07 09:04:06 +08:00
|
|
|
|
|
|
|
# Create a build directory that contains symlinks to actual files
|
|
|
|
# but doesn't symlink actual directories.
|
2020-11-09 12:23:31 +08:00
|
|
|
build = pytester.mkdir("build")
|
|
|
|
build.joinpath("app").mkdir()
|
2019-02-07 09:04:06 +08:00
|
|
|
for f in source:
|
2020-11-09 12:23:31 +08:00
|
|
|
symlink_or_skip(real.joinpath(f), build.joinpath(f))
|
|
|
|
os.chdir(build)
|
|
|
|
result = pytester.runpytest("-vs", "app/test_foo.py")
|
2019-02-07 09:04:06 +08:00
|
|
|
result.stdout.fnmatch_lines(["*conftest_loaded*", "PASSED"])
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.OK
|
2019-02-07 09:04:06 +08:00
|
|
|
|
2019-08-27 22:16:45 +08:00
|
|
|
|
2019-09-12 07:09:08 +08:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
os.path.normcase("x") != os.path.normcase("X"),
|
|
|
|
reason="only relevant for case insensitive file systems",
|
|
|
|
)
|
2020-11-09 12:23:31 +08:00
|
|
|
def test_conftest_badcase(pytester: Pytester) -> None:
|
2019-09-12 07:09:08 +08:00
|
|
|
"""Check conftest.py loading when directory casing is wrong (#5792)."""
|
2020-11-09 12:23:31 +08:00
|
|
|
pytester.path.joinpath("JenkinsRoot/test").mkdir(parents=True)
|
2019-09-12 07:09:08 +08:00
|
|
|
source = {"setup.py": "", "test/__init__.py": "", "test/conftest.py": ""}
|
2020-11-09 12:23:31 +08:00
|
|
|
pytester.makepyfile(**{"JenkinsRoot/%s" % k: v for k, v in source.items()})
|
2019-09-12 07:09:08 +08:00
|
|
|
|
2020-11-09 12:23:31 +08:00
|
|
|
os.chdir(pytester.path.joinpath("jenkinsroot/test"))
|
|
|
|
result = pytester.runpytest()
|
2019-09-12 07:09:08 +08:00
|
|
|
assert result.ret == ExitCode.NO_TESTS_COLLECTED
|
|
|
|
|
|
|
|
|
2020-11-09 12:23:31 +08:00
|
|
|
def test_conftest_uppercase(pytester: Pytester) -> None:
|
2019-09-12 07:09:08 +08:00
|
|
|
"""Check conftest.py whose qualified name contains uppercase characters (#5819)"""
|
|
|
|
source = {"__init__.py": "", "Foo/conftest.py": "", "Foo/__init__.py": ""}
|
2020-11-09 12:23:31 +08:00
|
|
|
pytester.makepyfile(**source)
|
2019-09-12 07:09:08 +08:00
|
|
|
|
2020-11-09 12:23:31 +08:00
|
|
|
os.chdir(pytester.path)
|
|
|
|
result = pytester.runpytest()
|
2019-09-12 07:09:08 +08:00
|
|
|
assert result.ret == ExitCode.NO_TESTS_COLLECTED
|
|
|
|
|
|
|
|
|
2020-11-09 12:23:31 +08:00
|
|
|
def test_no_conftest(pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest("assert 0")
|
|
|
|
result = pytester.runpytest("--noconftest")
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.NO_TESTS_COLLECTED
|
2015-07-05 01:42:22 +08:00
|
|
|
|
2020-11-09 12:23:31 +08:00
|
|
|
result = pytester.runpytest()
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.USAGE_ERROR
|
2015-06-23 13:53:32 +08:00
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-11-09 12:23:31 +08:00
|
|
|
def test_conftest_existing_junitxml(pytester: Pytester) -> None:
|
|
|
|
x = pytester.mkdir("tests")
|
|
|
|
x.joinpath("conftest.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption("--xyz", action="store_true")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-11-09 12:23:31 +08:00
|
|
|
pytester.makefile(ext=".xml", junit="") # Writes junit.xml
|
|
|
|
result = pytester.runpytest("-h", "--junitxml", "junit.xml")
|
2014-04-02 17:29:23 +08:00
|
|
|
result.stdout.fnmatch_lines(["*--xyz*"])
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2020-12-11 19:40:37 +08:00
|
|
|
def test_conftest_import_order(pytester: Pytester, monkeypatch: MonkeyPatch) -> None:
|
|
|
|
ct1 = pytester.makeconftest("")
|
|
|
|
sub = pytester.mkdir("sub")
|
|
|
|
ct2 = sub / "conftest.py"
|
2023-06-20 19:55:39 +08:00
|
|
|
ct2.write_text("", encoding="utf-8")
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2021-04-06 04:10:03 +08:00
|
|
|
def impct(p, importmode, root):
|
2013-04-18 19:24:53 +08:00
|
|
|
return p
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2015-04-22 20:15:42 +08:00
|
|
|
conftest = PytestPluginManager()
|
2020-12-11 19:40:37 +08:00
|
|
|
conftest._confcutdir = pytester.path
|
2018-05-23 22:48:46 +08:00
|
|
|
monkeypatch.setattr(conftest, "_importconftest", impct)
|
2022-01-09 19:58:29 +08:00
|
|
|
conftest._loadconftestmodules(sub, importmode="prepend", rootpath=pytester.path)
|
|
|
|
mods = cast(List[Path], conftest._getconftestmodules(sub))
|
2020-11-09 12:23:31 +08:00
|
|
|
expected = [ct1, ct2]
|
|
|
|
assert mods == expected
|
2013-11-12 20:45:36 +08:00
|
|
|
|
|
|
|
|
2020-11-09 12:23:31 +08:00
|
|
|
def test_fixture_dependency(pytester: Pytester) -> None:
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makeconftest("")
|
|
|
|
pytester.path.joinpath("__init__.py").touch()
|
2020-11-09 12:23:31 +08:00
|
|
|
sub = pytester.mkdir("sub")
|
2020-12-15 19:02:32 +08:00
|
|
|
sub.joinpath("__init__.py").touch()
|
2020-11-09 12:23:31 +08:00
|
|
|
sub.joinpath("conftest.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
import pytest
|
2013-11-12 20:45:36 +08:00
|
|
|
|
2018-08-24 00:06:17 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def not_needed():
|
|
|
|
assert False, "Should not be called!"
|
2013-11-12 20:45:36 +08:00
|
|
|
|
2018-08-24 00:06:17 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def foo():
|
|
|
|
assert False, "Should not be called!"
|
2013-11-12 20:45:36 +08:00
|
|
|
|
2018-08-24 00:06:17 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def bar(foo):
|
|
|
|
return 'bar'
|
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-11-09 12:23:31 +08:00
|
|
|
subsub = sub.joinpath("subsub")
|
|
|
|
subsub.mkdir()
|
2020-12-15 19:02:32 +08:00
|
|
|
subsub.joinpath("__init__.py").touch()
|
2020-11-09 12:23:31 +08:00
|
|
|
subsub.joinpath("test_bar.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
import pytest
|
2013-11-12 20:45:36 +08:00
|
|
|
|
2018-08-24 00:06:17 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def bar():
|
|
|
|
return 'sub bar'
|
2013-11-12 20:45:36 +08:00
|
|
|
|
2018-08-24 00:06:17 +08:00
|
|
|
def test_event_fixture(bar):
|
|
|
|
assert bar == 'sub bar'
|
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-11-09 12:23:31 +08:00
|
|
|
result = pytester.runpytest("sub")
|
2013-11-12 20:45:36 +08:00
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
2014-07-28 17:48:37 +08:00
|
|
|
|
|
|
|
|
2020-11-09 12:23:31 +08:00
|
|
|
def test_conftest_found_with_double_dash(pytester: Pytester) -> None:
|
|
|
|
sub = pytester.mkdir("sub")
|
|
|
|
sub.joinpath("conftest.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption("--hello-world", action="store_true")
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-11-09 12:23:31 +08:00
|
|
|
p = sub.joinpath("test_hello.py")
|
2023-06-20 19:55:39 +08:00
|
|
|
p.write_text("def test_hello(): pass", encoding="utf-8")
|
2020-11-09 12:23:31 +08:00
|
|
|
result = pytester.runpytest(str(p) + "::test_hello", "-h")
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
"""
|
2014-07-28 17:48:37 +08:00
|
|
|
*--hello-world*
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
|
|
|
)
|
2015-02-27 04:56:44 +08:00
|
|
|
|
|
|
|
|
2019-06-03 06:32:00 +08:00
|
|
|
class TestConftestVisibility:
|
2020-11-09 12:23:31 +08:00
|
|
|
def _setup_tree(self, pytester: Pytester) -> Dict[str, Path]: # for issue616
|
2015-02-27 16:51:53 +08:00
|
|
|
# example mostly taken from:
|
|
|
|
# https://mail.python.org/pipermail/pytest-dev/2014-September/002617.html
|
2020-11-09 12:23:31 +08:00
|
|
|
runner = pytester.mkdir("empty")
|
|
|
|
package = pytester.mkdir("package")
|
2015-02-27 16:51:53 +08:00
|
|
|
|
2020-11-09 12:23:31 +08:00
|
|
|
package.joinpath("conftest.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""\
|
2018-08-24 00:06:17 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def fxtr():
|
|
|
|
return "from-package"
|
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-11-09 12:23:31 +08:00
|
|
|
package.joinpath("test_pkgroot.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""\
|
2018-08-24 00:06:17 +08:00
|
|
|
def test_pkgroot(fxtr):
|
|
|
|
assert fxtr == "from-package"
|
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2015-02-27 16:51:53 +08:00
|
|
|
|
2020-11-09 12:23:31 +08:00
|
|
|
swc = package.joinpath("swc")
|
|
|
|
swc.mkdir()
|
|
|
|
swc.joinpath("__init__.py").touch()
|
|
|
|
swc.joinpath("conftest.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""\
|
2018-08-24 00:06:17 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def fxtr():
|
|
|
|
return "from-swc"
|
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-11-09 12:23:31 +08:00
|
|
|
swc.joinpath("test_with_conftest.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""\
|
2018-08-24 00:06:17 +08:00
|
|
|
def test_with_conftest(fxtr):
|
|
|
|
assert fxtr == "from-swc"
|
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2015-02-27 16:51:53 +08:00
|
|
|
|
2020-11-09 12:23:31 +08:00
|
|
|
snc = package.joinpath("snc")
|
|
|
|
snc.mkdir()
|
|
|
|
snc.joinpath("__init__.py").touch()
|
|
|
|
snc.joinpath("test_no_conftest.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
2018-05-23 22:48:46 +08:00
|
|
|
"""\
|
2018-08-24 00:06:17 +08:00
|
|
|
def test_no_conftest(fxtr):
|
|
|
|
assert fxtr == "from-package" # No local conftest.py, so should
|
|
|
|
# use value from parent dir's
|
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2017-07-21 06:14:54 +08:00
|
|
|
print("created directory structure:")
|
2022-05-09 17:52:57 +08:00
|
|
|
for x in pytester.path.glob("**/"):
|
2020-11-09 12:23:31 +08:00
|
|
|
print(" " + str(x.relative_to(pytester.path)))
|
2015-02-27 16:51:53 +08:00
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
return {"runner": runner, "package": package, "swc": swc, "snc": snc}
|
2015-02-27 16:51:53 +08:00
|
|
|
|
|
|
|
# N.B.: "swc" stands for "subdir with conftest.py"
|
|
|
|
# "snc" stands for "subdir no [i.e. without] conftest.py"
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"chdir,testarg,expect_ntests_passed",
|
|
|
|
[
|
|
|
|
# Effective target: package/..
|
|
|
|
("runner", "..", 3),
|
|
|
|
("package", "..", 3),
|
|
|
|
("swc", "../..", 3),
|
|
|
|
("snc", "../..", 3),
|
|
|
|
# Effective target: package
|
|
|
|
("runner", "../package", 3),
|
|
|
|
("package", ".", 3),
|
|
|
|
("swc", "..", 3),
|
|
|
|
("snc", "..", 3),
|
|
|
|
# Effective target: package/swc
|
|
|
|
("runner", "../package/swc", 1),
|
|
|
|
("package", "./swc", 1),
|
|
|
|
("swc", ".", 1),
|
|
|
|
("snc", "../swc", 1),
|
|
|
|
# Effective target: package/snc
|
|
|
|
("runner", "../package/snc", 1),
|
|
|
|
("package", "./snc", 1),
|
|
|
|
("swc", "../snc", 1),
|
|
|
|
("snc", ".", 1),
|
|
|
|
],
|
|
|
|
)
|
2015-02-27 16:51:53 +08:00
|
|
|
def test_parsefactories_relative_node_ids(
|
2020-11-09 12:23:31 +08:00
|
|
|
self, pytester: Pytester, chdir: str, testarg: str, expect_ntests_passed: int
|
|
|
|
) -> None:
|
2019-04-27 22:25:37 +08:00
|
|
|
"""#616"""
|
2020-11-09 12:23:31 +08:00
|
|
|
dirs = self._setup_tree(pytester)
|
|
|
|
print("pytest run in cwd: %s" % (dirs[chdir].relative_to(pytester.path)))
|
2020-12-15 19:02:32 +08:00
|
|
|
print("pytestarg : %s" % testarg)
|
|
|
|
print("expected pass : %s" % expect_ntests_passed)
|
2020-11-09 12:23:31 +08:00
|
|
|
os.chdir(dirs[chdir])
|
2023-05-27 23:42:37 +08:00
|
|
|
reprec = pytester.inline_run(
|
|
|
|
testarg,
|
|
|
|
"-q",
|
|
|
|
"--traceconfig",
|
|
|
|
"--confcutdir",
|
|
|
|
pytester.path,
|
|
|
|
)
|
2020-11-09 12:23:31 +08:00
|
|
|
reprec.assertoutcome(passed=expect_ntests_passed)
|
2015-07-24 08:48:59 +08:00
|
|
|
|
|
|
|
|
2018-05-23 22:48:46 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"confcutdir,passed,error", [(".", 2, 0), ("src", 1, 1), (None, 1, 1)]
|
|
|
|
)
|
2020-11-09 12:23:31 +08:00
|
|
|
def test_search_conftest_up_to_inifile(
|
|
|
|
pytester: Pytester, confcutdir: str, passed: int, error: int
|
|
|
|
) -> None:
|
2018-05-13 18:06:09 +08:00
|
|
|
"""Test that conftest files are detected only up to an ini file, unless
|
2015-07-24 08:48:59 +08:00
|
|
|
an explicit --confcutdir option is given.
|
|
|
|
"""
|
2020-11-09 12:23:31 +08:00
|
|
|
root = pytester.path
|
|
|
|
src = root.joinpath("src")
|
|
|
|
src.mkdir()
|
2023-06-20 19:55:40 +08:00
|
|
|
src.joinpath("pytest.ini").write_text("[pytest]", encoding="utf-8")
|
2020-11-09 12:23:31 +08:00
|
|
|
src.joinpath("conftest.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def fix1(): pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-11-09 12:23:31 +08:00
|
|
|
src.joinpath("test_foo.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
def test_1(fix1):
|
|
|
|
pass
|
|
|
|
def test_2(out_of_reach):
|
|
|
|
pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-11-09 12:23:31 +08:00
|
|
|
root.joinpath("conftest.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
import pytest
|
|
|
|
@pytest.fixture
|
|
|
|
def out_of_reach(): pass
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2015-07-24 08:48:59 +08:00
|
|
|
|
|
|
|
args = [str(src)]
|
|
|
|
if confcutdir:
|
2020-11-09 12:23:31 +08:00
|
|
|
args = ["--confcutdir=%s" % root.joinpath(confcutdir)]
|
|
|
|
result = pytester.runpytest(*args)
|
2018-05-23 22:48:46 +08:00
|
|
|
match = ""
|
2015-07-24 08:48:59 +08:00
|
|
|
if passed:
|
2018-05-23 22:48:46 +08:00
|
|
|
match += "*%d passed*" % passed
|
2015-07-24 08:48:59 +08:00
|
|
|
if error:
|
2018-05-23 22:48:46 +08:00
|
|
|
match += "*%d error*" % error
|
2015-07-24 08:48:59 +08:00
|
|
|
result.stdout.fnmatch_lines(match)
|
2015-09-28 19:34:28 +08:00
|
|
|
|
|
|
|
|
2020-11-09 12:23:31 +08:00
|
|
|
def test_issue1073_conftest_special_objects(pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-08-24 00:06:17 +08:00
|
|
|
"""\
|
2017-02-17 02:41:51 +08:00
|
|
|
class DontTouchMe(object):
|
2015-09-28 19:34:28 +08:00
|
|
|
def __getattr__(self, x):
|
|
|
|
raise Exception('cant touch me')
|
|
|
|
|
|
|
|
x = DontTouchMe()
|
2018-08-24 00:06:17 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-11-09 12:23:31 +08:00
|
|
|
pytester.makepyfile(
|
2018-08-24 00:06:17 +08:00
|
|
|
"""\
|
2015-09-28 19:34:28 +08:00
|
|
|
def test_some():
|
|
|
|
pass
|
2018-08-24 00:06:17 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-11-09 12:23:31 +08:00
|
|
|
res = pytester.runpytest()
|
2015-09-28 19:34:28 +08:00
|
|
|
assert res.ret == 0
|
2016-07-05 17:39:12 +08:00
|
|
|
|
|
|
|
|
2020-11-09 12:23:31 +08:00
|
|
|
def test_conftest_exception_handling(pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2018-08-24 00:06:17 +08:00
|
|
|
"""\
|
2016-07-05 17:39:12 +08:00
|
|
|
raise ValueError()
|
2018-08-24 00:06:17 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-11-09 12:23:31 +08:00
|
|
|
pytester.makepyfile(
|
2018-08-24 00:06:17 +08:00
|
|
|
"""\
|
2016-07-05 17:39:12 +08:00
|
|
|
def test_some():
|
|
|
|
pass
|
2018-08-24 00:06:17 +08:00
|
|
|
"""
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-11-09 12:23:31 +08:00
|
|
|
res = pytester.runpytest()
|
2016-07-05 17:39:12 +08:00
|
|
|
assert res.ret == 4
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "raise ValueError()" in [line.strip() for line in res.errlines]
|
2016-11-21 22:50:21 +08:00
|
|
|
|
|
|
|
|
2020-11-09 12:23:31 +08:00
|
|
|
def test_hook_proxy(pytester: Pytester) -> None:
|
2016-11-21 22:50:21 +08:00
|
|
|
"""Session's gethookproxy() would cache conftests incorrectly (#2016).
|
|
|
|
It was decided to remove the cache altogether.
|
|
|
|
"""
|
2020-11-09 12:23:31 +08:00
|
|
|
pytester.makepyfile(
|
2018-05-23 22:48:46 +08:00
|
|
|
**{
|
|
|
|
"root/demo-0/test_foo1.py": "def test1(): pass",
|
|
|
|
"root/demo-a/test_foo2.py": "def test1(): pass",
|
2018-08-24 00:06:17 +08:00
|
|
|
"root/demo-a/conftest.py": """\
|
2021-12-03 20:14:09 +08:00
|
|
|
def pytest_ignore_collect(collection_path, config):
|
2016-11-21 22:50:21 +08:00
|
|
|
return True
|
|
|
|
""",
|
2018-05-23 22:48:46 +08:00
|
|
|
"root/demo-b/test_foo3.py": "def test1(): pass",
|
|
|
|
"root/demo-c/test_foo4.py": "def test1(): pass",
|
|
|
|
}
|
|
|
|
)
|
2020-11-09 12:23:31 +08:00
|
|
|
result = pytester.runpytest()
|
2018-05-23 22:48:46 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
|
|
|
["*test_foo1.py*", "*test_foo3.py*", "*test_foo4.py*", "*3 passed*"]
|
|
|
|
)
|
2017-06-01 04:55:30 +08:00
|
|
|
|
|
|
|
|
2020-11-09 12:23:31 +08:00
|
|
|
def test_required_option_help(pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest("assert 0")
|
|
|
|
x = pytester.mkdir("x")
|
|
|
|
x.joinpath("conftest.py").write_text(
|
2018-08-24 00:06:17 +08:00
|
|
|
textwrap.dedent(
|
|
|
|
"""\
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption("--xyz", action="store_true", required=True)
|
2018-05-23 22:48:46 +08:00
|
|
|
"""
|
2023-06-20 19:55:39 +08:00
|
|
|
),
|
|
|
|
encoding="utf-8",
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2020-11-09 12:23:31 +08:00
|
|
|
result = pytester.runpytest("-h", x)
|
2019-10-06 01:18:51 +08:00
|
|
|
result.stdout.no_fnmatch_line("*argument --xyz is required*")
|
2018-05-23 22:48:46 +08:00
|
|
|
assert "general:" in result.stdout.str()
|