4
4
import inspect
5
5
import itertools
6
6
import os
7
- import pathlib
8
7
import sys
9
8
import types
10
9
import warnings
@@ -1408,19 +1407,18 @@ def _show_fixtures_per_test(config: Config, session: Session) -> None:
1408
1407
import _pytest .config
1409
1408
1410
1409
session .perform_collect ()
1411
- curdir = pathlib . Path .cwd ()
1410
+ curdir = Path .cwd ()
1412
1411
tw = _pytest .config .create_terminal_writer (config )
1413
1412
verbose = config .getvalue ("verbose" )
1414
1413
1415
- def get_best_relpath (func ):
1416
- return getlocation (func , curdir )
1417
-
1418
1414
def write_fixture (fixture_def : fixtures .FixtureDef [object ]) -> None :
1419
1415
argname = fixture_def .argname
1420
1416
if verbose <= 0 and argname .startswith ("_" ):
1421
1417
return
1422
1418
if verbose > 0 :
1423
- bestrel = get_best_relpath (fixture_def .func )
1419
+ bestrel = getlocation (
1420
+ fixture_def .func , relative_to = curdir , allow_escape = True
1421
+ )
1424
1422
funcargspec = f"{ argname } -- { bestrel } "
1425
1423
else :
1426
1424
funcargspec = argname
@@ -1434,13 +1432,17 @@ def write_fixture(fixture_def: fixtures.FixtureDef[object]) -> None:
1434
1432
def write_item (item : nodes .Item ) -> None :
1435
1433
# Not all items have _fixtureinfo attribute.
1436
1434
info : Optional [FuncFixtureInfo ] = getattr (item , "_fixtureinfo" , None )
1435
+ function : Optional [Callable [..., Any ]] = getattr (item , "function" , None )
1437
1436
if info is None or not info .name2fixturedefs :
1438
1437
# This test item does not use any fixtures.
1439
1438
return
1439
+ if function is None :
1440
+ return
1440
1441
tw .line ()
1441
1442
tw .sep ("-" , f"fixtures used by { item .name } " )
1442
- # TODO: Fix this type ignore.
1443
- tw .sep ("-" , "({})" .format (get_best_relpath (item .function ))) # type: ignore[attr-defined]
1443
+
1444
+ loc = getlocation (function , relative_to = curdir , allow_escape = True )
1445
+ tw .sep ("-" , f"({ loc } )" )
1444
1446
# dict key not used in loop but needed for sorting.
1445
1447
for _ , fixturedefs in sorted (info .name2fixturedefs .items ()):
1446
1448
assert fixturedefs is not None
@@ -1477,7 +1479,7 @@ def _showfixtures_main(config: Config, session: Session) -> None:
1477
1479
if not fixturedefs :
1478
1480
continue
1479
1481
for fixturedef in fixturedefs :
1480
- loc = getlocation (fixturedef .func , curdir )
1482
+ loc = getlocation (fixturedef .func , relative_to = curdir , allow_escape = True )
1481
1483
if (fixturedef .argname , loc ) in seen :
1482
1484
continue
1483
1485
seen .add ((fixturedef .argname , loc ))
@@ -1503,11 +1505,11 @@ def _showfixtures_main(config: Config, session: Session) -> None:
1503
1505
continue
1504
1506
tw .write (argname , green = True )
1505
1507
if fixturedef .scope != "function" :
1506
- tw .write (" [%s scope]" % fixturedef . scope , cyan = True )
1508
+ tw .write (f " [{ fixturedef . scope } scope]" , cyan = True )
1507
1509
if verbose > 0 :
1508
- tw .write (" -- %s" % str ( bestrel ) , yellow = True )
1510
+ tw .write (f " -- { bestrel } " , yellow = True )
1509
1511
tw .write ("\n " )
1510
- loc = getlocation (fixturedef .func , curdir )
1512
+ loc = getlocation (fixturedef .func , relative_to = curdir , allow_escape = True )
1511
1513
doc = inspect .getdoc (fixturedef .func )
1512
1514
if doc :
1513
1515
write_docstring (tw , doc )
0 commit comments