-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix ScopeMismatch with parametrized cases (#317)
* 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
1 parent
c0f0a43
commit ac7f3c3
Showing
7 changed files
with
101 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
49
tests/cases/issues/issue_311/test_issue_311/test_issue_311.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |