Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e59e1bf

Browse filesBrowse files
authored
Merge pull request #4709 from maettu-this/4686_ProgressTraceListener
Issue #4686: Add ProgressTraceListener
2 parents 2e436d7 + 972aa33 commit e59e1bf
Copy full SHA for e59e1bf

File tree

Expand file treeCollapse file tree

2 files changed

+211
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+211
-0
lines changed
+59Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
2+
3+
using System;
4+
using System.Diagnostics;
5+
6+
namespace NUnit.Framework.Diagnostics
7+
{
8+
/// <summary><para>
9+
/// The ProgressTraceListener class allows directing tracing or
10+
/// debugging output to <see cref="TestContext.Progress"/>.
11+
/// </para><para>
12+
/// To activate, place the following snippet into the one-time
13+
/// set-up method of either a test's fixture or the set-up
14+
/// fixture of a project:
15+
/// <c>
16+
/// System.Trace.Listeners.Add(new ProgressTraceListener());
17+
/// </c>
18+
/// </para><para>
19+
/// Make sure to only add a listener once, e.g.:
20+
/// <c>
21+
/// if (!System.Trace.Listeners.OfType&lt;ProgressTraceListener&gt;().Any())
22+
/// System.Trace.Listeners.Add(new ProgressTraceListener());
23+
/// </c>
24+
/// </para><para>
25+
/// Alternatively, add it in the one-time set-up and again remove
26+
/// it in the one-time tear-down, e.g.:
27+
/// <c>
28+
/// _progressTraceListener = new ProgressTraceListener();
29+
/// System.Trace.Listeners.Add(_progressTraceListener);
30+
/// </c>
31+
/// <c>
32+
/// System.Trace.Listeners.Remove(_progressTraceListener);
33+
/// _progressTraceListener.Close();
34+
/// </c>
35+
/// </para></summary>
36+
/// <remarks><para>
37+
/// Although named "Trace", <see cref="TextWriterTraceListener"/>
38+
/// "directs tracing or debugging output".
39+
/// </para><para>
40+
/// This listener is provided by NUnit (i.e. the origin of
41+
/// <see cref="TestContext.Progress"/>) same as the
42+
/// <see cref="ConsoleTraceListener"/> is provided by .NET
43+
/// (the origin of <see cref="Console"/>).
44+
/// </para></remarks>
45+
public class ProgressTraceListener : TextWriterTraceListener
46+
{
47+
#region Constructors
48+
49+
/// <summary>
50+
/// Construct a ProgressTraceListener with trace
51+
/// output written to <see cref="TestContext.Progress"/>
52+
/// </summary>
53+
public ProgressTraceListener() : base(TestContext.Progress)
54+
{
55+
}
56+
57+
#endregion
58+
}
59+
}
+152Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
// Copyright (c) Charlie Poole, Rob Prouse and Contributors. MIT License - see LICENSE.txt
2+
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Diagnostics;
6+
using NUnit.Framework.Diagnostics;
7+
using NUnit.Framework.Interfaces;
8+
using NUnit.Framework.Internal;
9+
10+
namespace NUnit.Framework.Tests.Diagnostics
11+
{
12+
public abstract class ProgressTraceListenerTestsBase
13+
{
14+
protected const string SOME_TEXT = "Should go to the output";
15+
protected static readonly string NL = Environment.NewLine;
16+
17+
protected TestListenerInterceptor TestResultListener { get; private set; }
18+
19+
[SetUp]
20+
public void AddTestResultListener()
21+
{
22+
// Wrap the current listener, listening to events, and forwarding the original event
23+
TestResultListener = new TestListenerInterceptor(TestExecutionContext.CurrentContext.Listener);
24+
TestExecutionContext.CurrentContext.Listener = TestResultListener;
25+
}
26+
27+
[TearDown]
28+
public void RemoveTestResultListener()
29+
{
30+
// Restore the original listener
31+
TestExecutionContext.CurrentContext.Listener = TestResultListener.DefaultListener;
32+
}
33+
34+
[Test]
35+
public void TestProgressIsOutput()
36+
{
37+
Assert.That(TestResultListener.Outputs, Has.Count.EqualTo(0));
38+
39+
TestContext.Progress.WriteLine(SOME_TEXT);
40+
Assert.That(TestResultListener.Outputs, Has.Count.EqualTo(1));
41+
Assert.That(TestResultListener.Outputs[0], Is.EqualTo(SOME_TEXT + NL));
42+
}
43+
44+
#region ITestListener implementation
45+
46+
protected class TestListenerInterceptor : ITestListener
47+
{
48+
public IList<string> Outputs { get; }
49+
public ITestListener DefaultListener { get; }
50+
51+
public TestListenerInterceptor(ITestListener defaultListener)
52+
{
53+
DefaultListener = defaultListener;
54+
Outputs = new List<string>();
55+
}
56+
57+
void ITestListener.TestStarted(ITest test)
58+
{
59+
DefaultListener?.TestStarted(test);
60+
}
61+
62+
void ITestListener.TestFinished(ITestResult result)
63+
{
64+
DefaultListener?.TestFinished(result);
65+
}
66+
67+
void ITestListener.TestOutput(TestOutput output)
68+
{
69+
Assert.That(output, Is.Not.Null);
70+
Outputs.Add(output.Text);
71+
72+
DefaultListener?.TestOutput(output);
73+
}
74+
75+
void ITestListener.SendMessage(TestMessage message)
76+
{
77+
}
78+
}
79+
80+
#endregion
81+
}
82+
83+
[TestFixture, NonParallelizable] // Non-parallelizable because the "ProgressTraceListener" may lead to side-effects in other tests.
84+
public class ProgressTraceListenerTests : ProgressTraceListenerTestsBase
85+
{
86+
private ProgressTraceListener _progressTraceListener;
87+
88+
[OneTimeSetUp]
89+
public void AddProgressTraceListener()
90+
{
91+
_progressTraceListener = new ProgressTraceListener();
92+
Trace.Listeners.Add(_progressTraceListener);
93+
}
94+
95+
[OneTimeTearDown]
96+
public void RemoveProgressTraceListener()
97+
{
98+
Trace.Listeners.Remove(_progressTraceListener);
99+
_progressTraceListener.Dispose();
100+
}
101+
102+
[Test]
103+
public void TestDebugIsDirectedToOutput()
104+
{
105+
Assert.That(TestResultListener.Outputs, Has.Count.EqualTo(0));
106+
107+
Debug.WriteLine(SOME_TEXT);
108+
#if DEBUG
109+
Assert.That(TestResultListener.Outputs, Has.Count.EqualTo(1));
110+
Assert.That(TestResultListener.Outputs[0], Is.EqualTo(SOME_TEXT + NL));
111+
#else
112+
Assert.That(TestResultListener.Outputs, Has.Count.EqualTo(0));
113+
#endif
114+
}
115+
116+
[Test]
117+
public void TestTraceIsDirectedToOutput()
118+
{
119+
Assert.That(TestResultListener.Outputs, Has.Count.EqualTo(0));
120+
121+
Trace.WriteLine(SOME_TEXT);
122+
#if TRACE
123+
Assert.That(TestResultListener.Outputs, Has.Count.EqualTo(1));
124+
Assert.That(TestResultListener.Outputs[0], Is.EqualTo(SOME_TEXT + NL));
125+
#else
126+
Assert.That(TestResultListener.Outputs, Has.Count.EqualTo(0));
127+
#endif
128+
}
129+
}
130+
131+
[TestFixture, NonParallelizable] // Non-parallelizable same as "ProgressTraceListenerTests" above.
132+
public class NoProgressTraceListenerTests : ProgressTraceListenerTestsBase
133+
{
134+
[Test]
135+
public void TestDebugIsNotDirectedToOutput()
136+
{
137+
Assert.That(TestResultListener.Outputs, Has.Count.EqualTo(0));
138+
139+
Debug.WriteLine(SOME_TEXT);
140+
Assert.That(TestResultListener.Outputs, Has.Count.EqualTo(0));
141+
}
142+
143+
[Test]
144+
public void TestTraceIsNotDirectedToOutput()
145+
{
146+
Assert.That(TestResultListener.Outputs, Has.Count.EqualTo(0));
147+
148+
Trace.WriteLine(SOME_TEXT);
149+
Assert.That(TestResultListener.Outputs, Has.Count.EqualTo(0));
150+
}
151+
}
152+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.