Skip to content

Commit

Permalink
[refactor] Existing tests in test_module_marker are executed with pyt…
Browse files Browse the repository at this point in the history
…est.Pytester to avoid applying pytestmark to subsequent tests in the test module.

Signed-off-by: Michael Seifert <m.seifert@digitalernachschub.de>
  • Loading branch information
seifertm committed Sep 26, 2023
1 parent 2eefc69 commit be36ce6
Showing 1 changed file with 39 additions and 26 deletions.
65 changes: 39 additions & 26 deletions tests/markers/test_module_marker.py
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit be36ce6

Please sign in to comment.