[NUI] Rebase develnui (DevelNUI only patches --> master) (#3910)
[platform/core/csapi/tizenfx.git] / test / Tizen.NUI.Devel.Tests.Ubuntu / nunitlite / OutputWriters / NUnit3XmlOutputWriter.cs
1 // ***********************************************************************
2 // Copyright (c) 2011 Charlie Poole
3 //
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:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
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 // ***********************************************************************
23 #define PORTABLE
24 #define TIZEN
25 #define NUNIT_FRAMEWORK
26 #define NUNITLITE
27 #define NET_4_5
28 #define PARALLEL
29 using System;
30 using System.Collections;
31 using System.Collections.Generic;
32 using System.Globalization;
33 using System.IO;
34 using System.Reflection;
35 using System.Xml;
36 using NUnit.Common;
37 using NUnit.Framework.Api;
38 using NUnit.Framework.Interfaces;
39 using NUnit.Framework.Internal;
40
41 namespace NUnitLite
42 {
43     /// <summary>
44     /// NUnit3XmlOutputWriter is responsible for writing the results
45     /// of a test to a file in NUnit 3.0 format.
46     /// </summary>
47     public class NUnit3XmlOutputWriter : OutputWriter
48     {
49         /// <summary>
50         /// Writes test info to the specified TextWriter
51         /// </summary>
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)
55         {
56             XmlWriterSettings settings = new XmlWriterSettings();
57             settings.Indent = true;
58
59             using (XmlWriter xmlWriter = XmlWriter.Create(writer, settings))
60             {
61                 test.ToXml(true).WriteTo(xmlWriter);
62             }
63         }
64
65         /// <summary>
66         /// Writes the test result to the specified TextWriter
67         /// </summary>
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)
73         {
74             XmlWriterSettings xmlSettings = new XmlWriterSettings();
75             xmlSettings.Indent = true;
76
77             using (XmlWriter xmlWriter = XmlWriter.Create(writer, xmlSettings))
78             {
79                 WriteXmlResultOutput(result, xmlWriter, runSettings, filter);
80             }
81         }
82
83         private void WriteXmlResultOutput(ITestResult result, XmlWriter xmlWriter, IDictionary<string, object> runSettings, TestFilter filter)
84         {
85             TNode resultNode = result.ToXml(true);
86
87             // Insert elements as first child in reverse order
88             if (runSettings != null) // Some platforms don't have settings
89                 FrameworkController.InsertSettingsElement(resultNode, runSettings);
90 #if !SILVERLIGHT
91             //FrameworkController.InsertEnvironmentElement(resultNode);
92 #endif
93
94             TNode testRun = MakeTestRunElement(result);
95
96 #if !SILVERLIGHT && !NETCF
97             //testRun.ChildNodes.Add(MakeCommandLineElement());
98 #endif
99             testRun.ChildNodes.Add(MakeTestFilterElement(filter));
100             testRun.ChildNodes.Add(resultNode);
101
102             testRun.WriteTo(xmlWriter);
103         }
104
105         private TNode MakeTestRunElement(ITestResult result)
106         {
107             TNode testRun = new TNode("test-run");
108
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());
113
114             testRun.AddAttribute("result", result.ResultState.Status.ToString());
115             if (result.ResultState.Label != string.Empty)
116                 testRun.AddAttribute("label", result.ResultState.Label);
117
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));
121
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());
128
129             testRun.AddAttribute("random-seed", Randomizer.InitialSeed.ToString());
130
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.
134
135             return testRun;
136         }
137
138 #if !SILVERLIGHT && !NETCF
139         //private static TNode MakeCommandLineElement()
140         //{
141         //    return new TNode("command-line", Environment.CommandLine, true);
142         //}
143 #endif
144
145         private static TNode MakeTestFilterElement(TestFilter filter)
146         {
147             TNode result = new TNode("filter");
148             if (!filter.IsEmpty)
149                 filter.AddToXml(result, true);
150             return result;
151         }
152     }
153 }