Skip to content

Commit

Permalink
Fix ScopeMismatch with parametrized cases (#317)
Browse files Browse the repository at this point in the history
* tests: add test for issue #311

* fix: propagate scope in ...ParamAlternative

* fix: mangle fixture name for conftest

Fixtures that are to be injected in conftest.py will be available globally. If we do not include this information in the name of the autogenerated fixtures, we may risk causing a conflict if another test/case/conftest uses parametrization on the same tests.

* fix: avoid conflict also if __init__.py exists

When conftest is in a package rather than in a mere folder, its name is "fully qualified".

Perhaps there would be no need to actually add the scope in this case, but better safe than sorry.

* Do not pass on None as scope

Make sure that the default scope always is "function" so as to avoid issues with pytest <= 6, which 'translates' the scope string kwarg into an index from a list. The list does not contain None.

* More explicit None scope replacement

As suggested by @smarie

* Add note and example for conftest qualname

* Test correctness of scopes

As suggested by @smarie

* Add changelog for #317

* Update docs/changelog.md

---------

Co-authored-by: Sylvain Marié <sylvain.marie@schneider-electric.com>
  • Loading branch information
michele-riva and smarie authored Nov 10, 2023
1 parent c0f0a43 commit ac7f3c3
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 3 deletions.
8 changes: 8 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

### 3.8.1 - bugfixes

- Fixed `ScopeMismatch` with parametrized cases in non-trivial test
trees. `scope` is now correctly handled for (i) `fixture` cases, and
(ii) fixtures defined in `conftest.py` files at any depth. Fixes
[#311](https://github.com/smarie/python-pytest-cases/issues/311). PR
[#317](https://github.com/smarie/python-pytest-cases/pull/317) by [@michele-riva](https://github.com/michele-riva).

### 3.8.0 - async, generators and strict-markers

- `@fixture` and `@parametrize` are now async and generator aware. Fixes
Expand Down
15 changes: 15 additions & 0 deletions src/pytest_cases/case_parametrizer_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,21 @@ def get_or_create_case_fixture(case_id, # type: str
# Fix the problem with "case_foo(foo)" leading to the generated fixture having the same name
existing_fixture_names += fixtures_in_cases_module

# If the fixture will be injected in a conftest, make sure its name
# is unique. Include also its scope to avoid conflicts. See #311.
# Notice target_host.__name__ may just be 'conftest' when tests
# are simple modules or a more complicated fully qualified name
# when the test suite is a package (i.e., with __init__.py). For
# example, target_host.__name__ would be 'tests.conftest' when
# executing tests from within 'base' in the following tree:
# base/
# tests/
# __init__.py
# conftest.py
if 'conftest' in target_host.__name__:
extra = target_host.__name__.replace('.', '_')
case_id = extra + '_' + case_id + '_with_scope_' + scope

def name_changer(name, i):
return name + '_' * i

Expand Down
10 changes: 7 additions & 3 deletions src/pytest_cases/fixture_parametrize_plus.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ def create(cls,
i, # type: int
argvalue, # type: Any
id, # type: Union[str, Callable]
scope=None, # type: str
hook=None, # type: Callable
debug=False # type: bool
):
Expand Down Expand Up @@ -362,7 +363,7 @@ def create(cls,

# Create the fixture. IMPORTANT auto_simplify=True : we create a NON-parametrized fixture.
_create_param_fixture(new_fixture_host, argname=p_fix_name, argvalues=(argvalue,),
hook=hook, auto_simplify=True, debug=debug)
scope=scope, hook=hook, auto_simplify=True, debug=debug)

# Create the alternative
argvals = (argvalue,) if nb_params == 1 else argvalue
Expand Down Expand Up @@ -426,6 +427,7 @@ def create(cls,
to_i, # type: int
argvalues, # type: Any
ids, # type: Union[Sequence[str], Callable]
scope="function", # type: str
hook=None, # type: Callable
debug=False # type: bool
):
Expand Down Expand Up @@ -493,8 +495,8 @@ def create(cls,
else:
ids = [mini_idvalset(argnames, vals, i) for i, vals in enumerate(unmarked_argvalues)]

_create_param_fixture(new_fixture_host, argname=p_fix_name, argvalues=argvalues, ids=ids, hook=hook,
debug=debug)
_create_param_fixture(new_fixture_host, argname=p_fix_name, argvalues=argvalues, ids=ids,
scope=scope, hook=hook, debug=debug)

# Create the corresponding alternative
# note: as opposed to SingleParamAlternative, no need to move the custom id/marks to the ParamAlternative
Expand Down Expand Up @@ -874,6 +876,7 @@ def _create_params_alt(fh, test_func, union_name, from_i, to_i, hook): # noqa
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,
scope="function" if scope is None else scope,
hook=hook, debug=debug)
else:
# If an explicit list of ids was provided, slice it. Otherwise the provided callable will be used later
Expand All @@ -882,6 +885,7 @@ def _create_params_alt(fh, test_func, union_name, from_i, to_i, hook): # noqa
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,
scope="function" if scope is None else scope,
hook=hook, debug=debug)


Expand Down
6 changes: 6 additions & 0 deletions tests/cases/issues/issue_311/cases.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from pytest_cases import parametrize


@parametrize(arg=(1,))
def case_parametrized(arg):
return arg
9 changes: 9 additions & 0 deletions tests/cases/issues/issue_311/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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]

session_scoped = scope_mismatch
7 changes: 7 additions & 0 deletions tests/cases/issues/issue_311/test_issue_311/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from pytest_cases import fixture, parametrize_with_cases


@fixture(scope='class')
@parametrize_with_cases('arg', cases='cases', scope='class')
def class_scoped(arg):
return [arg]
49 changes: 49 additions & 0 deletions tests/cases/issues/issue_311/test_issue_311/test_issue_311.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
from pytest_cases import fixture, parametrize_with_cases


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

# This tests would fail with a ScopeMismatch
# during collection before #317
def test_scope_mismatch_collection(scope_mismatch):
assert scope_mismatch == [1]

def test_scopes(session_scoped, function_scoped, class_scoped):
session_scoped.append(2)
function_scoped.append(2)
class_scoped.append(2)
assert session_scoped == [1, 2]
assert function_scoped == [1, 2]
assert class_scoped == [1, 2]

def test_scopes_again(session_scoped, function_scoped, class_scoped):
session_scoped.append(3)
function_scoped.append(3)
class_scoped.append(3)
assert session_scoped == [1, 2, 3]
assert function_scoped == [1, 3]
assert class_scoped == [1, 3]


class TestScopesInClass:

def test_scopes_in_class(self, session_scoped,
function_scoped, class_scoped):
session_scoped.append(4)
function_scoped.append(4)
class_scoped.append(4)
assert session_scoped == [1, 2, 3, 4]
assert function_scoped == [1, 4]
assert class_scoped == [1, 4]

def test_scopes_in_class_again(self, session_scoped,
function_scoped, class_scoped):
session_scoped.append(5)
function_scoped.append(5)
class_scoped.append(5)
assert session_scoped == [1, 2, 3, 4, 5]
assert function_scoped == [1, 5]
assert class_scoped == [1, 4, 5]

0 comments on commit ac7f3c3

Please sign in to comment.