Skip to content

Commit ba9ab3e

Browse files
committed
Skip ID for single factories
1 parent dbe0009 commit ba9ab3e

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

pytest_asyncio/plugin.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,10 +760,12 @@ def pytest_generate_tests(metafunc: pytest.Metafunc) -> None:
760760
metafunc.fixturenames.append(_asyncio_loop_factory.__name__)
761761
default_loop_scope = _get_default_test_loop_scope(metafunc.config)
762762
loop_scope = marker_loop_scope or default_loop_scope
763+
# pytest.HIDDEN_PARAM was added in pytest 9.0
764+
hide_id = len(effective_factories) == 1 and hasattr(pytest, "HIDDEN_PARAM")
763765
metafunc.parametrize(
764766
_asyncio_loop_factory.__name__,
765767
effective_factories.values(),
766-
ids=effective_factories.keys(),
768+
ids=(pytest.HIDDEN_PARAM,) if hide_id else effective_factories.keys(),
767769
indirect=True,
768770
scope=loop_scope,
769771
)

tests/test_loop_factory_parametrization.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,35 @@
66
from pytest import Pytester
77

88

9+
@pytest.mark.skipif(
10+
not hasattr(pytest, "HIDDEN_PARAM"),
11+
reason="pytest.HIDDEN_PARAM requires pytest 9.0+",
12+
)
13+
def test_single_factory_does_not_add_suffix_to_test_name(
14+
pytester: Pytester,
15+
) -> None:
16+
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
17+
pytester.makeconftest(dedent("""\
18+
import asyncio
19+
20+
def pytest_asyncio_loop_factories(config, item):
21+
return {"asyncio": asyncio.new_event_loop}
22+
"""))
23+
pytester.makepyfile(dedent("""\
24+
import pytest
25+
26+
pytest_plugins = "pytest_asyncio"
27+
28+
@pytest.mark.asyncio
29+
async def test_example():
30+
assert True
31+
"""))
32+
result = pytester.runpytest("--asyncio-mode=strict", "--collect-only", "-q")
33+
result.stdout.fnmatch_lines(
34+
["test_single_factory_does_not_add_suffix_to_test_name.py::test_example"]
35+
)
36+
37+
938
def test_named_hook_factories_apply_to_async_tests(pytester: Pytester) -> None:
1039
pytester.makeini("[pytest]\nasyncio_default_fixture_loop_scope = function")
1140
pytester.makeconftest(dedent("""\

0 commit comments

Comments
 (0)