@@ -373,7 +373,7 @@ internal void TestCompleted(
373
373
TestOutcome outcome ,
374
374
TimeSpan duration ,
375
375
string ? errorMessage ,
376
- string ? errorStackTrace ,
376
+ Exception ? exception ,
377
377
string ? expected ,
378
378
string ? actual )
379
379
{
@@ -410,7 +410,7 @@ internal void TestCompleted(
410
410
outcome ,
411
411
duration ,
412
412
errorMessage ,
413
- errorStackTrace ,
413
+ exception ,
414
414
expected ,
415
415
actual ) ) ;
416
416
}
@@ -425,7 +425,7 @@ internal void TestCompleted(
425
425
TestOutcome outcome ,
426
426
TimeSpan duration ,
427
427
string ? errorMessage ,
428
- string ? errorStackTrace ,
428
+ Exception ? exception ,
429
429
string ? expected ,
430
430
string ? actual )
431
431
{
@@ -469,9 +469,10 @@ internal void TestCompleted(
469
469
470
470
terminal . AppendLine ( ) ;
471
471
472
- FormatErrorMessage ( terminal , errorMessage ) ;
472
+ FormatErrorMessage ( terminal , errorMessage , exception , outcome ) ;
473
473
FormatExpectedAndActual ( terminal , expected , actual ) ;
474
- FormatStackTrace ( terminal , errorStackTrace ) ;
474
+ FormatStackTrace ( terminal , exception ) ;
475
+ FormatInnerExceptions ( terminal , exception ) ;
475
476
}
476
477
477
478
private static void AppendAssemblyLinkTargetFrameworkAndArchitecture ( ITerminal terminal , string assembly , string ? targetFramework , string ? architecture )
@@ -495,25 +496,16 @@ private static void AppendAssemblyLinkTargetFrameworkAndArchitecture(ITerminal t
495
496
}
496
497
}
497
498
498
- private static void FormatStackTrace ( ITerminal terminal , string ? errorStackTrace )
499
+ private static void FormatStackTrace ( ITerminal terminal , Exception ? exception )
499
500
{
500
- if ( RoslynString . IsNullOrWhiteSpace ( errorStackTrace ) )
501
+ if ( exception ? . StackTrace is not { } stackTrace )
501
502
{
502
503
return ;
503
504
}
504
505
505
- terminal . SetColor ( TerminalColor . Red ) ;
506
- terminal . Append ( SingleIndentation ) ;
507
- terminal . Append ( PlatformResources . StackTrace ) ;
508
- terminal . AppendLine ( ":" ) ;
509
-
510
- if ( ! errorStackTrace . Contains ( '\n ' ) )
511
- {
512
- AppendStackFrame ( terminal , errorStackTrace ) ;
513
- return ;
514
- }
506
+ terminal . SetColor ( TerminalColor . DarkGray ) ;
515
507
516
- string [ ] lines = errorStackTrace . Split ( NewLineStrings , StringSplitOptions . None ) ;
508
+ string [ ] lines = stackTrace . Split ( NewLineStrings , StringSplitOptions . None ) ;
517
509
foreach ( string line in lines )
518
510
{
519
511
AppendStackFrame ( terminal , line ) ;
@@ -581,18 +573,57 @@ private static void FormatExpectedAndActual(ITerminal terminal, string? expected
581
573
terminal . ResetColor ( ) ;
582
574
}
583
575
584
- private static void FormatErrorMessage ( ITerminal terminal , string ? errorMessage )
576
+ private static void FormatErrorMessage ( ITerminal terminal , string ? errorMessage , Exception ? exception , TestOutcome outcome )
585
577
{
586
- if ( RoslynString . IsNullOrWhiteSpace ( errorMessage ) )
578
+ if ( RoslynString . IsNullOrWhiteSpace ( errorMessage ) && exception is null )
587
579
{
588
580
return ;
589
581
}
590
582
591
583
terminal . SetColor ( TerminalColor . Red ) ;
592
- AppendIndentedLine ( terminal , errorMessage , SingleIndentation ) ;
584
+
585
+ if ( exception is null )
586
+ {
587
+ AppendIndentedLine ( terminal , errorMessage , SingleIndentation ) ;
588
+ }
589
+ else if ( outcome == TestOutcome . Fail )
590
+ {
591
+ // For failed tests, we don't prefix the message with the exception type because it is most likely an assertion specific exception like AssertionFailedException, and we prefer to show that without the exception type to avoid additional noise.
592
+ AppendIndentedLine ( terminal , errorMessage ?? exception . Message , SingleIndentation ) ;
593
+ }
594
+ else
595
+ {
596
+ AppendIndentedLine ( terminal , $ "{ exception . GetType ( ) . FullName } : { errorMessage ?? exception . Message } ", SingleIndentation ) ;
597
+ }
598
+
593
599
terminal . ResetColor ( ) ;
594
600
}
595
601
602
+ private static void FormatInnerExceptions ( ITerminal terminal , Exception ? exception )
603
+ {
604
+ IEnumerable < Exception ? > aggregateExceptions = exception switch
605
+ {
606
+ AggregateException aggregate => aggregate . Flatten ( ) . InnerExceptions ,
607
+ _ => [ exception ? . InnerException ] ,
608
+ } ;
609
+
610
+ foreach ( Exception ? aggregate in aggregateExceptions )
611
+ {
612
+ Exception ? currentException = aggregate ;
613
+ while ( currentException is not null )
614
+ {
615
+ terminal . SetColor ( TerminalColor . Red ) ;
616
+ terminal . Append ( SingleIndentation ) ;
617
+ terminal . Append ( "--->" ) ;
618
+ FormatErrorMessage ( terminal , null , currentException , TestOutcome . Error ) ;
619
+
620
+ FormatStackTrace ( terminal , currentException ) ;
621
+
622
+ currentException = currentException . InnerException ;
623
+ }
624
+ }
625
+ }
626
+
596
627
private static void AppendIndentedLine ( ITerminal terminal , string ? message , string indent )
597
628
{
598
629
if ( RoslynString . IsNullOrWhiteSpace ( message ) )
0 commit comments