From edb9ae0f740f2c11f716e646be750bed082c3bb6 Mon Sep 17 00:00:00 2001 From: Michael Seifert Date: Thu, 7 Dec 2023 09:34:51 +0100 Subject: [PATCH] [fix] Fixes a bug that caused an internal pytest error when using unittest.SkipTest in a module. Signed-off-by: Michael Seifert --- docs/source/reference/changelog.rst | 1 + pytest_asyncio/plugin.py | 5 +++++ tests/test_skips.py | 17 +++++++++++++++++ 3 files changed, 23 insertions(+) diff --git a/docs/source/reference/changelog.rst b/docs/source/reference/changelog.rst index 7e063d24..f06157e4 100644 --- a/docs/source/reference/changelog.rst +++ b/docs/source/reference/changelog.rst @@ -5,6 +5,7 @@ Changelog 0.23.3 (UNRELEASED) =================== - Fixes a bug that caused event loops to be closed prematurely when using async generator fixtures with class scope or wider in a function-scoped test `#708 `_ +- Fixes a bug that caused an internal pytest error when using unittest.SkipTest in a module `#711 `_ 0.23.2 (2023-12-04) diff --git a/pytest_asyncio/plugin.py b/pytest_asyncio/plugin.py index 0a92730c..4af34fec 100644 --- a/pytest_asyncio/plugin.py +++ b/pytest_asyncio/plugin.py @@ -26,6 +26,7 @@ Union, overload, ) +from unittest import SkipTest import pytest from _pytest.outcomes import OutcomeException @@ -622,6 +623,10 @@ def scoped_event_loop( # kwargs is missing. These cases are handled correctly when they happen inside # Collector.collect(), but this hook runs before the actual collect call. return + except SkipTest: + # Users may also have a unittest suite that they run with pytest. + # Therefore, we need to handle SkipTest to avoid breaking test collection. + 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 diff --git a/tests/test_skips.py b/tests/test_skips.py index 17d0befc..abd9dd70 100644 --- a/tests/test_skips.py +++ b/tests/test_skips.py @@ -88,3 +88,20 @@ async def test_is_skipped(): ) result = pytester.runpytest("--asyncio-mode=auto") result.assert_outcomes(errors=1) + + +def test_unittest_skiptest_compatibility(pytester: Pytester): + pytester.makepyfile( + dedent( + """\ + from unittest import SkipTest + + raise SkipTest("Skip all tests") + + async def test_is_skipped(): + pass + """ + ) + ) + result = pytester.runpytest("--asyncio-mode=auto") + result.assert_outcomes(skipped=1)