1 // ***********************************************************************
2 // Copyright (c) 2010 Charlie Poole
4 // Permission is hereby granted, free of charge, to any person obtaining
5 // a copy of this software and associated documentation files (the
6 // "Software"), to deal in the Software without restriction, including
7 // without limitation the rights to use, copy, modify, merge, publish,
8 // distribute, sublicense, and/or sell copies of the Software, and to
9 // permit persons to whom the Software is furnished to do so, subject to
10 // the following conditions:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22 // ***********************************************************************
25 #define NUNIT_FRAMEWORK
31 using NUnit.Framework.Interfaces;
33 namespace NUnit.Framework.Internal
36 /// TestProgressReporter translates ITestListener events into
37 /// the async callbacks that are used to inform the client
38 /// software about the progress of a test run.
40 public class TestProgressReporter : ITestListener
42 static Logger log = InternalTrace.GetLogger("TestProgressReporter");
44 private ICallbackEventHandler handler;
47 /// Initializes a new instance of the <see cref="TestProgressReporter"/> class.
49 /// <param name="handler">The callback handler to be used for reporting progress.</param>
50 public TestProgressReporter(ICallbackEventHandler handler)
52 this.handler = handler;
55 #region ITestListener Members
58 /// Called when a test has just started
60 /// <param name="test">The test that is starting</param>
61 public void TestStarted(ITest test)
63 string startElement = test is TestSuite
67 var parent = GetParent(test);
70 string report = string.Format(
71 "<{0} id=\"{1}\" parentId=\"{2}\" name=\"{3}\" fullname=\"{4}\"/>",
74 parent != null ? parent.Id : string.Empty,
75 FormatAttributeValue(test.Name),
76 FormatAttributeValue(test.FullName));
78 handler.RaiseCallbackEvent(report);
82 log.Error("Exception processing " + test.FullName + NUnit.Env.NewLine + ex.ToString());
87 /// Called when a test has finished. Sends a result summary to the callback.
90 /// <param name="result">The result of the test</param>
91 public void TestFinished(ITestResult result)
95 var node = result.ToXml(false);
96 var parent = GetParent(result.Test);
97 node.Attributes.Add("parentId", parent != null ? parent.Id : string.Empty);
98 handler.RaiseCallbackEvent(node.OuterXml);
102 log.Error("Exception processing " + result.FullName + NUnit.Env.NewLine + ex.ToString());
107 /// Called when a test produces output for immediate display
109 /// <param name="output">A TestOutput object containing the text to display</param>
110 public void TestOutput(TestOutput output)
114 handler.RaiseCallbackEvent(output.ToXml());
118 log.Error("Exception processing TestOutput event" + NUnit.Env.NewLine + ex.ToString());
124 #region Helper Methods
127 /// Returns the parent test item for the targer test item if it exists
129 /// <param name="test"></param>
130 /// <returns>parent test item</returns>
131 private static ITest GetParent(ITest test)
133 if (test == null || test.Parent == null)
138 return test.Parent.IsSuite ? test.Parent : GetParent(test.Parent);
142 /// Makes a string safe for use as an attribute, replacing
143 /// characters characters that can't be used with their
144 /// corresponding xml representations.
146 /// <param name="original">The string to be used</param>
147 /// <returns>A new string with the _values replaced</returns>
148 private static string FormatAttributeValue(string original)
151 .Replace("&", "&")
152 .Replace("\"", """)
153 .Replace("'", "'")
154 .Replace("<", "<")
155 .Replace(">", ">");