Skip to content

Commit

Permalink
[fix] Fixes a bug that caused an internal pytest error when using mod…
Browse files Browse the repository at this point in the history
…ule-level skips.

Signed-off-by: Michael Seifert <[email protected]>
  • Loading branch information
seifertm committed Dec 3, 2023
1 parent 176d558 commit a214c3e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 3 deletions.
5 changes: 5 additions & 0 deletions docs/source/reference/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Changelog
=========

0.23.1 (2023-12-03)
===================
- Fixes a bug that caused an internal pytest error when using module-level skips `#701 <https://github.com/pytest-dev/pytest-asyncio/issues/701>`_


0.23.0 (2023-12-03)
===================
This release is backwards-compatible with v0.21.
Expand Down
11 changes: 10 additions & 1 deletion pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
)

import pytest
from _pytest.outcomes import OutcomeException
from pytest import (
Class,
Collector,
Expand Down Expand Up @@ -607,7 +608,15 @@ def scoped_event_loop(
# know it exists. We work around this by attaching the fixture function to the
# collected Python class, where it will be picked up by pytest.Class.collect()
# or pytest.Module.collect(), respectively
collector.obj.__pytest_asyncio_scoped_event_loop = scoped_event_loop
try:
collector.obj.__pytest_asyncio_scoped_event_loop = scoped_event_loop
except (OutcomeException, Collector.CollectError):
# Accessing Module.obj triggers a module import executing module-level
# statements. A module-level pytest.skip statement raises the "Skipped"
# OutcomeException or a Collector.CollectError, if the "allow_module_level"
# kwargs is missing. These cases are handled correctly when they happen inside
# Collector.collect(), but this hook runs before the actual collect call.
return
# When collector is a package, collector.obj is the package's __init__.py.
# pytest doesn't seem to collect fixtures in __init__.py.
# Using parsefactories to collect fixtures in __init__.py their baseid will end
Expand Down
56 changes: 54 additions & 2 deletions tests/test_pytest_skip.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from pytest import Pytester


def test_asyncio_marker_compatibility_with_skip(pytester: Pytester):
def test_asyncio_strict_mode_skip(pytester: Pytester):
pytester.makepyfile(
dedent(
"""\
Expand All @@ -21,7 +21,7 @@ async def test_no_warning_on_skip():
result.assert_outcomes(skipped=1)


def test_asyncio_auto_mode_compatibility_with_skip(pytester: Pytester):
def test_asyncio_auto_mode_skip(pytester: Pytester):
pytester.makepyfile(
dedent(
"""\
Expand All @@ -36,3 +36,55 @@ async def test_no_warning_on_skip():
)
result = pytester.runpytest("--asyncio-mode=auto")
result.assert_outcomes(skipped=1)


def test_asyncio_strict_mode_module_level_skip(pytester: Pytester):
pytester.makepyfile(
dedent(
"""\
import pytest
pytest.skip("Skip all tests", allow_module_level=True)
@pytest.mark.asyncio
async def test_is_skipped():
pass
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict")
result.assert_outcomes(skipped=1)


def test_asyncio_auto_mode_module_level_skip(pytester: Pytester):
pytester.makepyfile(
dedent(
"""\
import pytest
pytest.skip("Skip all tests", allow_module_level=True)
async def test_is_skipped():
pass
"""
)
)
result = pytester.runpytest("--asyncio-mode=auto")
result.assert_outcomes(skipped=1)


def test_asyncio_auto_mode_wrong_skip_usage(pytester: Pytester):
pytester.makepyfile(
dedent(
"""\
import pytest
pytest.skip("Skip all tests")
async def test_is_skipped():
pass
"""
)
)
result = pytester.runpytest("--asyncio-mode=auto")
result.assert_outcomes(errors=1)

0 comments on commit a214c3e

Please sign in to comment.