From be36ce68cc16143aa5c709bec2ed06aa9cbdc516 Mon Sep 17 00:00:00 2001 From: Michael Seifert Date: Tue, 26 Sep 2023 15:03:09 +0200 Subject: [PATCH] [refactor] Existing tests in test_module_marker are executed with pytest.Pytester to avoid applying pytestmark to subsequent tests in the test module. Signed-off-by: Michael Seifert --- tests/markers/test_module_marker.py | 65 +++++++++++++++++------------ 1 file changed, 39 insertions(+), 26 deletions(-) diff --git a/tests/markers/test_module_marker.py b/tests/markers/test_module_marker.py index 2f69dbc9..c870edb7 100644 --- a/tests/markers/test_module_marker.py +++ b/tests/markers/test_module_marker.py @@ -1,39 +1,52 @@ -"""Test if pytestmark works when defined in a module.""" -import asyncio +from textwrap import dedent -import pytest +from pytest import Pytester -pytestmark = pytest.mark.asyncio +def test_asyncio_mark_works_on_module_level(pytester: Pytester): + pytester.makepyfile( + dedent( + """\ + import asyncio -class TestPyTestMark: - async def test_is_asyncio(self, event_loop, sample_fixture): - assert asyncio.get_event_loop() + import pytest - counter = 1 + pytestmark = pytest.mark.asyncio - async def inc(): - nonlocal counter - counter += 1 - await asyncio.sleep(0) - await asyncio.ensure_future(inc()) - assert counter == 2 + class TestPyTestMark: + async def test_is_asyncio(self, event_loop, sample_fixture): + assert asyncio.get_event_loop() + counter = 1 -async def test_is_asyncio(event_loop, sample_fixture): - assert asyncio.get_event_loop() - counter = 1 + async def inc(): + nonlocal counter + counter += 1 + await asyncio.sleep(0) - async def inc(): - nonlocal counter - counter += 1 - await asyncio.sleep(0) + await asyncio.ensure_future(inc()) + assert counter == 2 - await asyncio.ensure_future(inc()) - assert counter == 2 + async def test_is_asyncio(event_loop, sample_fixture): + assert asyncio.get_event_loop() + counter = 1 -@pytest.fixture -def sample_fixture(): - return None + async def inc(): + nonlocal counter + counter += 1 + await asyncio.sleep(0) + + await asyncio.ensure_future(inc()) + assert counter == 2 + + + @pytest.fixture + def sample_fixture(): + return None + """ + ) + ) + result = pytester.runpytest("--asyncio-mode=strict") + result.assert_outcomes(passed=2)