Skip to content

Commit 1119c53

Browse files
author
Sylvain MARIE
committed
Improved error message when something that is not a fixture is used in unpack_fixture or fixture_union. Fixed #75
`get_fixture_name` can now receive a string.
1 parent 5880f59 commit 1119c53

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

pytest_cases/common.py

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
except ImportError:
44
from funcsigs import signature
55

6+
try:
7+
from typing import Union, Callable, Any
8+
except ImportError:
9+
pass
10+
611
from distutils.version import LooseVersion
712
from warnings import warn
813

@@ -25,14 +30,50 @@ def remove_duplicates(lst):
2530
if item not in dset and not dset.add(item)]
2631

2732

28-
def get_fixture_name(fixture_fun):
33+
def is_fixture(fixture_fun # type: Any
34+
):
35+
"""
36+
Returns True if the provided function is a fixture
37+
38+
:param fixture_fun:
39+
:return:
2940
"""
30-
Internal utility to retrieve the fixture name corresponding to the given fixture function .
41+
try:
42+
# noinspection PyStatementEffect
43+
fixture_fun._pytestfixturefunction
44+
return True
45+
except AttributeError:
46+
# not a fixture ?
47+
return False
48+
49+
50+
def assert_is_fixture(fixture_fun # type: Any
51+
):
52+
"""
53+
Raises a ValueError if the provided fixture function is not a fixture.
54+
55+
:param fixture_fun:
56+
:return:
57+
"""
58+
if not is_fixture(fixture_fun):
59+
raise ValueError("The provided fixture function does not seem to be a fixture: %s. Did you properly decorate "
60+
"it ?" % fixture_fun)
61+
62+
63+
def get_fixture_name(fixture_fun # type: Union[str, Callable]
64+
):
65+
"""
66+
Internal utility to retrieve the fixture name corresponding to the given fixture function.
3167
Indeed there is currently no pytest API to do this.
3268
69+
Note: this function can receive a string, in which case it is directly returned.
70+
3371
:param fixture_fun:
3472
:return:
3573
"""
74+
if isinstance(fixture_fun, string_types):
75+
return fixture_fun
76+
assert_is_fixture(fixture_fun)
3677
try: # pytest 3
3778
custom_fixture_name = fixture_fun._pytestfixturefunction.name
3879
except AttributeError:
@@ -62,8 +103,7 @@ def get_fixture_scope(fixture_fun):
62103
:param fixture_fun:
63104
:return:
64105
"""
65-
# try:
66-
# # pytest 3
106+
assert_is_fixture(fixture_fun)
67107
return fixture_fun._pytestfixturefunction.scope
68108
# except AttributeError:
69109
# # pytest 2

pytest_cases/main_fixtures.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,10 @@ def _unpack_fixture(caller_module, argnames, fixture):
9494
argnames_lst = get_param_argnames_as_list(argnames)
9595

9696
# possibly get the source fixture name if the fixture symbol was provided
97+
source_f_name = get_fixture_name(fixture)
9798
if not isinstance(fixture, string_types):
98-
source_f_name = get_fixture_name(fixture)
9999
scope = get_fixture_scope(fixture)
100100
else:
101-
source_f_name = fixture
102101
# we dont have a clue about the real scope, so lets use function scope
103102
scope = 'function'
104103

@@ -911,7 +910,7 @@ def _fixture_union(caller_module,
911910
f_names = []
912911
for f in fixtures:
913912
# possibly get the fixture name if the fixture symbol was provided
914-
f_names.append(get_fixture_name(f) if not isinstance(f, string_types) else f)
913+
f_names.append(get_fixture_name(f))
915914

916915
if len(f_names) < 1:
917916
raise ValueError("Empty fixture unions are not permitted")
@@ -981,7 +980,7 @@ def _fixture_product(caller_module, name, fixtures_or_values, fixture_positions,
981980
# possibly get the fixture name if the fixture symbol was provided
982981
f = fixtures_or_values[f_pos]
983982
# and remember the position in the tuple
984-
f_names[f_pos] = get_fixture_name(f) if not isinstance(f, string_types) else f
983+
f_names[f_pos] = get_fixture_name(f)
985984

986985
# remove duplicates by making it an ordered set
987986
all_names = remove_duplicates((n for n in f_names if n is not None))

0 commit comments

Comments
 (0)