|
| 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