33except ImportError :
44 from funcsigs import signature
55
6+ try :
7+ from typing import Union , Callable , Any
8+ except ImportError :
9+ pass
10+
611from distutils .version import LooseVersion
712from 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
0 commit comments