Skip to content

Commit

Permalink
#408: Add ability to print live output messages
Browse files Browse the repository at this point in the history
  • Loading branch information
bradwilson committed May 10, 2024
1 parent 06087e3 commit e205244
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
<NerdbankGitVersioningVersion>3.6.133</NerdbankGitVersioningVersion>
<NSubstituteVersion>5.1.0</NSubstituteVersion>
<TunnelVisionLabsReferenceAssemblyAnnotatorVersion>1.0.0-alpha.160</TunnelVisionLabsReferenceAssemblyAnnotatorVersion>
<XunitAnalyzersVersion>1.13.0</XunitAnalyzersVersion>
<XunitVersion>2.8.0</XunitVersion>
<XunitAnalyzersVersion>1.14.0-pre.3</XunitAnalyzersVersion>
<XunitVersion>2.8.1-pre.10</XunitVersion>
</PropertyGroup>

</Project>
12 changes: 11 additions & 1 deletion src/xunit.runner.visualstudio/Sinks/VsExecutionSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public VsExecutionSink(
ITestExecutionRecorder recorder,
LoggerHelper logger,
Dictionary<string, TestCase> testCasesMap,
Func<bool> cancelledThunk)
Func<bool> cancelledThunk,
bool showLiveOutput)
{
this.innerSink = innerSink;
this.recorder = recorder;
Expand All @@ -45,6 +46,9 @@ public VsExecutionSink(
Execution.TestMethodCleanupFailureEvent += HandleTestMethodCleanupFailure;
Execution.TestPassedEvent += HandleTestPassed;
Execution.TestSkippedEvent += HandleTestSkipped;

if (showLiveOutput)
Execution.TestOutputEvent += HandleTestOutput;
}

public ExecutionSummary ExecutionSummary { get; private set; }
Expand Down Expand Up @@ -127,6 +131,12 @@ void HandleTestFailed(MessageHandlerArgs<ITestFailed> args)
HandleCancellation(args);
}

void HandleTestOutput(MessageHandlerArgs<ITestOutput> args)
{
var testOutput = args.Message;
logger.Log(" {0} [OUTPUT] {1}", testOutput.Test.DisplayName, testOutput.Output.TrimEnd());
}

void HandleTestPassed(MessageHandlerArgs<ITestPassed> args)
{
var testPassed = args.Message;
Expand Down
8 changes: 8 additions & 0 deletions src/xunit.runner.visualstudio/Utility/RunSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class RunSettings
public bool? PreEnumerateTheories { get; set; }
public string? ReporterSwitch { get; set; }
public bool? ShadowCopy { get; set; }
public bool? ShowLiveOutput { get; set; }
public bool? StopOnFail { get; set; }
public string? TargetFrameworkVersion { get; set; }

Expand Down Expand Up @@ -54,6 +55,8 @@ public void CopyTo(TestAssemblyConfiguration configuration)
configuration.PreEnumerateTheories = PreEnumerateTheories;
if (ShadowCopy.HasValue)
configuration.ShadowCopy = ShadowCopy;
if (ShowLiveOutput.HasValue)
configuration.ShowLiveOutput = ShowLiveOutput;
if (StopOnFail.HasValue)
configuration.StopOnFail = StopOnFail;
}
Expand Down Expand Up @@ -154,6 +157,10 @@ public static RunSettings Parse(string? settingsXml)
if (bool.TryParse(shadowCopyString, out var shadowCopy))
result.ShadowCopy = shadowCopy;

var showLiveOutputString = xunitElement.Element(Constants.Xunit.ShowLiveOutput)?.Value;
if (bool.TryParse(showLiveOutputString, out var showLiveOutput))
result.ShowLiveOutput = showLiveOutput;

var stopOnFailString = xunitElement.Element(Constants.Xunit.StopOnFail)?.Value;
if (bool.TryParse(stopOnFailString, out var stopOnFail))
result.StopOnFail = stopOnFail;
Expand Down Expand Up @@ -243,6 +250,7 @@ public static class Xunit
public const string PreEnumerateTheories = "PreEnumerateTheories";
public const string ReporterSwitch = "ReporterSwitch";
public const string ShadowCopy = "ShadowCopy";
public const string ShowLiveOutput = "ShowLiveOutput";
public const string StopOnFail = "StopOnFail";
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/xunit.runner.visualstudio/VsTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ void RunTestsInAssembly(

reporterMessageHandler.OnMessage(new TestAssemblyExecutionStarting(runInfo.Assembly, executionOptions));

using var vsExecutionSink = new VsExecutionSink(reporterMessageHandler, frameworkHandle, logger, testCasesMap, () => cancelled);
using var vsExecutionSink = new VsExecutionSink(reporterMessageHandler, frameworkHandle, logger, testCasesMap, () => cancelled, executionOptions.GetShowLiveOutputOrDefault());
var executionSinkOptions = new ExecutionSinkOptions
{
DiagnosticMessageSink = diagnosticsSinkRemote,
Expand Down
4 changes: 4 additions & 0 deletions test/test.xunit.runner.visualstudio/RunSettingsTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using Xunit;
using Xunit.Abstractions;
using Xunit.Runner.VisualStudio;

public class RunSettingsTests
Expand All @@ -23,6 +24,7 @@ void AssertDefaultValues(RunSettings runSettings)
Assert.Null(runSettings.PreEnumerateTheories);
Assert.Null(runSettings.ReporterSwitch);
Assert.Null(runSettings.ShadowCopy);
Assert.Null(runSettings.ShowLiveOutput);
Assert.Null(runSettings.StopOnFail);
Assert.Null(runSettings.TargetFrameworkVersion);
}
Expand Down Expand Up @@ -123,6 +125,7 @@ public void RunSettingsHelperShouldReadBooleanValuesCorrectly(bool testValue)
<ParallelizeTestCollections>{testValueString}</ParallelizeTestCollections>
<PreEnumerateTheories>{testValueString}</PreEnumerateTheories>
<ShadowCopy>{testValueString}</ShadowCopy>
<ShowLiveOutput>{testValueString}</ShowLiveOutput>
<StopOnFail>{testValueString}</StopOnFail>
</xUnit>
</RunSettings>";
Expand All @@ -140,6 +143,7 @@ public void RunSettingsHelperShouldReadBooleanValuesCorrectly(bool testValue)
Assert.Equal(testValue, runSettings.ParallelizeTestCollections);
Assert.Equal(testValue, runSettings.PreEnumerateTheories);
Assert.Equal(testValue, runSettings.ShadowCopy);
Assert.Equal(testValue, runSettings.ShowLiveOutput);
Assert.Equal(testValue, runSettings.StopOnFail);
}

Expand Down

0 comments on commit e205244

Please sign in to comment.