Skip to content

ScopeMismatch with parametrized cases #311

Description

@michele-riva

Hi, here's the last one. A bit hairier.

Example to reproduce:

cases.py

from pytest_cases import parametrize

@parametrize(arg=(1,))
def case_parametrized(arg):
    return arg

conftest.py

from pytest_cases import fixture, parametrize_with_cases

@fixture(scope='session')
@parametrize_with_cases('arg', cases='cases', scope='session')
def scope_mismatch(arg):
    return arg

test_scope_mismatch.py

from pytest_cases import fixture, parametrize_with_cases

@fixture
@parametrize_with_cases('arg', cases='cases')
def function_scoped(arg):
    return arg

def test_scope_mismatch(scope_mismatch):
    assert scope_mismatch == 1

Gives:

____________________________________________ ERROR at setup of test_scope_mismatch[parametrized-arg=1] _______________________________
ScopeMismatch: You tried to access the function scoped fixture parametrized with a session scoped request object, involved factories:
conftest.py:6:  def scope_mismatch(scope_mismatch_arg, request)
<makefun-gen-5>:1:  def scope_mismatch_arg(parametrized, request)
cases.py:6:  def case_parametrized(request)

Notice that this does not happen if the two fixtures are defined in the same module (either conftest.py or test_scope_mismatch.py). Also, this may be obscured by #309 or #310 if using AUTO cases.

It looks like the core of the issue is that the scope keyword argument is not passed to the .create calls here

def _create_params_alt(fh, test_func, union_name, from_i, to_i, hook): # noqa
""" Routine that will be used to create a parameter fixture for argvalues between prev_i and i"""
# is this about a single value or several values ?
if to_i == from_i + 1:
i = from_i
del from_i
# If an explicit list of ids was provided, slice it. Otherwise use the provided callable
if ids is not None:
_id = ids[i] if explicit_ids_to_use else ids(argvalues[i])
else:
_id = None
return SingleParamAlternative.create(new_fixture_host=fh, test_func=test_func,
param_union_name=union_name, argnames=argnames, i=i,
argvalue=marked_argvalues[i], id=_id,
hook=hook, debug=debug)
else:
# If an explicit list of ids was provided, slice it. Otherwise the provided callable will be used later
_ids = ids[from_i:to_i] if explicit_ids_to_use else ids
return MultiParamAlternative.create(new_fixture_host=fh, test_func=test_func,
param_union_name=union_name, argnames=argnames, from_i=from_i,
to_i=to_i, argvalues=marked_argvalues[from_i:to_i], ids=_ids,
hook=hook, debug=debug)

In principle I can draft a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions