Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import pytest 

2 

3 

4def pytest_addoption(parser): 

5 group = parser.getgroup("debugconfig") 

6 group.addoption( 

7 "--setuponly", 

8 "--setup-only", 

9 action="store_true", 

10 help="only setup fixtures, do not execute tests.", 

11 ) 

12 group.addoption( 

13 "--setupshow", 

14 "--setup-show", 

15 action="store_true", 

16 help="show setup of fixtures while executing tests.", 

17 ) 

18 

19 

20@pytest.hookimpl(hookwrapper=True) 

21def pytest_fixture_setup(fixturedef, request): 

22 yield 

23 if request.config.option.setupshow: 

24 if hasattr(request, "param"): 

25 # Save the fixture parameter so ._show_fixture_action() can 

26 # display it now and during the teardown (in .finish()). 

27 if fixturedef.ids: 

28 if callable(fixturedef.ids): 

29 fixturedef.cached_param = fixturedef.ids(request.param) 

30 else: 

31 fixturedef.cached_param = fixturedef.ids[request.param_index] 

32 else: 

33 fixturedef.cached_param = request.param 

34 _show_fixture_action(fixturedef, "SETUP") 

35 

36 

37def pytest_fixture_post_finalizer(fixturedef): 

38 if hasattr(fixturedef, "cached_result"): 

39 config = fixturedef._fixturemanager.config 

40 if config.option.setupshow: 

41 _show_fixture_action(fixturedef, "TEARDOWN") 

42 if hasattr(fixturedef, "cached_param"): 

43 del fixturedef.cached_param 

44 

45 

46def _show_fixture_action(fixturedef, msg): 

47 config = fixturedef._fixturemanager.config 

48 capman = config.pluginmanager.getplugin("capturemanager") 

49 if capman: 

50 capman.suspend_global_capture() 

51 

52 tw = config.get_terminal_writer() 

53 tw.line() 

54 tw.write(" " * 2 * fixturedef.scopenum) 

55 tw.write( 

56 "{step} {scope} {fixture}".format( 

57 step=msg.ljust(8), # align the output to TEARDOWN 

58 scope=fixturedef.scope[0].upper(), 

59 fixture=fixturedef.argname, 

60 ) 

61 ) 

62 

63 if msg == "SETUP": 

64 deps = sorted(arg for arg in fixturedef.argnames if arg != "request") 

65 if deps: 

66 tw.write(" (fixtures used: {})".format(", ".join(deps))) 

67 

68 if hasattr(fixturedef, "cached_param"): 

69 tw.write("[{}]".format(fixturedef.cached_param)) 

70 

71 if capman: 

72 capman.resume_global_capture() 

73 

74 

75@pytest.hookimpl(tryfirst=True) 

76def pytest_cmdline_main(config): 

77 if config.option.setuponly: 

78 config.option.setupshow = True