Marking a package with pytestmark
in __init__.py
#11612
-
I was looking for ways to attach a marker to all tests inside a package and I stumbled upon two issues that use However, the docs only mention markers for classes and modules. Is this approach for marking tests in a package "officially" supported? In other words, is it reasonable for a plugin (in this case pytest-asyncio) to rely on package markers? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
Even more frustrating is that package markers are clearly visible when stopping in "get_closest_marker" while debugging. After researching the topic more, I understood that it works during debugging session because of automatic "obj" property execution for packages by watching variables. Well, good news. This seems to be a design feature to prevent heavy packages eager initialization. So came up with the following workaround (my packages are not heavy): def pytest_collection_modifyitems(config, items: list[pytest.Item]):
MARK_NAME = 'xxx'
for item in items:
# inline modified `get_closest_marker`
for node in reversed(item.listchain()):
if hasattr(node, "obj"):
unused_obj = node.obj # force package to load
for mark in node.own_markers:
if getattr(mark, "name", None) == MARK_NAME:
... # perform changes we need.
break
... |
Beta Was this translation helpful? Give feedback.
-
package markers are currently not a official feature, if they work by accident, we should be more explicit about them and either elevate them to official, or disallow them allowing them plainly seems like a potential way to break module level markers for just init.py |
Beta Was this translation helpful? Give feedback.
package markers are currently not a official feature, if they work by accident, we should be more explicit about them and either elevate them to official, or disallow them
allowing them plainly seems like a potential way to break module level markers for just init.py