1 // ***********************************************************************
2 // Copyright (c) 2011 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
30 using System.Collections;
31 using System.Collections.Generic;
32 using System.Globalization;
34 using System.Reflection;
37 using NUnit.Framework.Api;
38 using NUnit.Framework.Interfaces;
39 using NUnit.Framework.Internal;
44 /// NUnit3XmlOutputWriter is responsible for writing the results
45 /// of a test to a file in NUnit 3.0 format.
47 public class NUnit3XmlOutputWriter : OutputWriter
50 /// Writes test info to the specified TextWriter
52 /// <param name="test">The test to be written</param>
53 /// <param name="writer">A TextWriter to which the test info is written</param>
54 public override void WriteTestFile(ITest test, TextWriter writer)
56 XmlWriterSettings settings = new XmlWriterSettings();
57 settings.Indent = true;
59 using (XmlWriter xmlWriter = XmlWriter.Create(writer, settings))
61 test.ToXml(true).WriteTo(xmlWriter);
66 /// Writes the test result to the specified TextWriter
68 /// <param name="result">The result to be written to a file</param>
69 /// <param name="writer">A TextWriter to which the result is written</param>
70 /// <param name="runSettings"></param>
71 /// <param name="filter"></param>
72 public override void WriteResultFile(ITestResult result, TextWriter writer, IDictionary<string, object> runSettings, TestFilter filter)
74 XmlWriterSettings xmlSettings = new XmlWriterSettings();
75 xmlSettings.Indent = true;
77 using (XmlWriter xmlWriter = XmlWriter.Create(writer, xmlSettings))
79 WriteXmlResultOutput(result, xmlWriter, runSettings, filter);
83 private void WriteXmlResultOutput(ITestResult result, XmlWriter xmlWriter, IDictionary<string, object> runSettings, TestFilter filter)
85 TNode resultNode = result.ToXml(true);
87 // Insert elements as first child in reverse order
88 if (runSettings != null) // Some platforms don't have settings
89 FrameworkController.InsertSettingsElement(resultNode, runSettings);
91 //FrameworkController.InsertEnvironmentElement(resultNode);
94 TNode testRun = MakeTestRunElement(result);
96 #if !SILVERLIGHT && !NETCF
97 //testRun.ChildNodes.Add(MakeCommandLineElement());
99 testRun.ChildNodes.Add(MakeTestFilterElement(filter));
100 testRun.ChildNodes.Add(resultNode);
102 testRun.WriteTo(xmlWriter);
105 private TNode MakeTestRunElement(ITestResult result)
107 TNode testRun = new TNode("test-run");
109 testRun.AddAttribute("id", "2");
110 testRun.AddAttribute("name", result.Name);
111 testRun.AddAttribute("fullname", result.FullName);
112 testRun.AddAttribute("testcasecount", result.Test.TestCaseCount.ToString());
114 testRun.AddAttribute("result", result.ResultState.Status.ToString());
115 if (result.ResultState.Label != string.Empty)
116 testRun.AddAttribute("label", result.ResultState.Label);
118 testRun.AddAttribute("start-time", result.StartTime.ToString("u"));
119 testRun.AddAttribute("end-time", result.EndTime.ToString("u"));
120 testRun.AddAttribute("duration", result.Duration.ToString("0.000000", NumberFormatInfo.InvariantInfo));
122 testRun.AddAttribute("total", (result.PassCount + result.FailCount + result.SkipCount + result.InconclusiveCount).ToString());
123 testRun.AddAttribute("passed", result.PassCount.ToString());
124 testRun.AddAttribute("failed", result.FailCount.ToString());
125 testRun.AddAttribute("inconclusive", result.InconclusiveCount.ToString());
126 testRun.AddAttribute("skipped", result.SkipCount.ToString());
127 testRun.AddAttribute("asserts", result.AssertCount.ToString());
129 testRun.AddAttribute("random-seed", Randomizer.InitialSeed.ToString());
131 // NOTE: The console runner adds attributes for engine-version and clr-version
132 // Neither of these is needed under nunitlite since there is no engine involved
133 // and we are running under the same runtime as the tests.
138 #if !SILVERLIGHT && !NETCF
139 //private static TNode MakeCommandLineElement()
141 // return new TNode("command-line", Environment.CommandLine, true);
145 private static TNode MakeTestFilterElement(TestFilter filter)
147 TNode result = new TNode("filter");
149 filter.AddToXml(result, true);