2024-01-28 21:12:42 +08:00
|
|
|
# mypy: allow-untyped-defs
|
2015-04-29 22:40:51 +08:00
|
|
|
import os
|
2020-12-15 19:02:32 +08:00
|
|
|
import shutil
|
2017-12-27 11:47:26 +08:00
|
|
|
import sys
|
|
|
|
import types
|
2020-05-01 19:40:17 +08:00
|
|
|
from typing import List
|
2015-04-29 22:40:51 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
from _pytest.config import Config
|
2020-02-11 05:43:30 +08:00
|
|
|
from _pytest.config import ExitCode
|
2018-10-25 15:01:29 +08:00
|
|
|
from _pytest.config import PytestPluginManager
|
2019-03-28 02:10:33 +08:00
|
|
|
from _pytest.config.exceptions import UsageError
|
2018-10-25 15:01:29 +08:00
|
|
|
from _pytest.main import Session
|
2020-12-15 19:02:32 +08:00
|
|
|
from _pytest.monkeypatch import MonkeyPatch
|
|
|
|
from _pytest.pathlib import import_path
|
2020-12-11 19:40:37 +08:00
|
|
|
from _pytest.pytester import Pytester
|
2018-10-25 15:01:29 +08:00
|
|
|
import pytest
|
2016-11-21 22:50:21 +08:00
|
|
|
|
2015-04-29 22:40:51 +08:00
|
|
|
|
|
|
|
@pytest.fixture
|
2020-05-01 19:40:17 +08:00
|
|
|
def pytestpm() -> PytestPluginManager:
|
2015-04-29 22:40:51 +08:00
|
|
|
return PytestPluginManager()
|
|
|
|
|
2017-07-17 07:25:09 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestPytestPluginInteractions:
|
2020-12-11 19:40:37 +08:00
|
|
|
def test_addhooks_conftestplugin(
|
2020-12-15 19:02:32 +08:00
|
|
|
self, pytester: Pytester, _config_for_test: Config
|
2020-12-11 19:40:37 +08:00
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(
|
2015-04-29 22:40:51 +08:00
|
|
|
newhooks="""
|
|
|
|
def pytest_myhook(xyz):
|
|
|
|
"new hook"
|
|
|
|
"""
|
|
|
|
)
|
2020-12-11 19:40:37 +08:00
|
|
|
conf = pytester.makeconftest(
|
2015-04-29 22:40:51 +08:00
|
|
|
"""
|
|
|
|
import newhooks
|
|
|
|
def pytest_addhooks(pluginmanager):
|
2018-12-12 06:02:36 +08:00
|
|
|
pluginmanager.add_hookspecs(newhooks)
|
2015-04-29 22:40:51 +08:00
|
|
|
def pytest_myhook(xyz):
|
|
|
|
return xyz + 1
|
|
|
|
"""
|
|
|
|
)
|
2019-03-28 05:34:37 +08:00
|
|
|
config = _config_for_test
|
2015-04-29 22:40:51 +08:00
|
|
|
pm = config.pluginmanager
|
|
|
|
pm.hook.pytest_addhooks.call_historic(
|
2017-07-17 07:25:07 +08:00
|
|
|
kwargs=dict(pluginmanager=config.pluginmanager)
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2021-04-06 04:10:03 +08:00
|
|
|
config.pluginmanager._importconftest(
|
|
|
|
conf, importmode="prepend", rootpath=pytester.path
|
|
|
|
)
|
2017-07-17 07:25:09 +08:00
|
|
|
# print(config.pluginmanager.get_plugins())
|
2015-04-29 22:40:51 +08:00
|
|
|
res = config.hook.pytest_myhook(xyz=10)
|
|
|
|
assert res == [11]
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_addhooks_nohooks(self, pytester: Pytester) -> None:
|
|
|
|
pytester.makeconftest(
|
2015-04-29 22:40:51 +08:00
|
|
|
"""
|
|
|
|
import sys
|
|
|
|
def pytest_addhooks(pluginmanager):
|
2018-12-12 06:02:36 +08:00
|
|
|
pluginmanager.add_hookspecs(sys)
|
2015-04-29 22:40:51 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
res = pytester.runpytest()
|
2015-04-29 22:40:51 +08:00
|
|
|
assert res.ret != 0
|
|
|
|
res.stderr.fnmatch_lines(["*did not find*sys*"])
|
|
|
|
|
2020-12-11 19:40:37 +08:00
|
|
|
def test_do_option_postinitialize(self, pytester: Pytester) -> None:
|
|
|
|
config = pytester.parseconfigure()
|
2015-04-29 22:40:51 +08:00
|
|
|
assert not hasattr(config.option, "test123")
|
2020-12-11 19:40:37 +08:00
|
|
|
p = pytester.makepyfile(
|
2015-04-29 22:40:51 +08:00
|
|
|
"""
|
|
|
|
def pytest_addoption(parser):
|
|
|
|
parser.addoption('--test123', action="store_true",
|
|
|
|
default=True)
|
|
|
|
"""
|
|
|
|
)
|
2021-04-06 04:10:03 +08:00
|
|
|
config.pluginmanager._importconftest(
|
|
|
|
p, importmode="prepend", rootpath=pytester.path
|
|
|
|
)
|
2015-04-29 22:40:51 +08:00
|
|
|
assert config.option.test123
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_configure(self, pytester: Pytester) -> None:
|
|
|
|
config = pytester.parseconfig()
|
2017-11-04 23:17:20 +08:00
|
|
|
values = []
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class A:
|
2020-01-17 02:42:29 +08:00
|
|
|
def pytest_configure(self):
|
2017-11-04 23:17:20 +08:00
|
|
|
values.append(self)
|
2015-04-29 22:40:51 +08:00
|
|
|
|
|
|
|
config.pluginmanager.register(A())
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 0
|
2015-04-29 22:40:51 +08:00
|
|
|
config._do_configure()
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 1
|
2015-04-29 22:40:51 +08:00
|
|
|
config.pluginmanager.register(A()) # leads to a configured() plugin
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 2
|
|
|
|
assert values[0] != values[1]
|
2015-04-29 22:40:51 +08:00
|
|
|
|
|
|
|
config._ensure_unconfigure()
|
|
|
|
config.pluginmanager.register(A())
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) == 2
|
2015-04-29 22:40:51 +08:00
|
|
|
|
2023-12-15 23:54:07 +08:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
not sys.platform.startswith("win"),
|
|
|
|
reason="requires a case-insensitive file system",
|
|
|
|
)
|
|
|
|
def test_conftestpath_case_sensitivity(self, pytester: Pytester) -> None:
|
|
|
|
"""Unit test for issue #9765."""
|
|
|
|
config = pytester.parseconfig()
|
|
|
|
pytester.makepyfile(**{"tests/conftest.py": ""})
|
|
|
|
|
|
|
|
conftest = pytester.path.joinpath("tests/conftest.py")
|
|
|
|
conftest_upper_case = pytester.path.joinpath("TESTS/conftest.py")
|
|
|
|
|
|
|
|
mod = config.pluginmanager._importconftest(
|
|
|
|
conftest,
|
|
|
|
importmode="prepend",
|
|
|
|
rootpath=pytester.path,
|
|
|
|
)
|
|
|
|
plugin = config.pluginmanager.get_plugin(str(conftest))
|
|
|
|
assert plugin is mod
|
|
|
|
|
|
|
|
mod_uppercase = config.pluginmanager._importconftest(
|
|
|
|
conftest_upper_case,
|
|
|
|
importmode="prepend",
|
|
|
|
rootpath=pytester.path,
|
|
|
|
)
|
|
|
|
plugin_uppercase = config.pluginmanager.get_plugin(str(conftest_upper_case))
|
|
|
|
assert plugin_uppercase is mod_uppercase
|
|
|
|
|
|
|
|
# No str(conftestpath) normalization so conftest should be imported
|
|
|
|
# twice and modules should be different objects
|
|
|
|
assert mod is not mod_uppercase
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_hook_tracing(self, _config_for_test: Config) -> None:
|
2019-03-28 05:34:37 +08:00
|
|
|
pytestpm = _config_for_test.pluginmanager # fully initialized with plugins
|
2015-04-29 22:40:51 +08:00
|
|
|
saveindent = []
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class api1:
|
2015-04-29 22:40:51 +08:00
|
|
|
def pytest_plugin_registered(self):
|
|
|
|
saveindent.append(pytestpm.trace.root.indent)
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class api2:
|
2015-04-29 22:40:51 +08:00
|
|
|
def pytest_plugin_registered(self):
|
|
|
|
saveindent.append(pytestpm.trace.root.indent)
|
|
|
|
raise ValueError()
|
2016-11-21 04:59:15 +08:00
|
|
|
|
2020-10-06 09:13:05 +08:00
|
|
|
values: List[str] = []
|
2017-11-04 23:17:20 +08:00
|
|
|
pytestpm.trace.root.setwriter(values.append)
|
2015-04-29 22:40:51 +08:00
|
|
|
undo = pytestpm.enable_tracing()
|
|
|
|
try:
|
|
|
|
indent = pytestpm.trace.root.indent
|
|
|
|
p = api1()
|
|
|
|
pytestpm.register(p)
|
|
|
|
assert pytestpm.trace.root.indent == indent
|
2017-11-04 23:17:20 +08:00
|
|
|
assert len(values) >= 2
|
|
|
|
assert "pytest_plugin_registered" in values[0]
|
|
|
|
assert "finish" in values[1]
|
2015-04-29 22:40:51 +08:00
|
|
|
|
2017-11-04 23:17:20 +08:00
|
|
|
values[:] = []
|
2015-04-29 22:40:51 +08:00
|
|
|
with pytest.raises(ValueError):
|
|
|
|
pytestpm.register(api2())
|
|
|
|
assert pytestpm.trace.root.indent == indent
|
|
|
|
assert saveindent[0] > indent
|
|
|
|
finally:
|
|
|
|
undo()
|
|
|
|
|
2020-12-11 19:40:37 +08:00
|
|
|
def test_hook_proxy(self, pytester: Pytester) -> None:
|
2016-11-21 22:50:21 +08:00
|
|
|
"""Test the gethookproxy function(#2016)"""
|
2020-12-11 19:40:37 +08:00
|
|
|
config = pytester.parseconfig()
|
2019-10-17 03:52:04 +08:00
|
|
|
session = Session.from_config(config)
|
2020-12-11 19:40:37 +08:00
|
|
|
pytester.makepyfile(**{"tests/conftest.py": "", "tests/subdir/conftest.py": ""})
|
2016-11-21 22:50:21 +08:00
|
|
|
|
2020-12-11 19:40:37 +08:00
|
|
|
conftest1 = pytester.path.joinpath("tests/conftest.py")
|
|
|
|
conftest2 = pytester.path.joinpath("tests/subdir/conftest.py")
|
2016-11-21 22:50:21 +08:00
|
|
|
|
2021-04-06 04:10:03 +08:00
|
|
|
config.pluginmanager._importconftest(
|
|
|
|
conftest1, importmode="prepend", rootpath=pytester.path
|
|
|
|
)
|
2020-12-11 19:40:37 +08:00
|
|
|
ihook_a = session.gethookproxy(pytester.path / "tests")
|
2016-11-21 22:50:21 +08:00
|
|
|
assert ihook_a is not None
|
2021-04-06 04:10:03 +08:00
|
|
|
config.pluginmanager._importconftest(
|
|
|
|
conftest2, importmode="prepend", rootpath=pytester.path
|
|
|
|
)
|
2020-12-11 19:40:37 +08:00
|
|
|
ihook_b = session.gethookproxy(pytester.path / "tests")
|
2016-11-21 22:50:21 +08:00
|
|
|
assert ihook_a is not ihook_b
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_hook_with_addoption(self, pytester: Pytester) -> None:
|
2019-10-31 02:18:13 +08:00
|
|
|
"""Test that hooks can be used in a call to pytest_addoption"""
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2019-10-31 02:18:13 +08:00
|
|
|
newhooks="""
|
|
|
|
import pytest
|
|
|
|
@pytest.hookspec(firstresult=True)
|
|
|
|
def pytest_default_value():
|
|
|
|
pass
|
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(
|
2019-10-31 02:18:13 +08:00
|
|
|
myplugin="""
|
|
|
|
import newhooks
|
|
|
|
def pytest_addhooks(pluginmanager):
|
|
|
|
pluginmanager.add_hookspecs(newhooks)
|
|
|
|
def pytest_addoption(parser, pluginmanager):
|
|
|
|
default_value = pluginmanager.hook.pytest_default_value()
|
|
|
|
parser.addoption("--config", help="Config, defaults to %(default)s", default=default_value)
|
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makeconftest(
|
2019-10-31 02:18:13 +08:00
|
|
|
"""
|
|
|
|
pytest_plugins=("myplugin",)
|
|
|
|
def pytest_default_value():
|
|
|
|
return "default_value"
|
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
res = pytester.runpytest("--help")
|
2019-10-31 02:18:13 +08:00
|
|
|
res.stdout.fnmatch_lines(["*--config=CONFIG*default_value*"])
|
|
|
|
|
2015-04-29 22:40:51 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_default_markers(pytester: Pytester) -> None:
|
|
|
|
result = pytester.runpytest("--markers")
|
2015-04-29 22:40:51 +08:00
|
|
|
result.stdout.fnmatch_lines(["*tryfirst*first*", "*trylast*last*"])
|
|
|
|
|
2016-03-09 06:18:13 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_importplugin_error_message(
|
|
|
|
pytester: Pytester, pytestpm: PytestPluginManager
|
|
|
|
) -> None:
|
2016-03-09 06:18:13 +08:00
|
|
|
"""Don't hide import errors when importing plugins and provide
|
|
|
|
an easy to debug message.
|
2016-10-13 04:46:47 +08:00
|
|
|
|
|
|
|
See #375 and #1998.
|
2016-03-09 06:18:13 +08:00
|
|
|
"""
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.syspathinsert(pytester.path)
|
|
|
|
pytester.makepyfile(
|
2019-06-03 06:40:34 +08:00
|
|
|
qwe="""\
|
2017-09-19 20:23:07 +08:00
|
|
|
def test_traceback():
|
2019-06-03 06:40:34 +08:00
|
|
|
raise ImportError('Not possible to import: ☺')
|
2017-09-19 20:23:07 +08:00
|
|
|
test_traceback()
|
2019-06-03 06:40:34 +08:00
|
|
|
"""
|
2016-10-13 04:46:47 +08:00
|
|
|
)
|
2015-04-29 22:40:51 +08:00
|
|
|
with pytest.raises(ImportError) as excinfo:
|
|
|
|
pytestpm.import_plugin("qwe")
|
2017-09-19 20:23:07 +08:00
|
|
|
|
2019-03-22 23:56:00 +08:00
|
|
|
assert str(excinfo.value).endswith(
|
|
|
|
'Error importing plugin "qwe": Not possible to import: ☺'
|
|
|
|
)
|
|
|
|
assert "in test_traceback" in str(excinfo.traceback[-1])
|
2015-04-29 22:40:51 +08:00
|
|
|
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestPytestPluginManager:
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_register_imported_modules(self) -> None:
|
2015-04-29 22:40:51 +08:00
|
|
|
pm = PytestPluginManager()
|
2017-12-27 11:47:26 +08:00
|
|
|
mod = types.ModuleType("x.y.pytest_hello")
|
2015-04-29 22:40:51 +08:00
|
|
|
pm.register(mod)
|
|
|
|
assert pm.is_registered(mod)
|
2017-11-04 23:17:20 +08:00
|
|
|
values = pm.get_plugins()
|
|
|
|
assert mod in values
|
2018-11-23 02:05:10 +08:00
|
|
|
pytest.raises(ValueError, pm.register, mod)
|
2015-04-29 22:40:51 +08:00
|
|
|
pytest.raises(ValueError, lambda: pm.register(mod))
|
2017-07-17 07:25:09 +08:00
|
|
|
# assert not pm.is_registered(mod2)
|
2017-11-04 23:17:20 +08:00
|
|
|
assert pm.get_plugins() == values
|
2015-04-29 22:40:51 +08:00
|
|
|
|
|
|
|
def test_canonical_import(self, monkeypatch):
|
2017-12-27 11:47:26 +08:00
|
|
|
mod = types.ModuleType("pytest_xyz")
|
|
|
|
monkeypatch.setitem(sys.modules, "pytest_xyz", mod)
|
2015-04-29 22:40:51 +08:00
|
|
|
pm = PytestPluginManager()
|
|
|
|
pm.import_plugin("pytest_xyz")
|
|
|
|
assert pm.get_plugin("pytest_xyz") == mod
|
|
|
|
assert pm.is_registered(mod)
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_consider_module(
|
|
|
|
self, pytester: Pytester, pytestpm: PytestPluginManager
|
|
|
|
) -> None:
|
|
|
|
pytester.syspathinsert()
|
|
|
|
pytester.makepyfile(pytest_p1="#")
|
|
|
|
pytester.makepyfile(pytest_p2="#")
|
2017-12-27 11:47:26 +08:00
|
|
|
mod = types.ModuleType("temp")
|
2020-05-01 19:40:17 +08:00
|
|
|
mod.__dict__["pytest_plugins"] = ["pytest_p1", "pytest_p2"]
|
2015-04-29 22:40:51 +08:00
|
|
|
pytestpm.consider_module(mod)
|
2023-08-26 14:53:45 +08:00
|
|
|
p1 = pytestpm.get_plugin("pytest_p1")
|
|
|
|
assert p1 is not None
|
|
|
|
assert p1.__name__ == "pytest_p1"
|
|
|
|
p2 = pytestpm.get_plugin("pytest_p2")
|
|
|
|
assert p2 is not None
|
|
|
|
assert p2.__name__ == "pytest_p2"
|
2015-04-29 22:40:51 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_consider_module_import_module(
|
|
|
|
self, pytester: Pytester, _config_for_test: Config
|
|
|
|
) -> None:
|
2019-03-28 05:34:37 +08:00
|
|
|
pytestpm = _config_for_test.pluginmanager
|
2017-12-27 11:47:26 +08:00
|
|
|
mod = types.ModuleType("x")
|
2020-05-01 19:40:17 +08:00
|
|
|
mod.__dict__["pytest_plugins"] = "pytest_a"
|
2020-12-15 19:02:32 +08:00
|
|
|
aplugin = pytester.makepyfile(pytest_a="#")
|
|
|
|
reprec = pytester.make_hook_recorder(pytestpm)
|
|
|
|
pytester.syspathinsert(aplugin.parent)
|
2015-04-29 22:40:51 +08:00
|
|
|
pytestpm.consider_module(mod)
|
|
|
|
call = reprec.getcall(pytestpm.hook.pytest_plugin_registered.name)
|
|
|
|
assert call.plugin.__name__ == "pytest_a"
|
|
|
|
|
|
|
|
# check that it is not registered twice
|
|
|
|
pytestpm.consider_module(mod)
|
2017-11-04 23:17:20 +08:00
|
|
|
values = reprec.getcalls("pytest_plugin_registered")
|
|
|
|
assert len(values) == 1
|
2015-04-29 22:40:51 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_consider_env_fails_to_import(
|
|
|
|
self, monkeypatch: MonkeyPatch, pytestpm: PytestPluginManager
|
|
|
|
) -> None:
|
2015-04-29 22:40:51 +08:00
|
|
|
monkeypatch.setenv("PYTEST_PLUGINS", "nonexisting", prepend=",")
|
|
|
|
with pytest.raises(ImportError):
|
|
|
|
pytestpm.consider_env()
|
|
|
|
|
2018-12-12 06:02:36 +08:00
|
|
|
@pytest.mark.filterwarnings("always")
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_plugin_skip(self, pytester: Pytester, monkeypatch: MonkeyPatch) -> None:
|
|
|
|
p = pytester.makepyfile(
|
2015-04-29 22:40:51 +08:00
|
|
|
skipping1="""
|
|
|
|
import pytest
|
2018-12-12 06:02:36 +08:00
|
|
|
pytest.skip("hello", allow_module_level=True)
|
2015-04-29 22:40:51 +08:00
|
|
|
"""
|
|
|
|
)
|
2020-12-15 19:02:32 +08:00
|
|
|
shutil.copy(p, p.with_name("skipping2.py"))
|
2015-04-29 22:40:51 +08:00
|
|
|
monkeypatch.setenv("PYTEST_PLUGINS", "skipping2")
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest("-p", "skipping1", syspathinsert=True)
|
2019-06-07 18:58:51 +08:00
|
|
|
assert result.ret == ExitCode.NO_TESTS_COLLECTED
|
2015-04-29 22:40:51 +08:00
|
|
|
result.stdout.fnmatch_lines(
|
2017-03-08 07:47:43 +08:00
|
|
|
["*skipped plugin*skipping1*hello*", "*skipped plugin*skipping2*hello*"]
|
2015-04-29 22:40:51 +08:00
|
|
|
)
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_consider_env_plugin_instantiation(
|
|
|
|
self,
|
|
|
|
pytester: Pytester,
|
|
|
|
monkeypatch: MonkeyPatch,
|
|
|
|
pytestpm: PytestPluginManager,
|
|
|
|
) -> None:
|
|
|
|
pytester.syspathinsert()
|
|
|
|
pytester.makepyfile(xy123="#")
|
2015-04-29 22:40:51 +08:00
|
|
|
monkeypatch.setitem(os.environ, "PYTEST_PLUGINS", "xy123")
|
|
|
|
l1 = len(pytestpm.get_plugins())
|
|
|
|
pytestpm.consider_env()
|
|
|
|
l2 = len(pytestpm.get_plugins())
|
|
|
|
assert l2 == l1 + 1
|
|
|
|
assert pytestpm.get_plugin("xy123")
|
|
|
|
pytestpm.consider_env()
|
|
|
|
l3 = len(pytestpm.get_plugins())
|
|
|
|
assert l2 == l3
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_pluginmanager_ENV_startup(
|
|
|
|
self, pytester: Pytester, monkeypatch: MonkeyPatch
|
|
|
|
) -> None:
|
|
|
|
pytester.makepyfile(pytest_x500="#")
|
|
|
|
p = pytester.makepyfile(
|
2015-04-29 22:40:51 +08:00
|
|
|
"""
|
|
|
|
import pytest
|
|
|
|
def test_hello(pytestconfig):
|
|
|
|
plugin = pytestconfig.pluginmanager.get_plugin('pytest_x500')
|
|
|
|
assert plugin is not None
|
|
|
|
"""
|
|
|
|
)
|
|
|
|
monkeypatch.setenv("PYTEST_PLUGINS", "pytest_x500", prepend=",")
|
2020-12-15 19:02:32 +08:00
|
|
|
result = pytester.runpytest(p, syspathinsert=True)
|
2015-04-29 22:40:51 +08:00
|
|
|
assert result.ret == 0
|
|
|
|
result.stdout.fnmatch_lines(["*1 passed*"])
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_import_plugin_importname(
|
|
|
|
self, pytester: Pytester, pytestpm: PytestPluginManager
|
|
|
|
) -> None:
|
2018-11-23 02:05:10 +08:00
|
|
|
pytest.raises(ImportError, pytestpm.import_plugin, "qweqwex.y")
|
|
|
|
pytest.raises(ImportError, pytestpm.import_plugin, "pytest_qweqwx.y")
|
2015-04-29 22:40:51 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.syspathinsert()
|
2015-04-29 22:40:51 +08:00
|
|
|
pluginname = "pytest_hello"
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.makepyfile(**{pluginname: ""})
|
2015-04-29 22:40:51 +08:00
|
|
|
pytestpm.import_plugin("pytest_hello")
|
|
|
|
len1 = len(pytestpm.get_plugins())
|
|
|
|
pytestpm.import_plugin("pytest_hello")
|
|
|
|
len2 = len(pytestpm.get_plugins())
|
|
|
|
assert len1 == len2
|
|
|
|
plugin1 = pytestpm.get_plugin("pytest_hello")
|
2023-08-26 14:53:45 +08:00
|
|
|
assert plugin1 is not None
|
2015-04-29 22:40:51 +08:00
|
|
|
assert plugin1.__name__.endswith("pytest_hello")
|
|
|
|
plugin2 = pytestpm.get_plugin("pytest_hello")
|
|
|
|
assert plugin2 is plugin1
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_import_plugin_dotted_name(
|
|
|
|
self, pytester: Pytester, pytestpm: PytestPluginManager
|
|
|
|
) -> None:
|
2018-11-23 02:05:10 +08:00
|
|
|
pytest.raises(ImportError, pytestpm.import_plugin, "qweqwex.y")
|
|
|
|
pytest.raises(ImportError, pytestpm.import_plugin, "pytest_qweqwex.y")
|
2015-04-29 22:40:51 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
pytester.syspathinsert()
|
2023-06-20 19:55:40 +08:00
|
|
|
pytester.mkpydir("pkg").joinpath("plug.py").write_text("x=3", encoding="utf-8")
|
2015-04-29 22:40:51 +08:00
|
|
|
pluginname = "pkg.plug"
|
|
|
|
pytestpm.import_plugin(pluginname)
|
|
|
|
mod = pytestpm.get_plugin("pkg.plug")
|
2023-08-26 14:53:45 +08:00
|
|
|
assert mod is not None
|
2015-04-29 22:40:51 +08:00
|
|
|
assert mod.x == 3
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_consider_conftest_deps(
|
2020-12-30 17:56:09 +08:00
|
|
|
self,
|
|
|
|
pytester: Pytester,
|
|
|
|
pytestpm: PytestPluginManager,
|
2020-12-15 19:02:32 +08:00
|
|
|
) -> None:
|
2021-04-06 04:10:03 +08:00
|
|
|
mod = import_path(
|
|
|
|
pytester.makepyfile("pytest_plugins='xyz'"), root=pytester.path
|
|
|
|
)
|
2015-04-29 22:40:51 +08:00
|
|
|
with pytest.raises(ImportError):
|
2023-12-15 23:54:07 +08:00
|
|
|
pytestpm.consider_conftest(mod, registration_name="unused")
|
2015-04-29 22:40:51 +08:00
|
|
|
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
class TestPytestPluginManagerBootstrapming:
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_preparse_args(self, pytestpm: PytestPluginManager) -> None:
|
2015-04-29 22:40:51 +08:00
|
|
|
pytest.raises(
|
2017-07-17 07:25:07 +08:00
|
|
|
ImportError, lambda: pytestpm.consider_preparse(["xyz", "-p", "hello123"])
|
2018-05-23 22:48:46 +08:00
|
|
|
)
|
2015-04-29 22:40:51 +08:00
|
|
|
|
2018-12-09 21:23:08 +08:00
|
|
|
# Handles -p without space (#3532).
|
|
|
|
with pytest.raises(ImportError) as excinfo:
|
|
|
|
pytestpm.consider_preparse(["-phello123"])
|
|
|
|
assert '"hello123"' in excinfo.value.args[0]
|
|
|
|
pytestpm.consider_preparse(["-pno:hello123"])
|
|
|
|
|
2019-03-21 11:38:11 +08:00
|
|
|
# Handles -p without following arg (when used without argparse).
|
|
|
|
pytestpm.consider_preparse(["-p"])
|
|
|
|
|
2019-03-28 02:10:33 +08:00
|
|
|
with pytest.raises(UsageError, match="^plugin main cannot be disabled$"):
|
|
|
|
pytestpm.consider_preparse(["-p", "no:main"])
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_plugin_prevent_register(self, pytestpm: PytestPluginManager) -> None:
|
2015-04-29 22:40:51 +08:00
|
|
|
pytestpm.consider_preparse(["xyz", "-p", "no:abc"])
|
|
|
|
l1 = pytestpm.get_plugins()
|
|
|
|
pytestpm.register(42, name="abc")
|
|
|
|
l2 = pytestpm.get_plugins()
|
|
|
|
assert len(l2) == len(l1)
|
|
|
|
assert 42 not in l2
|
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_plugin_prevent_register_unregistered_alredy_registered(
|
|
|
|
self, pytestpm: PytestPluginManager
|
|
|
|
) -> None:
|
2015-04-29 22:40:51 +08:00
|
|
|
pytestpm.register(42, name="abc")
|
|
|
|
l1 = pytestpm.get_plugins()
|
|
|
|
assert 42 in l1
|
|
|
|
pytestpm.consider_preparse(["xyz", "-p", "no:abc"])
|
|
|
|
l2 = pytestpm.get_plugins()
|
|
|
|
assert 42 not in l2
|
2018-11-08 04:32:23 +08:00
|
|
|
|
|
|
|
def test_plugin_prevent_register_stepwise_on_cacheprovider_unregister(
|
2020-12-15 19:02:32 +08:00
|
|
|
self, pytestpm: PytestPluginManager
|
|
|
|
) -> None:
|
2020-07-18 17:35:13 +08:00
|
|
|
"""From PR #4304: The only way to unregister a module is documented at
|
2021-07-06 15:09:04 +08:00
|
|
|
the end of https://docs.pytest.org/en/stable/how-to/plugins.html.
|
2018-11-08 04:32:23 +08:00
|
|
|
|
2020-07-18 17:35:13 +08:00
|
|
|
When unregister cacheprovider, then unregister stepwise too.
|
2018-11-05 07:14:35 +08:00
|
|
|
"""
|
|
|
|
pytestpm.register(42, name="cacheprovider")
|
|
|
|
pytestpm.register(43, name="stepwise")
|
|
|
|
l1 = pytestpm.get_plugins()
|
|
|
|
assert 42 in l1
|
|
|
|
assert 43 in l1
|
|
|
|
pytestpm.consider_preparse(["xyz", "-p", "no:cacheprovider"])
|
|
|
|
l2 = pytestpm.get_plugins()
|
|
|
|
assert 42 not in l2
|
|
|
|
assert 43 not in l2
|
2019-03-16 22:29:27 +08:00
|
|
|
|
2020-12-15 19:02:32 +08:00
|
|
|
def test_blocked_plugin_can_be_used(self, pytestpm: PytestPluginManager) -> None:
|
2019-03-16 22:29:27 +08:00
|
|
|
pytestpm.consider_preparse(["xyz", "-p", "no:abc", "-p", "abc"])
|
|
|
|
|
|
|
|
assert pytestpm.has_plugin("abc")
|
|
|
|
assert not pytestpm.is_blocked("abc")
|
|
|
|
assert not pytestpm.is_blocked("pytest_abc")
|