-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[refactor] Existing tests in test_module_marker are executed with pyt…
…est.Pytester to avoid applying pytestmark to subsequent tests in the test module. Signed-off-by: Michael Seifert <[email protected]>
- Loading branch information
Showing
1 changed file
with
39 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |