pytest-dev / pytest-dev/pytest

pytest.skip in conftest/__init__ files doesn't work as expected

Open
#12,371 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: collection
Dominant language
Python
Stars
14.5k
Forks
3.4k
Avg merge
2d 9h
Merged PRs (30d)
35

Description

Problem description

Pytest has special mechanism to skip modules described here.

But when applying this to conftest.py (or __init__.py), this call skip entire package, not only that module. And this happened even with allow_module_level=False. For example:

# content of tests/test_package_A/confest.py
import sys
import pytest

if sys.version_info < (3, 11):
    pytest.skip("Python>=3.11 is required. ", allow_module_level=False)
Screenshot 2024-05-26 at 12 43 42

Pytest and operating system versions

pytest 8.2.1
MacOS Monterey 12.3.1 (21E258)

Expecting behaviour

1- Do not allow using pytest.skip(allow_module_level=False) in conftest.py (or __init__.py). The same, as It's not allowed for test modules. So, expected error message might be the same:

Using pytest.skip outside of a test will skip the entire module. If that's your intention, pass allow_module_level=True

2- And even more, I would suggest that another flag should be set explicitly to skip entire directory/package. For example:

pytest.skip("Python>=3.11 is required. ", allow_package_level=True)   # package level flag specified here

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the behavior using the conftest.py example and compare it with the module-skipping guidance in the linked documentation. Trace how pytest handles pytest.skip in conftest.py and init.py, then define tests that verify the intended error for allow_module_level=False and the agreed behavior for package-level skipping.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.