Skip to content

Commit be36ce6

Browse files
committed
[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 <[email protected]>
1 parent 2eefc69 commit be36ce6

File tree

1 file changed

+39
-26
lines changed

1 file changed

+39
-26
lines changed

Diff for: tests/markers/test_module_marker.py

+39-26
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,52 @@
1-
"""Test if pytestmark works when defined in a module."""
2-
import asyncio
1+
from textwrap import dedent
32

4-
import pytest
3+
from pytest import Pytester
54

6-
pytestmark = pytest.mark.asyncio
75

6+
def test_asyncio_mark_works_on_module_level(pytester: Pytester):
7+
pytester.makepyfile(
8+
dedent(
9+
"""\
10+
import asyncio
811
9-
class TestPyTestMark:
10-
async def test_is_asyncio(self, event_loop, sample_fixture):
11-
assert asyncio.get_event_loop()
12+
import pytest
1213
13-
counter = 1
14+
pytestmark = pytest.mark.asyncio
1415
15-
async def inc():
16-
nonlocal counter
17-
counter += 1
18-
await asyncio.sleep(0)
1916
20-
await asyncio.ensure_future(inc())
21-
assert counter == 2
17+
class TestPyTestMark:
18+
async def test_is_asyncio(self, event_loop, sample_fixture):
19+
assert asyncio.get_event_loop()
2220
21+
counter = 1
2322
24-
async def test_is_asyncio(event_loop, sample_fixture):
25-
assert asyncio.get_event_loop()
26-
counter = 1
23+
async def inc():
24+
nonlocal counter
25+
counter += 1
26+
await asyncio.sleep(0)
2727
28-
async def inc():
29-
nonlocal counter
30-
counter += 1
31-
await asyncio.sleep(0)
28+
await asyncio.ensure_future(inc())
29+
assert counter == 2
3230
33-
await asyncio.ensure_future(inc())
34-
assert counter == 2
3531
32+
async def test_is_asyncio(event_loop, sample_fixture):
33+
assert asyncio.get_event_loop()
34+
counter = 1
3635
37-
@pytest.fixture
38-
def sample_fixture():
39-
return None
36+
async def inc():
37+
nonlocal counter
38+
counter += 1
39+
await asyncio.sleep(0)
40+
41+
await asyncio.ensure_future(inc())
42+
assert counter == 2
43+
44+
45+
@pytest.fixture
46+
def sample_fixture():
47+
return None
48+
"""
49+
)
50+
)
51+
result = pytester.runpytest("--asyncio-mode=strict")
52+
result.assert_outcomes(passed=2)

0 commit comments

Comments
 (0)