Skip to content

Commit

Permalink
Fix Microsoft.AspNetCore.Testing not writing to output in VS
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNK authored May 12, 2021
1 parent 990e060 commit b2f392b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 6 deletions.
11 changes: 6 additions & 5 deletions src/Testing/src/xunit/AspNetTestInvoker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace Microsoft.AspNetCore.Testing
{
internal class AspNetTestInvoker : XunitTestInvoker
{
private readonly TestOutputHelper _testOutputHelper;

public AspNetTestInvoker(
ITest test,
IMessageBus messageBus,
Expand All @@ -23,17 +25,16 @@ public AspNetTestInvoker(
object[] testMethodArguments,
IReadOnlyList<BeforeAfterTestAttribute> beforeAfterAttributes,
ExceptionAggregator aggregator,
CancellationTokenSource cancellationTokenSource)
CancellationTokenSource cancellationTokenSource,
TestOutputHelper testOutputHelper)
: base(test, messageBus, testClass, constructorArguments, testMethod, testMethodArguments, beforeAfterAttributes, aggregator, cancellationTokenSource)
{
_testOutputHelper = testOutputHelper;
}

protected override async Task<decimal> InvokeTestMethodAsync(object testClassInstance)
{
var output = new TestOutputHelper();
output.Initialize(MessageBus, Test);

var context = new TestContext(TestClass, ConstructorArguments, TestMethod, TestMethodArguments, output);
var context = new TestContext(TestClass, ConstructorArguments, TestMethod, TestMethodArguments, _testOutputHelper);
var lifecycleHooks = GetLifecycleHooks(testClassInstance, TestClass, TestMethod);

await Aggregator.RunAsync(async () =>
Expand Down
44 changes: 43 additions & 1 deletion src/Testing/src/xunit/AspNetTestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ namespace Microsoft.AspNetCore.Testing
{
internal class AspNetTestRunner : XunitTestRunner
{
private readonly TestOutputHelper _testOutputHelper;
private readonly bool _ownsTestOutputHelper;

public AspNetTestRunner(
ITest test,
IMessageBus messageBus,
Expand All @@ -26,6 +29,45 @@ public AspNetTestRunner(
CancellationTokenSource cancellationTokenSource)
: base(test, messageBus, testClass, constructorArguments, testMethod, testMethodArguments, skipReason, beforeAfterAttributes, aggregator, cancellationTokenSource)
{
// Prioritize using ITestOutputHelper from constructor.
if (ConstructorArguments != null)
{
foreach (var obj in ConstructorArguments)
{
_testOutputHelper = obj as TestOutputHelper;
if (_testOutputHelper != null)
{
break;
}
}
}

// No ITestOutputHelper in constructor so we'll create it ourselves.
if (_testOutputHelper == null)
{
_testOutputHelper = new TestOutputHelper();
_ownsTestOutputHelper = true;
}
}

protected override async Task<Tuple<decimal, string>> InvokeTestAsync(ExceptionAggregator aggregator)
{
if (_ownsTestOutputHelper)
{
_testOutputHelper.Initialize(MessageBus, Test);
}

var result = await base.InvokeTestAsync(aggregator);

if (_ownsTestOutputHelper)
{
// Update result with output if we created our own ITestOutputHelper.
// The string returned from this method is what VS displays as the test output.
result = new Tuple<decimal, string>(result.Item1, _testOutputHelper.Output);
_testOutputHelper.Uninitialize();
}

return result;
}

protected override async Task<decimal> InvokeTestMethodAsync(ExceptionAggregator aggregator)
Expand Down Expand Up @@ -54,7 +96,7 @@ protected override async Task<decimal> InvokeTestMethodAsync(ExceptionAggregator

private Task<decimal> InvokeTestMethodCoreAsync(ExceptionAggregator aggregator)
{
var invoker = new AspNetTestInvoker(Test, MessageBus, TestClass, ConstructorArguments, TestMethod, TestMethodArguments, BeforeAfterAttributes, aggregator, CancellationTokenSource);
var invoker = new AspNetTestInvoker(Test, MessageBus, TestClass, ConstructorArguments, TestMethod, TestMethodArguments, BeforeAfterAttributes, aggregator, CancellationTokenSource, _testOutputHelper);
return invoker.RunAsync();
}

Expand Down

0 comments on commit b2f392b

Please sign in to comment.