@@ -209,6 +209,13 @@ def pytest_addoption(parser):
209
209
group .addoption ('--fixtures' , '--funcargs' ,
210
210
action = "store_true" , dest = "showfixtures" , default = False ,
211
211
help = "show available fixtures, sorted by plugin appearance" )
212
+ group .addoption (
213
+ '--fixtures-per-test' ,
214
+ action = "store_true" ,
215
+ dest = "show_fixtures_per_test" ,
216
+ default = False ,
217
+ help = "show fixtures per test" ,
218
+ )
212
219
parser .addini ("usefixtures" , type = "args" , default = [],
213
220
help = "list of default fixtures to be used with this project" )
214
221
parser .addini ("python_files" , type = "args" ,
@@ -230,6 +237,9 @@ def pytest_cmdline_main(config):
230
237
if config .option .showfixtures :
231
238
showfixtures (config )
232
239
return 0
240
+ if config .option .show_fixtures_per_test :
241
+ show_fixtures_per_test (config )
242
+ return 0
233
243
234
244
235
245
def pytest_generate_tests (metafunc ):
@@ -1195,6 +1205,67 @@ def idmaker(argnames, argvalues, idfn=None, ids=None, config=None):
1195
1205
counters [testid ] += 1
1196
1206
return ids
1197
1207
1208
+
1209
+ def show_fixtures_per_test (config ):
1210
+ from _pytest .main import wrap_session
1211
+ return wrap_session (config , _show_fixtures_per_test )
1212
+
1213
+
1214
+ def _show_fixtures_per_test (config , session ):
1215
+ import _pytest .config
1216
+ session .perform_collect ()
1217
+ curdir = py .path .local ()
1218
+ tw = _pytest .config .create_terminal_writer (config )
1219
+ verbose = config .getvalue ("verbose" )
1220
+
1221
+ def get_best_rel (func ):
1222
+ loc = getlocation (func , curdir )
1223
+ return curdir .bestrelpath (loc )
1224
+
1225
+ def write_fixture (fixture_def ):
1226
+ argname = fixture_def .argname
1227
+
1228
+ if verbose <= 0 and argname .startswith ("_" ):
1229
+ return
1230
+ if verbose > 0 :
1231
+ bestrel = get_best_rel (fixture_def .func )
1232
+ funcargspec = "{} -- {}" .format (argname , bestrel )
1233
+ else :
1234
+ funcargspec = argname
1235
+ tw .line (funcargspec , green = True )
1236
+
1237
+ INDENT = ' {}'
1238
+ fixture_doc = fixture_def .func .__doc__
1239
+
1240
+ if fixture_doc :
1241
+ for line in fixture_doc .strip ().split ('\n ' ):
1242
+ tw .line (INDENT .format (line .strip ()))
1243
+ else :
1244
+ tw .line (INDENT .format ('no docstring available' ), red = True )
1245
+
1246
+ def write_item (item ):
1247
+ name2fixturedefs = item ._fixtureinfo .name2fixturedefs
1248
+
1249
+ if not name2fixturedefs :
1250
+ # The given test item does not use any fixtures
1251
+ return
1252
+ bestrel = get_best_rel (item .function )
1253
+
1254
+ tw .line ()
1255
+ tw .sep ('-' , 'fixtures used by {}' .format (item .name ))
1256
+ tw .sep ('-' , 'from {}' .format (bestrel ))
1257
+ for argname , fixture_defs in sorted (name2fixturedefs .items ()):
1258
+ assert fixture_defs is not None
1259
+ if not fixture_defs :
1260
+ continue
1261
+ # The last fixture def item in the list is expected
1262
+ # to be the one used by the test item
1263
+ write_fixture (fixture_defs [- 1 ])
1264
+
1265
+ for item in session .items :
1266
+ write_item (item )
1267
+
1268
+
1198
1269
def showfixtures (config ):
1199
1270
from _pytest .main import wrap_session
1200
1271
return wrap_session (config , _showfixtures_main )
0 commit comments