30
30
getfuncargnames ,
31
31
safe_getattr ,
32
32
FuncargnamesCompatAttr ,
33
+ get_real_method ,
33
34
)
34
35
from _pytest .deprecated import FIXTURE_FUNCTION_CALL , RemovedInPytest4Warning
35
36
from _pytest .outcomes import fail , TEST_OUTCOME
@@ -931,13 +932,6 @@ def pytest_fixture_setup(fixturedef, request):
931
932
request ._check_scope (argname , request .scope , fixdef .scope )
932
933
kwargs [argname ] = result
933
934
934
- # if function has been defined with @pytest.fixture, we want to
935
- # pass the special __being_called_by_pytest parameter so we don't raise a warning
936
- # this is an ugly hack, see #3720 for an opportunity to improve this
937
- defined_using_fixture_decorator = hasattr (fixturedef .func , "_pytestfixturefunction" )
938
- if defined_using_fixture_decorator :
939
- kwargs ["__being_called_by_pytest" ] = True
940
-
941
935
fixturefunc = resolve_fixture_function (fixturedef , request )
942
936
my_cache_key = request .param_index
943
937
try :
@@ -973,9 +967,7 @@ def wrap_function_to_warning_if_called_directly(function, fixture_marker):
973
967
@functools .wraps (function )
974
968
def result (* args , ** kwargs ):
975
969
__tracebackhide__ = True
976
- __being_called_by_pytest = kwargs .pop ("__being_called_by_pytest" , False )
977
- if not __being_called_by_pytest :
978
- warnings .warn (warning , stacklevel = 3 )
970
+ warnings .warn (warning , stacklevel = 3 )
979
971
for x in function (* args , ** kwargs ):
980
972
yield x
981
973
@@ -984,9 +976,7 @@ def result(*args, **kwargs):
984
976
@functools .wraps (function )
985
977
def result (* args , ** kwargs ):
986
978
__tracebackhide__ = True
987
- __being_called_by_pytest = kwargs .pop ("__being_called_by_pytest" , False )
988
- if not __being_called_by_pytest :
989
- warnings .warn (warning , stacklevel = 3 )
979
+ warnings .warn (warning , stacklevel = 3 )
990
980
return function (* args , ** kwargs )
991
981
992
982
if six .PY2 :
@@ -1279,9 +1269,9 @@ def parsefactories(self, node_or_obj, nodeid=NOTSET, unittest=False):
1279
1269
# The attribute can be an arbitrary descriptor, so the attribute
1280
1270
# access below can raise. safe_getatt() ignores such exceptions.
1281
1271
obj = safe_getattr (holderobj , name , None )
1272
+ marker = getfixturemarker (obj )
1282
1273
# fixture functions have a pytest_funcarg__ prefix (pre-2.3 style)
1283
1274
# or are "@pytest.fixture" marked
1284
- marker = getfixturemarker (obj )
1285
1275
if marker is None :
1286
1276
if not name .startswith (self ._argprefix ):
1287
1277
continue
@@ -1303,6 +1293,15 @@ def parsefactories(self, node_or_obj, nodeid=NOTSET, unittest=False):
1303
1293
name = marker .name
1304
1294
assert not name .startswith (self ._argprefix ), FIXTURE_MSG .format (name )
1305
1295
1296
+ # during fixture definition we wrap the original fixture function
1297
+ # to issue a warning if called directly, so here we unwrap it in order to not emit the warning
1298
+ # when pytest itself calls the fixture function
1299
+ if six .PY2 and unittest :
1300
+ # hack on Python 2 because of the unbound methods
1301
+ obj = get_real_func (obj )
1302
+ else :
1303
+ obj = get_real_method (obj , holderobj )
1304
+
1306
1305
fixture_def = FixtureDef (
1307
1306
self ,
1308
1307
nodeid ,
0 commit comments