13
13
from pathlib import Path
14
14
from typing import Any
15
15
from typing import Callable
16
+ from typing import cast
16
17
from typing import Dict
17
18
from typing import Generator
18
19
from typing import List
@@ -545,6 +546,16 @@ def pytest_runtest_logreport(self, report: TestReport) -> None:
545
546
line = self ._locationline (rep .nodeid , * rep .location )
546
547
if not running_xdist :
547
548
self .write_ensure_prefix (line , word , ** markup )
549
+ if rep .skipped or hasattr (report , "wasxfail" ):
550
+ available_width = (
551
+ (self ._tw .fullwidth - self ._tw .width_of_current_line )
552
+ - len (" [100%]" )
553
+ - 1
554
+ )
555
+ reason = _get_raw_skip_reason (rep )
556
+ reason_ = _format_trimmed (" ({})" , reason , available_width )
557
+ if reason_ is not None :
558
+ self ._tw .write (reason_ )
548
559
if self ._show_progress_info :
549
560
self ._write_progress_information_filling_space ()
550
561
else :
@@ -1249,6 +1260,31 @@ def _get_pos(config: Config, rep: BaseReport):
1249
1260
return nodeid
1250
1261
1251
1262
1263
+ def _format_trimmed (format : str , msg : str , available_width : int ) -> Optional [str ]:
1264
+ """Format msg into format, ellipsizing it if doesn't fit in available_width.
1265
+
1266
+ Returns None if even the ellipsis can't fit.
1267
+ """
1268
+ # Only use the first line.
1269
+ i = msg .find ("\n " )
1270
+ if i != - 1 :
1271
+ msg = msg [:i ]
1272
+
1273
+ ellipsis = "..."
1274
+ format_width = wcswidth (format .format ("" ))
1275
+ if format_width + len (ellipsis ) > available_width :
1276
+ return None
1277
+
1278
+ if format_width + wcswidth (msg ) > available_width :
1279
+ available_width -= len (ellipsis )
1280
+ msg = msg [:available_width ]
1281
+ while format_width + wcswidth (msg ) > available_width :
1282
+ msg = msg [:- 1 ]
1283
+ msg += ellipsis
1284
+
1285
+ return format .format (msg )
1286
+
1287
+
1252
1288
def _get_line_with_reprcrash_message (
1253
1289
config : Config , rep : BaseReport , termwidth : int
1254
1290
) -> str :
@@ -1257,34 +1293,19 @@ def _get_line_with_reprcrash_message(
1257
1293
pos = _get_pos (config , rep )
1258
1294
1259
1295
line = f"{ verbose_word } { pos } "
1260
- len_line = wcswidth (line )
1261
- ellipsis , len_ellipsis = "..." , 3
1262
- if len_line > termwidth - len_ellipsis :
1263
- # No space for an additional message.
1264
- return line
1296
+ line_width = wcswidth (line )
1265
1297
1266
1298
try :
1267
1299
# Type ignored intentionally -- possible AttributeError expected.
1268
1300
msg = rep .longrepr .reprcrash .message # type: ignore[union-attr]
1269
1301
except AttributeError :
1270
1302
pass
1271
1303
else :
1272
- # Only use the first line.
1273
- i = msg .find ("\n " )
1274
- if i != - 1 :
1275
- msg = msg [:i ]
1276
- len_msg = wcswidth (msg )
1277
-
1278
- sep , len_sep = " - " , 3
1279
- max_len_msg = termwidth - len_line - len_sep
1280
- if max_len_msg >= len_ellipsis :
1281
- if len_msg > max_len_msg :
1282
- max_len_msg -= len_ellipsis
1283
- msg = msg [:max_len_msg ]
1284
- while wcswidth (msg ) > max_len_msg :
1285
- msg = msg [:- 1 ]
1286
- msg += ellipsis
1287
- line += sep + msg
1304
+ available_width = termwidth - line_width
1305
+ msg = _format_trimmed (" - {}" , msg , available_width )
1306
+ if msg is not None :
1307
+ line += msg
1308
+
1288
1309
return line
1289
1310
1290
1311
@@ -1361,3 +1382,22 @@ def format_session_duration(seconds: float) -> str:
1361
1382
else :
1362
1383
dt = datetime .timedelta (seconds = int (seconds ))
1363
1384
return f"{ seconds :.2f} s ({ dt } )"
1385
+
1386
+
1387
+ def _get_raw_skip_reason (report : TestReport ) -> str :
1388
+ """Get the reason string of a skip/xfail/xpass test report.
1389
+
1390
+ The string is just the part given by the user.
1391
+ """
1392
+ if hasattr (report , "wasxfail" ):
1393
+ reason = cast (str , report .wasxfail )
1394
+ if reason .startswith ("reason: " ):
1395
+ reason = reason [len ("reason: " ) :]
1396
+ return reason
1397
+ else :
1398
+ assert report .skipped
1399
+ assert isinstance (report .longrepr , tuple )
1400
+ _ , _ , reason = report .longrepr
1401
+ if reason .startswith ("Skipped: " ):
1402
+ reason = reason [len ("Skipped: " ) :]
1403
+ return reason
0 commit comments