Skip to content

Fix duplication of markers in async tests #838

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/source/reference/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
Changelog
=========

0.23.8 (UNRELEASED)
===================
- Fixes a bug that caused duplicate markers in async tests `#813 <https://github.com/pytest-dev/pytest-asyncio/issues/813>`_

Known issues
------------
As of v0.23, pytest-asyncio attaches an asyncio event loop to each item of the test suite (i.e. session, packages, modules, classes, functions) and allows tests to be run in those loops when marked accordingly. Pytest-asyncio currently assumes that async fixture scope is correlated with the new event loop scope. This prevents fixtures from being evaluated independently from the event loop scope and breaks some existing test suites (see `#706`_). For example, a test suite may require all fixtures and tests to run in the same event loop, but have async fixtures that are set up and torn down for each module. If you're affected by this issue, please continue using the v0.21 release, until it is resolved.


0.23.7 (2024-05-19)
===================
- Silence deprecation warnings about unclosed event loops that occurred with certain CPython patch releases `#817 <https://github.com/pytest-dev/pytest-asyncio/pull/817>`_
Expand Down
3 changes: 2 additions & 1 deletion pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,8 @@ def _from_function(cls, function: Function, /) -> Function:
keywords=function.keywords,
originalname=function.originalname,
)
subclass_instance.own_markers.extend(function.own_markers)
subclass_instance.own_markers = function.own_markers
assert subclass_instance.own_markers == function.own_markers
subclassed_function_signature = inspect.signature(subclass_instance.obj)
if "event_loop" in subclassed_function_signature.parameters:
subclass_instance.warn(
Expand Down
4 changes: 2 additions & 2 deletions tests/hypothesis/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ async def test_mark_inner(n):
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict", "-W default")
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default")
result.assert_outcomes(passed=1)


Expand Down Expand Up @@ -67,7 +67,7 @@ async def test_explicit_fixture_request(event_loop, n):
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict", "-W default")
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default")
result.assert_outcomes(passed=1, warnings=2)
result.stdout.fnmatch_lines(
[
Expand Down
33 changes: 32 additions & 1 deletion tests/markers/test_function_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async def test_remember_loop(event_loop):
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict", "-W default")
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default")
result.assert_outcomes(passed=1, warnings=1)
result.stdout.fnmatch_lines(
'*is asynchronous and explicitly requests the "event_loop" fixture*'
Expand Down Expand Up @@ -197,3 +197,34 @@ async def test_anything():
)
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
result.assert_outcomes(warnings=0, passed=1)


def test_asyncio_mark_does_not_duplicate_other_marks_in_auto_mode(
pytester: Pytester,
):
pytester.makeconftest(
dedent(
"""\
def pytest_configure(config):
config.addinivalue_line(
"markers", "dummy_marker: mark used for testing purposes"
)
"""
)
)
pytester.makepyfile(
dedent(
"""\
import pytest

@pytest.mark.dummy_marker
async def test_markers_not_duplicated(request):
markers = []
for node, marker in request.node.iter_markers_with_node():
markers.append(marker)
assert len(markers) == 2
"""
)
)
result = pytester.runpytest_subprocess("--asyncio-mode=auto")
result.assert_outcomes(warnings=0, passed=1)
2 changes: 1 addition & 1 deletion tests/markers/test_module_scope.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def sample_fixture():
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict", "-W default")
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default")
result.assert_outcomes(passed=2, warnings=2)
result.stdout.fnmatch_lines(
'*is asynchronous and explicitly requests the "event_loop" fixture*'
Expand Down
4 changes: 2 additions & 2 deletions tests/modes/test_strict_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ async def test_anything():
"""
)
)
result = testdir.runpytest("--asyncio-mode=strict", "-W default")
result = testdir.runpytest_subprocess("--asyncio-mode=strict", "-W default")
result.assert_outcomes(skipped=1, warnings=1)
result.stdout.fnmatch_lines(["*async def functions are not natively supported*"])

Expand All @@ -100,7 +100,7 @@ async def test_anything(any_fixture):
"""
)
)
result = testdir.runpytest("--asyncio-mode=strict", "-W default")
result = testdir.runpytest_subprocess("--asyncio-mode=strict", "-W default")
result.assert_outcomes(skipped=1, warnings=2)
result.stdout.fnmatch_lines(
[
Expand Down
14 changes: 7 additions & 7 deletions tests/test_asyncio_mark.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_a():
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict", "-W default")
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default")
result.assert_outcomes(passed=1)
result.stdout.fnmatch_lines(
["*is marked with '@pytest.mark.asyncio' but it is not an async function.*"]
Expand All @@ -36,7 +36,7 @@ async def test_a():
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict", "-W default")
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default")
result.assert_outcomes(xfailed=1, warnings=1)
result.stdout.fnmatch_lines(
["*Tests based on asynchronous generators are not supported*"]
Expand All @@ -54,7 +54,7 @@ async def test_a():
"""
)
)
result = pytester.runpytest("--asyncio-mode=auto", "-W default")
result = pytester.runpytest_subprocess("--asyncio-mode=auto", "-W default")
result.assert_outcomes(xfailed=1, warnings=1)
result.stdout.fnmatch_lines(
["*Tests based on asynchronous generators are not supported*"]
Expand All @@ -76,7 +76,7 @@ async def test_a(self):
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict", "-W default")
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default")
result.assert_outcomes(xfailed=1, warnings=1)
result.stdout.fnmatch_lines(
["*Tests based on asynchronous generators are not supported*"]
Expand All @@ -96,7 +96,7 @@ async def test_a():
"""
)
)
result = pytester.runpytest("--asyncio-mode=auto", "-W default")
result = pytester.runpytest_subprocess("--asyncio-mode=auto", "-W default")
result.assert_outcomes(xfailed=1, warnings=1)
result.stdout.fnmatch_lines(
["*Tests based on asynchronous generators are not supported*"]
Expand All @@ -119,7 +119,7 @@ async def test_a():
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict", "-W default")
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default")
result.assert_outcomes(xfailed=1, warnings=1)
result.stdout.fnmatch_lines(
["*Tests based on asynchronous generators are not supported*"]
Expand All @@ -139,7 +139,7 @@ async def test_a():
"""
)
)
result = pytester.runpytest("--asyncio-mode=auto", "-W default")
result = pytester.runpytest_subprocess("--asyncio-mode=auto", "-W default")
result.assert_outcomes(xfailed=1, warnings=1)
result.stdout.fnmatch_lines(
["*Tests based on asynchronous generators are not supported*"]
Expand Down
6 changes: 3 additions & 3 deletions tests/test_event_loop_fixture_finalizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ async def test_async_with_explicit_fixture_request(event_loop):
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict", "-W default")
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default")
result.assert_outcomes(passed=1, warnings=1)
result.stdout.fnmatch_lines(
'*is asynchronous and explicitly requests the "event_loop" fixture*'
Expand Down Expand Up @@ -113,7 +113,7 @@ async def test_ends_with_unclosed_loop():
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict", "-W", "default")
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W", "default")
result.assert_outcomes(passed=1, warnings=2)
result.stdout.fnmatch_lines("*unclosed event loop*")

Expand All @@ -135,6 +135,6 @@ async def test_ends_with_unclosed_loop():
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict", "-W", "default")
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W", "default")
result.assert_outcomes(passed=1, warnings=1)
result.stdout.fnmatch_lines("*unclosed event loop*")
8 changes: 4 additions & 4 deletions tests/test_event_loop_fixture_override_deprecation.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async def test_emits_warning():
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict", "-W default")
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default")
result.assert_outcomes(passed=1, warnings=1)
result.stdout.fnmatch_lines(
["*event_loop fixture provided by pytest-asyncio has been redefined*"]
Expand Down Expand Up @@ -50,7 +50,7 @@ async def test_emits_warning_when_requested_explicitly(event_loop):
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict", "-W default")
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default")
result.assert_outcomes(passed=1, warnings=2)
result.stdout.fnmatch_lines(
["*event_loop fixture provided by pytest-asyncio has been redefined*"]
Expand Down Expand Up @@ -80,7 +80,7 @@ def test_emits_no_warning():
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict")
result = pytester.runpytest_subprocess("--asyncio-mode=strict")
result.assert_outcomes(passed=1, warnings=0)


Expand All @@ -107,5 +107,5 @@ def test_emits_warning(uses_event_loop):
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict", "-W default")
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default")
result.assert_outcomes(passed=1, warnings=1)
10 changes: 5 additions & 5 deletions tests/test_explicit_event_loop_fixture_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async def test_coroutine_emits_warning(event_loop):
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict", "-W default")
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default")
result.assert_outcomes(passed=1, warnings=1)
result.stdout.fnmatch_lines(
['*is asynchronous and explicitly requests the "event_loop" fixture*']
Expand All @@ -39,7 +39,7 @@ async def test_coroutine_emits_warning(self, event_loop):
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict", "-W default")
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default")
result.assert_outcomes(passed=1, warnings=1)
result.stdout.fnmatch_lines(
['*is asynchronous and explicitly requests the "event_loop" fixture*']
Expand All @@ -62,7 +62,7 @@ async def test_coroutine_emits_warning(event_loop):
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict", "-W default")
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default")
result.assert_outcomes(passed=1, warnings=1)
result.stdout.fnmatch_lines(
['*is asynchronous and explicitly requests the "event_loop" fixture*']
Expand All @@ -88,7 +88,7 @@ async def test_uses_fixture(emits_warning):
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict", "-W default")
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default")
result.assert_outcomes(passed=1, warnings=1)
result.stdout.fnmatch_lines(
['*is asynchronous and explicitly requests the "event_loop" fixture*']
Expand All @@ -114,7 +114,7 @@ async def test_uses_fixture(emits_warning):
"""
)
)
result = pytester.runpytest("--asyncio-mode=strict", "-W default")
result = pytester.runpytest_subprocess("--asyncio-mode=strict", "-W default")
result.assert_outcomes(passed=1, warnings=1)
result.stdout.fnmatch_lines(
['*is asynchronous and explicitly requests the "event_loop" fixture*']
Expand Down