Skip to content
Open
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
21 changes: 18 additions & 3 deletions launch_pytest/launch_pytest/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,21 @@ def get_launch_test_fixturename(item):
return None if fixture is None else fixture.__name__


def get_launch_test_fixture_scope(fixture):
"""Return launch fixture scope for multiple pytest fixture representations."""
# Pytest < 8.4 decorates fixtures in-place and stores metadata in
# `_pytestfixturefunction`; pytest >= 8.4 returns a fixture object with
# `_fixture_function_marker`.
fixture_marker = getattr(fixture, '_pytestfixturefunction', None)
if fixture_marker is None:
fixture_marker = getattr(fixture, '_fixture_function_marker', None)
if fixture_marker is None:
raise AttributeError(
f'Unable to retrieve fixture scope from fixture {fixture!r}.'
)
return fixture_marker.scope


def is_valid_test_item(obj):
"""Return true if obj is a valid launch test item."""
return (
Expand Down Expand Up @@ -236,7 +251,7 @@ def pytest_pycollect_makeitem(collector, name, obj):
return [item]
fixture = get_launch_test_fixture(item)
fixturename = fixture.__name__
scope = fixture._pytestfixturefunction.scope
scope = get_launch_test_fixture_scope(fixture)
is_shutdown = has_shutdown_kwarg(item)
items = generate_test_items(
collector, name, obj, fixturename, is_shutdown=is_shutdown, needs_renaming=False)
Expand Down Expand Up @@ -264,7 +279,7 @@ def is_same_launch_test_fixture(left_item, right_item):
return False
if lfn is not rfn:
return False
if lfn._pytestfixturefunction.scope == 'function':
if get_launch_test_fixture_scope(lfn) == 'function':
return False
name = lfn.__name__

Expand Down Expand Up @@ -331,7 +346,7 @@ def pytest_pyfunc_call(pyfuncitem):
return
shutdown_test = is_shutdown_test(pyfuncitem)
fixture = get_launch_test_fixture(pyfuncitem)
scope = fixture._pytestfixturefunction.scope
scope = get_launch_test_fixture_scope(fixture)
event_loop = pyfuncitem.funcargs['event_loop']
ls = pyfuncitem.funcargs['launch_service']
auto_shutdown = fixture._launch_pytest_fixture_options['auto_shutdown']
Expand Down
15 changes: 15 additions & 0 deletions launch_pytest/test/launch_pytest/test_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@

from pathlib import Path
import shutil
from types import SimpleNamespace

from launch_pytest.plugin import get_launch_test_fixture_scope


def test_launch_fixture_is_not_a_launch_description(testdir):
Expand Down Expand Up @@ -330,3 +333,15 @@ def test_examples(testdir):
shutil.copytree(examples_dir / 'executables', Path(testdir.tmpdir) / 'executables')
result = testdir.runpytest()
result.assert_outcomes(passed=22)


def test_get_launch_test_fixture_scope_with_new_and_old_pytest_repr():
old_pytest_fixture = SimpleNamespace(
_pytestfixturefunction=SimpleNamespace(scope='module')
)
new_pytest_fixture = SimpleNamespace(
_fixture_function_marker=SimpleNamespace(scope='class')
)

assert get_launch_test_fixture_scope(old_pytest_fixture) == 'module'
assert get_launch_test_fixture_scope(new_pytest_fixture) == 'class'