Skip to content

Package-level pytestmark in __init__.py no longer propagates to tests #14737

Open

Description

@SuYanqi

a detailed description of the bug or problem you are having

A package-level pytestmark = pytest.mark.skip(...) defined in __init__.py is no longer propagated to tests inside that package. Tests that should be skipped run and fail instead.

This is a regression introduced by PR #6197.

pytest and operating system versions

pytest 9.1.1
Python 3.11.13
Linux

minimal example if possible

from pathlib import Path
import shutil
import pytest

root = Path("/tmp/pytest_pkg_skip_test")
shutil.rmtree(root, ignore_errors=True)
package = root / "skippedpkg"
package.mkdir(parents=True)
(package / "__init__.py").write_text(
    "import pytest\n"
    "pytestmark = pytest.mark.skip(reason='package is disabled')\n"
)
(package / "test_skipped.py").write_text(
    "def test_should_be_skipped():\n"
    "    assert False\n"
)

result = pytest.main(["-q", str(package)])
print("exit code:", int(result))
shutil.rmtree(root, ignore_errors=True)

Expected: 1 skipped, exit code 0
Actual: 1 failed (assert False runs because skip marker is not applied), exit code 1

Activity

  1. Zuhef commented on Jul 29, 2026

    @Zuhef

    I can reproduce this, but I do not think it is a regression - the behaviour looks unchanged for as far back as I tested, so the fix is probably a policy decision rather than a straight bug fix.

    Reproduction

    Your script reproduces on current main (56b196e92): 1 failed, exit 1, where you expected 1 skipped.

    Version matrix

    I ran the same package against released versions in clean virtualenvs, so the only variable is the pytest version:

    pytest result
    7.4.4 1 failed
    8.3.5 1 failed
    9.1.1 1 failed
    main (56b196e) 1 failed

    The marker is ignored in all of them, so there is no version where this worked that a bisect could land between.

    Why #6197 is not quite the culprit

    #6197 is in the history as 19a15a94e, "Fix incorrect discovery of non-test __init__.py files" (Nov 2019), and the changelog records it as:

    :issue:6197: Revert "The first test in a package (__init__.py) marked with @pytest.mark.skip is now correctly skipped."

    That commit removed 24 lines from src/_pytest/python.py and deleted the corresponding test from testing/test_skipping.py, in order to fix #6194. So the current behaviour was chosen deliberately roughly six years ago as the cost of not collecting non-test __init__.py files - it is the intended state of the code, not something that drifted.

    Why I stopped short of a patch

    Restoring propagation means collecting __init__.py as a module again in some form, which is the thing #6197 was written to stop. A patch that simply re-applies the reverted behaviour would likely reintroduce #6194, so this needs a maintainer's call on which of two documented behaviours wins.

    If the desired outcome is that package-level marks apply, I would be glad to implement whichever direction you prefer:

    1. Collect marks from __init__.py without collecting it as a test module - read pytestmark from the package during Package collection and fold it into the child modules' marks, leaving discovery untouched.
    2. Document the current behaviour instead, since conftest.py already provides a supported way to apply marks package-wide (pytestmark in conftest.py, or pytest_collection_modifyitems), and close this as working-as-intended.

    Option 1 keeps the reporter's expectation and avoids #6194 because nothing new is collected, only marks are read - but I did not want to assume that is wanted given the explicit revert.

  2. aryansk commented on Aug 20, 2026

    @aryansk
  3. added a commit that references this issue on Nov 11, 2026
    1e8155b
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