Skip to content

Commit a214c3e

Browse files
committed
[fix] Fixes a bug that caused an internal pytest error when using module-level skips.
Signed-off-by: Michael Seifert <[email protected]>
1 parent 176d558 commit a214c3e

File tree

3 files changed

+69
-3
lines changed

3 files changed

+69
-3
lines changed

docs/source/reference/changelog.rst

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
Changelog
33
=========
44

5+
0.23.1 (2023-12-03)
6+
===================
7+
- Fixes a bug that caused an internal pytest error when using module-level skips `#701 <https://github.com/pytest-dev/pytest-asyncio/issues/701>`_
8+
9+
510
0.23.0 (2023-12-03)
611
===================
712
This release is backwards-compatible with v0.21.

pytest_asyncio/plugin.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
)
2929

3030
import pytest
31+
from _pytest.outcomes import OutcomeException
3132
from pytest import (
3233
Class,
3334
Collector,
@@ -607,7 +608,15 @@ def scoped_event_loop(
607608
# know it exists. We work around this by attaching the fixture function to the
608609
# collected Python class, where it will be picked up by pytest.Class.collect()
609610
# or pytest.Module.collect(), respectively
610-
collector.obj.__pytest_asyncio_scoped_event_loop = scoped_event_loop
611+
try:
612+
collector.obj.__pytest_asyncio_scoped_event_loop = scoped_event_loop
613+
except (OutcomeException, Collector.CollectError):
614+
# Accessing Module.obj triggers a module import executing module-level
615+
# statements. A module-level pytest.skip statement raises the "Skipped"
616+
# OutcomeException or a Collector.CollectError, if the "allow_module_level"
617+
# kwargs is missing. These cases are handled correctly when they happen inside
618+
# Collector.collect(), but this hook runs before the actual collect call.
619+
return
611620
# When collector is a package, collector.obj is the package's __init__.py.
612621
# pytest doesn't seem to collect fixtures in __init__.py.
613622
# Using parsefactories to collect fixtures in __init__.py their baseid will end

tests/test_pytest_skip.py

+54-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from pytest import Pytester
44

55

6-
def test_asyncio_marker_compatibility_with_skip(pytester: Pytester):
6+
def test_asyncio_strict_mode_skip(pytester: Pytester):
77
pytester.makepyfile(
88
dedent(
99
"""\
@@ -21,7 +21,7 @@ async def test_no_warning_on_skip():
2121
result.assert_outcomes(skipped=1)
2222

2323

24-
def test_asyncio_auto_mode_compatibility_with_skip(pytester: Pytester):
24+
def test_asyncio_auto_mode_skip(pytester: Pytester):
2525
pytester.makepyfile(
2626
dedent(
2727
"""\
@@ -36,3 +36,55 @@ async def test_no_warning_on_skip():
3636
)
3737
result = pytester.runpytest("--asyncio-mode=auto")
3838
result.assert_outcomes(skipped=1)
39+
40+
41+
def test_asyncio_strict_mode_module_level_skip(pytester: Pytester):
42+
pytester.makepyfile(
43+
dedent(
44+
"""\
45+
import pytest
46+
47+
pytest.skip("Skip all tests", allow_module_level=True)
48+
49+
@pytest.mark.asyncio
50+
async def test_is_skipped():
51+
pass
52+
"""
53+
)
54+
)
55+
result = pytester.runpytest("--asyncio-mode=strict")
56+
result.assert_outcomes(skipped=1)
57+
58+
59+
def test_asyncio_auto_mode_module_level_skip(pytester: Pytester):
60+
pytester.makepyfile(
61+
dedent(
62+
"""\
63+
import pytest
64+
65+
pytest.skip("Skip all tests", allow_module_level=True)
66+
67+
async def test_is_skipped():
68+
pass
69+
"""
70+
)
71+
)
72+
result = pytester.runpytest("--asyncio-mode=auto")
73+
result.assert_outcomes(skipped=1)
74+
75+
76+
def test_asyncio_auto_mode_wrong_skip_usage(pytester: Pytester):
77+
pytester.makepyfile(
78+
dedent(
79+
"""\
80+
import pytest
81+
82+
pytest.skip("Skip all tests")
83+
84+
async def test_is_skipped():
85+
pass
86+
"""
87+
)
88+
)
89+
result = pytester.runpytest("--asyncio-mode=auto")
90+
result.assert_outcomes(errors=1)

0 commit comments

Comments
 (0)