Skip to content

Commit ff2e9fd

Browse files
author
Sylvain MARIE
committed
Fixed ValueError when @parametrize is used to parametrize a class. Also, added a more explicit TypeError when @parametrize is used to parametrize a class and at least a fixture reference is present. Fixed #215
1 parent f8d03b2 commit ff2e9fd

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

pytest_cases/fixture_parametrize_plus.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
from .common_pytest_lazy_values import is_lazy_value, get_lazy_args
3232
from .common_pytest import get_fixture_name, remove_duplicates, mini_idvalset, is_marked_parameter_value, \
3333
extract_parameterset_info, ParameterSet, cart_product_pytest, mini_idval, inject_host, \
34-
get_marked_parameter_values, resolve_ids, get_marked_parameter_id, get_marked_parameter_marks, is_fixture
34+
get_marked_parameter_values, resolve_ids, get_marked_parameter_id, get_marked_parameter_marks, is_fixture, \
35+
safe_isinstance
3536

3637
from .fixture__creation import check_name_available, CHANGE, WARN
3738
from .fixture_core1_unions import InvalidParamsList, NOT_USED, UnionFixtureAlternative, _make_fixture_union, \
@@ -788,11 +789,16 @@ def _make_ids(**args):
788789
else:
789790
# wrap the decorator to check if the test function has the parameters as arguments
790791
def _apply(test_func):
791-
s = signature(test_func)
792-
for p in argnames:
793-
if p not in s.parameters:
794-
raise ValueError("parameter '%s' not found in test function signature '%s%s'"
795-
"" % (p, test_func.__name__, s))
792+
if not safe_isinstance(test_func, type):
793+
# a Function: raise a proper error message if improper use
794+
s = signature(test_func)
795+
for p in argnames:
796+
if p not in s.parameters:
797+
raise ValueError("parameter '%s' not found in test function signature '%s%s'"
798+
"" % (p, test_func.__name__, s))
799+
else:
800+
# a Class: we cannot really perform any check.
801+
pass
796802
return _decorator(test_func)
797803

798804
return _apply, False
@@ -926,6 +932,11 @@ def parametrize_plus_decorate(test_func, fixtures_dest):
926932
test_func_name = test_func.__name__
927933

928934
# first check if the test function has the parameters as arguments
935+
if safe_isinstance(test_func, type):
936+
# a test class: not supported yet
937+
raise NotImplementedError("@parametrize can not be used to decorate a Test class when the argvalues "
938+
"contain at least one reference to a fixture.")
939+
929940
old_sig = signature(test_func)
930941
for p in argnames:
931942
if p not in old_sig.parameters:
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# This checks for issue 215 https://github.com/smarie/python-pytest-cases/issues/215
2+
import pytest
3+
4+
from pytest_cases import parametrize, fixture
5+
6+
7+
@parametrize(foo=("bar",))
8+
class TestFoo:
9+
def test_foo(self, foo):
10+
pass
11+
12+
13+
def test_synthesis(module_results_dct):
14+
assert list(module_results_dct) == [
15+
"test_foo[foo=bar]"
16+
]
17+
18+
19+
@fixture
20+
def a():
21+
return
22+
23+
24+
def test_fixture_refs_are_not_supported_when_decorating_classes():
25+
class TestCls:
26+
pass
27+
28+
with pytest.raises(NotImplementedError) as e:
29+
parametrize(foo=("bar", a))(TestCls)
30+
31+
assert str(e.value).startswith("@parametrize can not be used to decorate a Test class")

0 commit comments

Comments
 (0)