TestProgressReporter
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.Web.UI;
namespace NUnit.Framework.Internal
{
public class TestProgressReporter : ITestListener
{
private static Logger log = InternalTrace.GetLogger("TestProgressReporter");
private ICallbackEventHandler handler;
public TestProgressReporter(ICallbackEventHandler handler)
{
this.handler = handler;
}
public void TestStarted(ITest test)
{
string text = (test is TestSuite) ? "start-suite" : "start-test";
ITest parent = GetParent(test);
try {
string text2 = $"""{text}""{test.Id}""{((parent != null) ? parent.Id : string.Empty)}""{FormatAttributeValue(test.Name)}""{FormatAttributeValue(test.FullName)}""";
handler.RaiseCallbackEvent(text2);
} catch (Exception ex) {
log.Error("Exception processing " + test.FullName + Env.NewLine + ex.ToString());
}
}
public void TestFinished(ITestResult result)
{
try {
TNode tNode = result.ToXml(false);
ITest parent = GetParent(result.Test);
tNode.Attributes.Add("parentId", (parent != null) ? parent.Id : string.Empty);
handler.RaiseCallbackEvent(tNode.OuterXml);
} catch (Exception ex) {
log.Error("Exception processing " + result.FullName + Env.NewLine + ex.ToString());
}
}
private static ITest GetParent(ITest test)
{
if (test == null || test.Parent == null)
return null;
if (!test.Parent.IsSuite)
return GetParent(test.Parent);
return test.Parent;
}
private static string FormatAttributeValue(string original)
{
return original.Replace("&", "&").Replace("\"", """).Replace("'", "'")
.Replace("<", "<")
.Replace(">", ">");
}
}
}