Merge branch 'main' into teardown_fixture_order

This commit is contained in:
John Litborn 2024-01-17 16:32:24 +01:00 committed by GitHub
commit 6e5a8fea11
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 54 additions and 42 deletions

View File

@ -64,6 +64,7 @@ repos:
additional_dependencies:
- iniconfig>=1.1.0
- attrs>=19.2.0
- pluggy
- packaging
- tomli
- types-pkg_resources

View File

@ -0,0 +1 @@
The :hook:`pytest_plugin_registered` hook has a new ``plugin_name`` parameter containing the name by which ``plugin`` is registered.

View File

@ -3,7 +3,6 @@
.. sidebar:: Next Open Trainings and Events
- `Professional Testing with Python <https://python-academy.com/courses/python_course_testing.html>`_, via `Python Academy <https://www.python-academy.com/>`_ (3 day in-depth training):
* **March 5th to 7th 2024**, Leipzig, Germany / Remote
* **June 11th to 13th 2024**, Remote
* **March 4th to 6th 2025**, Leipzig, Germany / Remote
- `pytest development sprint <https://github.com/pytest-dev/pytest/discussions/11655>`_, June 2024 (`date poll <https://nuudel.digitalcourage.de/2tEsEpRcwMNcAXVO>`_)

View File

@ -490,15 +490,19 @@ class PytestPluginManager(PluginManager):
)
)
return None
ret: Optional[str] = super().register(plugin, name)
if ret:
plugin_name = super().register(plugin, name)
if plugin_name is not None:
self.hook.pytest_plugin_registered.call_historic(
kwargs=dict(plugin=plugin, manager=self)
kwargs=dict(
plugin=plugin,
plugin_name=plugin_name,
manager=self,
)
)
if isinstance(plugin, types.ModuleType):
self.consider_module(plugin)
return ret
return plugin_name
def getplugin(self, name: str):
# Support deprecated naming because plugins (xdist e.g.) use it.

View File

@ -1492,25 +1492,27 @@ class FixtureManager:
return FuncFixtureInfo(argnames, initialnames, names_closure, arg2fixturedefs)
def pytest_plugin_registered(self, plugin: _PluggyPlugin) -> None:
nodeid = None
def pytest_plugin_registered(self, plugin: _PluggyPlugin, plugin_name: str) -> None:
# Fixtures defined in conftest plugins are only visible to within the
# conftest's directory. This is unlike fixtures in non-conftest plugins
# which have global visibility. So for conftests, construct the base
# nodeid from the plugin name (which is the conftest path).
if plugin_name and plugin_name.endswith("conftest.py"):
# Note: we explicitly do *not* use `plugin.__file__` here -- The
# difference is that plugin_name has the correct capitalization on
# case-insensitive systems (Windows) and other normalization issues
# (issue #11816).
conftestpath = absolutepath(plugin_name)
try:
p = absolutepath(plugin.__file__) # type: ignore[attr-defined]
except AttributeError:
pass
else:
# Construct the base nodeid which is later used to check
# what fixtures are visible for particular tests (as denoted
# by their test id).
if p.name == "conftest.py":
try:
nodeid = str(p.parent.relative_to(self.config.rootpath))
nodeid = str(conftestpath.parent.relative_to(self.config.rootpath))
except ValueError:
nodeid = ""
if nodeid == ".":
nodeid = ""
if os.sep != nodes.SEP:
nodeid = nodeid.replace(os.sep, nodes.SEP)
else:
nodeid = None
self.parsefactories(plugin, nodeid)

View File

@ -54,7 +54,7 @@ def pytest_addhooks(pluginmanager: "PytestPluginManager") -> None:
"""Called at plugin registration time to allow adding new hooks via a call to
:func:`pluginmanager.add_hookspecs(module_or_class, prefix) <pytest.PytestPluginManager.add_hookspecs>`.
:param pytest.PytestPluginManager pluginmanager: The pytest plugin manager.
:param pluginmanager: The pytest plugin manager.
.. note::
This hook is incompatible with hook wrappers.
@ -63,12 +63,15 @@ def pytest_addhooks(pluginmanager: "PytestPluginManager") -> None:
@hookspec(historic=True)
def pytest_plugin_registered(
plugin: "_PluggyPlugin", manager: "PytestPluginManager"
plugin: "_PluggyPlugin",
plugin_name: str,
manager: "PytestPluginManager",
) -> None:
"""A new pytest plugin got registered.
:param plugin: The plugin module or instance.
:param pytest.PytestPluginManager manager: pytest plugin manager.
:param plugin_name: The name by which the plugin is registered.
:param manager: The pytest plugin manager.
.. note::
This hook is incompatible with hook wrappers.
@ -86,13 +89,13 @@ def pytest_addoption(parser: "Parser", pluginmanager: "PytestPluginManager") ->
files situated at the tests root directory due to how pytest
:ref:`discovers plugins during startup <pluginorder>`.
:param pytest.Parser parser:
:param parser:
To add command line options, call
:py:func:`parser.addoption(...) <pytest.Parser.addoption>`.
To add ini-file values call :py:func:`parser.addini(...)
<pytest.Parser.addini>`.
:param pytest.PytestPluginManager pluginmanager:
:param pluginmanager:
The pytest plugin manager, which can be used to install :py:func:`~pytest.hookspec`'s
or :py:func:`~pytest.hookimpl`'s and allow one plugin to call another plugin's hooks
to change how command line options are added.
@ -127,7 +130,7 @@ def pytest_configure(config: "Config") -> None:
.. note::
This hook is incompatible with hook wrappers.
:param pytest.Config config: The pytest config object.
:param config: The pytest config object.
"""
@ -156,18 +159,6 @@ def pytest_cmdline_parse(
"""
@hookspec(firstresult=True)
def pytest_cmdline_main(config: "Config") -> Optional[Union["ExitCode", int]]:
"""Called for performing the main command line action. The default
implementation will invoke the configure hooks and runtest_mainloop.
Stops at first non-None result, see :ref:`firstresult`.
:param config: The pytest config object.
:returns: The exit code.
"""
def pytest_load_initial_conftests(
early_config: "Config", parser: "Parser", args: List[str]
) -> None:
@ -183,6 +174,20 @@ def pytest_load_initial_conftests(
"""
@hookspec(firstresult=True)
def pytest_cmdline_main(config: "Config") -> Optional[Union["ExitCode", int]]:
"""Called for performing the main command line action.
The default implementation will invoke the configure hooks and
:hook:`pytest_runtestloop`.
Stops at first non-None result, see :ref:`firstresult`.
:param config: The pytest config object.
:returns: The exit code.
"""
# -------------------------------------------------------------------------
# collection hooks
# -------------------------------------------------------------------------
@ -435,7 +440,7 @@ def pytest_make_parametrize_id(
:param config: The pytest config object.
:param val: The parametrized value.
:param str argname: The automatic parameter name produced by pytest.
:param argname: The automatic parameter name produced by pytest.
"""