1515 "Hey there, I believe the following fixture(s) are not being used:"
1616)
1717UNUSED_FIXTURES_NOT_FOUND_HEADLINE = "Cool, every declared fixture is being used."
18+ USED_FIXTURES_FOUND_HEADLINE = (
19+ "Hey there, I believe the following fixture(s) are being used:"
20+ )
21+ USED_FIXTURES_NOT_FOUND_HEADLINE = "We could not find any fixtures being used"
1822
1923EXIT_CODE_ERROR = 11
2024EXIT_CODE_SUCCESS = 0
2327
2428CachedFixture = namedtuple ("CachedFixture" , "fixturedef, relpath, result" )
2529
30+ UsedFixture = namedtuple ('UsedFixture' , 'relpath, argname, fixturedef' )
31+
2632
2733def pytest_addoption (parser ):
2834 group = parser .getgroup ("deadfixtures" )
@@ -40,6 +46,13 @@ def pytest_addoption(parser):
4046 default = False ,
4147 help = "Show duplicated fixtures" ,
4248 )
49+ group .addoption (
50+ '--used-fixtures' ,
51+ action = 'store_true' ,
52+ dest = 'usedfixtures' ,
53+ default = False ,
54+ help = 'Show fixtures being used' ,
55+ )
4356
4457
4558def pytest_cmdline_main (config ):
@@ -49,6 +62,10 @@ def pytest_cmdline_main(config):
4962 if _show_dead_fixtures (config ):
5063 return EXIT_CODE_ERROR
5164 return EXIT_CODE_SUCCESS
65+ elif config .option .usedfixtures :
66+ if _show_used_fixtures (config ):
67+ return EXIT_CODE_ERROR
68+ return EXIT_CODE_SUCCESS
5269
5370
5471def _show_dead_fixtures (config ):
@@ -57,6 +74,12 @@ def _show_dead_fixtures(config):
5774 return wrap_session (config , show_dead_fixtures )
5875
5976
77+ def _show_used_fixtures (config ):
78+ from _pytest .main import wrap_session
79+
80+ return wrap_session (config , show_used_fixtures )
81+
82+
6083def get_best_relpath (func , curdir ):
6184 loc = getlocation (func , curdir )
6285 return curdir .bestrelpath (loc )
@@ -98,8 +121,10 @@ def get_fixtures(session):
98121 return available
99122
100123
101- def get_used_fixturesdefs (session ):
102- fixturesdefs = []
124+ def get_used_fixtures (session ):
125+ used = []
126+ seen = set ()
127+ curdir = py .path .local ()
103128 for test_function in session .items :
104129 try :
105130 info = test_function ._fixtureinfo
@@ -113,8 +138,27 @@ def get_used_fixturesdefs(session):
113138 for _ , fixturedefs in sorted (info .name2fixturedefs .items ()):
114139 if fixturedefs is None :
115140 continue
116- fixturesdefs .append (fixturedefs [- 1 ])
117- return fixturesdefs
141+
142+ for fixturedef in fixturedefs :
143+ loc = getlocation (fixturedef .func , curdir )
144+ if (fixturedef .argname , loc ) in seen :
145+ continue
146+
147+ seen .add ((fixturedef .argname , loc ))
148+
149+ module = fixturedef .func .__module__
150+
151+ if (
152+ not module .startswith ("_pytest." )
153+ and not module .startswith ("pytest_" )
154+ and not ('site-packages' in loc )
155+ ):
156+ used .append (
157+ UsedFixture (
158+ curdir .bestrelpath (loc ), fixturedef .argname , fixturedef
159+ )
160+ )
161+ return used
118162
119163
120164def write_docstring (tw , doc ):
@@ -198,13 +242,13 @@ def show_dead_fixtures(config, session):
198242 tw = _pytest .config .create_terminal_writer (config )
199243 show_fixture_doc = config .getvalue ("show_fixture_doc" )
200244
201- used_fixtures = get_used_fixturesdefs (session )
245+ used_fixturedefs = [ f . fixturedef for f in get_used_fixtures (session )]
202246 available_fixtures = get_fixtures (session )
203247
204248 unused_fixtures = [
205249 fixture
206250 for fixture in available_fixtures
207- if fixture .fixturedef not in used_fixtures
251+ if fixture .fixturedef not in used_fixturedefs
208252 ]
209253
210254 tw .line ()
@@ -214,3 +258,19 @@ def show_dead_fixtures(config, session):
214258 else :
215259 tw .line (UNUSED_FIXTURES_NOT_FOUND_HEADLINE , green = True )
216260 return unused_fixtures
261+
262+
263+ def show_used_fixtures (config , session ):
264+ session .perform_collect ()
265+ tw = _pytest .config .create_terminal_writer (config )
266+ verbose = config .getvalue ('verbose' )
267+
268+ used_fixtures = get_used_fixtures (session )
269+
270+ tw .line ()
271+ if used_fixtures :
272+ tw .line (USED_FIXTURES_FOUND_HEADLINE , green = True )
273+ write_fixtures (tw , used_fixtures , verbose )
274+ else :
275+ tw .line (USED_FIXTURES_NOT_FOUND_HEADLINE , red = True )
276+ return used_fixtures
0 commit comments