Skip to content

Package level marker does not appear in iter_markers() during collection tests #9245

Closed

Description

@Denis-Alexeev
  • a detailed description of the bug or problem you are having
  • output of pip list from the virtual environment you are using
  • pytest and operating system versions
  • minimal example if possible

Package level marker does not appear in Item during tests collection.
If I have
__init__.py

import pytest

pytestmark = pytest.mark.package_mark

conftest.py

def pytest_itemcollected(item):
    print(item, list(item.iter_markers()))

test.py

@pytest.mark.test_mark
def test_1():
    pass

Then the output is: collecting ... <Function test_1> [Mark(name='test_mark', args=(), kwargs={})]

But if I modify conftest.py

def pytest_itemcollected(item):
    item.getparent(pytest.Package).obj
    print(item, list(item.iter_markers()))

Then the output is: collecting ... <Function test_1> [Mark(name='test_mark', args=(), kwargs={}), Mark(name='package_mark', args=(), kwargs={})]

Environment:

bare venv with pytest-6.2.5
MacOS BigSur
python 3.10

Activity

  1. Denis-Alexeev commented on Oct 28, 2021

    @Denis-Alexeev
    Author

    may be related to #5830

  2. bluetech commented on Oct 28, 2021

    @bluetech
    Member

    This is indeed a duplicate of #5830.

  3. bluetech commented on Oct 28, 2021

    @bluetech
    Member

    A hacky workaround is to not rely on pytestmark but to add the mark manually. You can add this to your conftest.py (you'll need to adjust the nodeid):

    def pytest_collectstart(collector: pytest.Collector) -> None:
        if collector.nodeid == 't/__init__.py':
            collector.add_marker(pytest.mark.package_mark)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions