<PackageReference Include="NUnit" Version="4.1.0" />

TestProgressReporter

public sealed class TestProgressReporter : ITestListener
TestProgressReporter translates ITestListener events into the async callbacks that are used to inform the client software about the progress of a test run.
using NUnit.Framework.Interfaces; using System; using System.IO; using System.Runtime.CompilerServices; using System.Text; using System.Web.UI; using System.Xml; namespace NUnit.Framework.Internal { [System.Runtime.CompilerServices.NullableContext(1)] [System.Runtime.CompilerServices.Nullable(0)] public sealed class TestProgressReporter : ITestListener { private static readonly Logger Log = InternalTrace.GetLogger("TestProgressReporter"); private readonly ICallbackEventHandler _handler; private readonly StringBuilder _stringBuilder = new StringBuilder(); public TestProgressReporter(ICallbackEventHandler handler) { _handler = handler; } public void TestStarted(ITest test) { string report = CreateTestStartedMessage(test); try { _handler.RaiseCallbackEvent(report); } catch (Exception value) { Logger log = Log; DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(21, 3); defaultInterpolatedStringHandler.AppendLiteral("Exception processing "); defaultInterpolatedStringHandler.AppendFormatted(test.FullName); defaultInterpolatedStringHandler.AppendFormatted(Environment.NewLine); defaultInterpolatedStringHandler.AppendFormatted(value); log.Error(defaultInterpolatedStringHandler.ToStringAndClear()); } } private string CreateTestStartedMessage(ITest test) { ITest parent = GetParent(test); StringBuilder stringBuilder = GetStringBuilder(); using (StringWriter stringWriter = new StringWriter(stringBuilder)) { using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, XmlExtensions.FragmentWriterSettings)) { bool flag = test is TestSuite; xmlWriter.WriteStartElement(flag ? "start-suite" : "start-test"); xmlWriter.WriteAttributeString("id", test.Id); xmlWriter.WriteAttributeString("parentId", (parent != null) ? parent.Id : string.Empty); xmlWriter.WriteAttributeStringSafe("name", test.Name); xmlWriter.WriteAttributeStringSafe("fullname", test.FullName); xmlWriter.WriteAttributeStringSafe("type", test.TestType); if (flag) { if (test.TestType == "Assembly") xmlWriter.WriteAttributeString("framework-version", typeof(TestProgressReporter).Assembly.GetName().Version?.ToString()); } else { xmlWriter.WriteAttributeStringSafe("classname", test.ClassName ?? string.Empty); xmlWriter.WriteAttributeStringSafe("methodname", test.MethodName ?? string.Empty); } xmlWriter.WriteEndElement(); } return stringWriter.ToString(); } } public void TestFinished(ITestResult result) { TNode tNode = result.ToXml(false); ITest parent = GetParent(result.Test); tNode.AddAttribute("parentId", (parent != null) ? parent.Id : string.Empty); using (StringWriter stringWriter = new StringWriter(GetStringBuilder())) { using (XmlWriter writer = XmlWriter.Create(stringWriter, XmlExtensions.FragmentWriterSettings)) tNode.WriteTo(writer); try { _handler.RaiseCallbackEvent(stringWriter.ToString()); } catch (Exception value) { Logger log = Log; DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(21, 3); defaultInterpolatedStringHandler.AppendLiteral("Exception processing "); defaultInterpolatedStringHandler.AppendFormatted(result.FullName); defaultInterpolatedStringHandler.AppendFormatted(Environment.NewLine); defaultInterpolatedStringHandler.AppendFormatted(value); log.Error(defaultInterpolatedStringHandler.ToStringAndClear()); } } } public void TestOutput(TestOutput output) { using (StringWriter stringWriter = new StringWriter(GetStringBuilder())) { using (XmlWriter writer = XmlWriter.Create(stringWriter, XmlExtensions.FragmentWriterSettings)) output.ToXml(writer); try { _handler.RaiseCallbackEvent(stringWriter.ToString()); } catch (Exception value) { Logger log = Log; DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(37, 2); defaultInterpolatedStringHandler.AppendLiteral("Exception processing TestOutput event"); defaultInterpolatedStringHandler.AppendFormatted(Environment.NewLine); defaultInterpolatedStringHandler.AppendFormatted(value); log.Error(defaultInterpolatedStringHandler.ToStringAndClear()); } } } public void SendMessage(TestMessage message) { using (StringWriter stringWriter = new StringWriter(GetStringBuilder())) { using (XmlWriter writer = XmlWriter.Create(stringWriter, XmlExtensions.FragmentWriterSettings)) message.ToXml(writer); try { _handler.RaiseCallbackEvent(stringWriter.ToString()); } catch (Exception value) { Logger log = Log; DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(38, 2); defaultInterpolatedStringHandler.AppendLiteral("Exception processing SendMessage event"); defaultInterpolatedStringHandler.AppendFormatted(Environment.NewLine); defaultInterpolatedStringHandler.AppendFormatted(value); log.Error(defaultInterpolatedStringHandler.ToStringAndClear()); } } } [return: System.Runtime.CompilerServices.Nullable(2)] private static ITest GetParent(ITest test) { ITest test2; while (true) { test2 = test?.Parent; if (test2 == null) return null; if (test2.IsSuite) break; test = test2; } return test2; } private StringBuilder GetStringBuilder() { _stringBuilder.Clear(); return _stringBuilder; } } }