@@ -2475,25 +2475,18 @@ def finish(self):
2475
2475
func = self ._finalizer .pop ()
2476
2476
func ()
2477
2477
finally :
2478
+ ihook = self ._fixturemanager .session .ihook
2479
+ ihook .pytest_fixture_post_finalizer (fixturedef = self )
2478
2480
# even if finalization fails, we invalidate
2479
2481
# the cached fixture value
2480
2482
if hasattr (self , "cached_result" ):
2481
- config = self ._fixturemanager .config
2482
- if config .option .setuponly or config .option .setupplan :
2483
- self ._show_fixture_action ('TEARDOWN' )
2484
- if hasattr (self , "cached_param" ):
2485
- del self .cached_param
2486
2483
del self .cached_result
2487
2484
2488
2485
def execute (self , request ):
2489
2486
# get required arguments and register our own finish()
2490
2487
# with their finalization
2491
- kwargs = {}
2492
2488
for argname in self .argnames :
2493
2489
fixturedef = request ._get_active_fixturedef (argname )
2494
- result , arg_cache_key , exc = fixturedef .cached_result
2495
- request ._check_scope (argname , request .scope , fixturedef .scope )
2496
- kwargs [argname ] = result
2497
2490
if argname != "request" :
2498
2491
fixturedef .addfinalizer (self .finish )
2499
2492
@@ -2511,76 +2504,44 @@ def execute(self, request):
2511
2504
self .finish ()
2512
2505
assert not hasattr (self , "cached_result" )
2513
2506
2514
- fixturefunc = self .func
2515
-
2516
- if self .unittest :
2517
- if request .instance is not None :
2518
- # bind the unbound method to the TestCase instance
2519
- fixturefunc = self .func .__get__ (request .instance )
2520
- else :
2521
- # the fixture function needs to be bound to the actual
2522
- # request.instance so that code working with "self" behaves
2523
- # as expected.
2524
- if request .instance is not None :
2525
- fixturefunc = getimfunc (self .func )
2526
- if fixturefunc != self .func :
2527
- fixturefunc = fixturefunc .__get__ (request .instance )
2528
-
2529
- try :
2530
- config = request .config
2531
- if config .option .setupplan :
2532
- result = None
2533
- else :
2534
- result = call_fixture_func (fixturefunc , request , kwargs )
2535
- if config .option .setuponly or config .option .setupplan :
2536
- if hasattr (request , 'param' ):
2537
- # Save the fixture parameter so ._show_fixture_action() can
2538
- # display it now and during the teardown (in .finish()).
2539
- if self .ids :
2540
- if callable (self .ids ):
2541
- self .cached_param = self .ids (request .param )
2542
- else :
2543
- self .cached_param = self .ids [request .param_index ]
2544
- else :
2545
- self .cached_param = request .param
2546
- self ._show_fixture_action ('SETUP' )
2547
- except Exception :
2548
- self .cached_result = (None , my_cache_key , sys .exc_info ())
2549
- raise
2550
- self .cached_result = (result , my_cache_key , None )
2551
- return result
2552
-
2553
- def _show_fixture_action (self , what ):
2554
- config = self ._fixturemanager .config
2555
- capman = config .pluginmanager .getplugin ('capturemanager' )
2556
- if capman :
2557
- out , err = capman .suspendcapture ()
2558
-
2559
- tw = config .get_terminal_writer ()
2560
- tw .line ()
2561
- tw .write (' ' * 2 * self .scopenum )
2562
- tw .write ('{step} {scope} {fixture}' .format (
2563
- step = what .ljust (8 ), # align the output to TEARDOWN
2564
- scope = self .scope [0 ].upper (),
2565
- fixture = self .argname ))
2566
-
2567
- if what == 'SETUP' :
2568
- deps = sorted (arg for arg in self .argnames if arg != 'request' )
2569
- if deps :
2570
- tw .write (' (fixtures used: {0})' .format (', ' .join (deps )))
2571
-
2572
- if hasattr (self , 'cached_param' ):
2573
- tw .write ('[{0}]' .format (self .cached_param ))
2574
-
2575
- if capman :
2576
- capman .resumecapture ()
2577
- sys .stdout .write (out )
2578
- sys .stderr .write (err )
2507
+ ihook = self ._fixturemanager .session .ihook
2508
+ ihook .pytest_fixture_setup (fixturedef = self , request = request )
2579
2509
2580
2510
def __repr__ (self ):
2581
2511
return ("<FixtureDef name=%r scope=%r baseid=%r >" %
2582
2512
(self .argname , self .scope , self .baseid ))
2583
2513
2514
+ def pytest_fixture_setup (fixturedef , request ):
2515
+ """ Execution of fixture setup. """
2516
+ kwargs = {}
2517
+ for argname in fixturedef .argnames :
2518
+ fixdef = request ._get_active_fixturedef (argname )
2519
+ result , arg_cache_key , exc = fixdef .cached_result
2520
+ request ._check_scope (argname , request .scope , fixdef .scope )
2521
+ kwargs [argname ] = result
2522
+
2523
+ fixturefunc = fixturedef .func
2524
+ if fixturedef .unittest :
2525
+ if request .instance is not None :
2526
+ # bind the unbound method to the TestCase instance
2527
+ fixturefunc = fixturedef .func .__get__ (request .instance )
2528
+ else :
2529
+ # the fixture function needs to be bound to the actual
2530
+ # request.instance so that code working with "fixturedef" behaves
2531
+ # as expected.
2532
+ if request .instance is not None :
2533
+ fixturefunc = getimfunc (fixturedef .func )
2534
+ if fixturefunc != fixturedef .func :
2535
+ fixturefunc = fixturefunc .__get__ (request .instance )
2536
+ my_cache_key = request .param_index
2537
+ try :
2538
+ result = call_fixture_func (fixturefunc , request , kwargs )
2539
+ except Exception :
2540
+ fixturedef .cached_result = (None , my_cache_key , sys .exc_info ())
2541
+ raise
2542
+ fixturedef .cached_result = (result , my_cache_key , None )
2543
+ return result
2544
+
2584
2545
def num_mock_patch_args (function ):
2585
2546
""" return number of arguments used up by mock arguments (if any) """
2586
2547
patchings = getattr (function , "patchings" , None )
0 commit comments