-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathTestConsoleOutputWriter.cs
53 lines (48 loc) · 1.73 KB
/
TestConsoleOutputWriter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
using System;
using System.IO;
using System.Threading;
using Xunit.Abstractions;
namespace Orleans.Indexing.Tests
{
/// <summary>
/// Workaround for Console.Write(Line) in Xunit tests
/// </summary>
// Requires app.config with the following entry for passing tests to output the strings (failing tests will always do so):
// <configuration>
// <appSettings>
// <add key = "xunit.diagnosticMessages" value="true"/>
// </appSettings>
// </configuration>
//
// This class is intended for debugging only one test at a time
internal class TestConsoleOutputWriter : IDisposable
{
ITestOutputHelper output;
private TextWriter originalConOut;
private StringWriter stringWriter;
internal TestConsoleOutputWriter(ITestOutputHelper output, string message)
{
this.output = output;
if (!string.IsNullOrEmpty(message))
{
output.WriteLine(message);
}
this.stringWriter = new StringWriter();
this.originalConOut = Console.Out;
Console.SetOut(stringWriter);
}
// using (var tw = new TestConsoleOutputWriter(...)) {
// All Console.Write* calls are collected between ctor and Dispose(), including in called assemblies.
// If "xunit.diagnosticMessages" is configured "true" as above, the are written on Dispose().
// }
public void Dispose()
{
var conOut = Interlocked.Exchange(ref this.originalConOut, null);
if (conOut != null)
{
this.output.WriteLine(this.stringWriter.ToString());
Console.SetOut(conOut);
}
}
}
}