| | 1 | | // Copyright (c) 2020-2024 dotBunny Inc. |
| | 2 | | // dotBunny licenses this file to you under the BSL-1.0 license. |
| | 3 | | // See the LICENSE file in the project root for more information. |
| | 4 | |
|
| | 5 | | using System.Runtime.CompilerServices; |
| | 6 | | using GDX.Developer.Reports.NUnit; |
| | 7 | |
|
| | 8 | | namespace GDX.Developer.Reports |
| | 9 | | { |
| | 10 | | public class NUnitReport |
| | 11 | | { |
| | 12 | | public const string SkippedString = "Skipped"; |
| | 13 | | public const string FailedString = "Failed"; |
| | 14 | | public const string PassedString = "Passed"; |
| | 15 | |
|
| | 16 | |
|
| 13 | 17 | | readonly TestRun m_Results = new TestRun(); |
| | 18 | | bool m_ForceFail; |
| | 19 | |
|
| 13 | 20 | | public NUnitReport(string name = null, string fullName = null, string className = null) |
| 13 | 21 | | { |
| 13 | 22 | | m_Results.TestSuite.Name = name; |
| 13 | 23 | | m_Results.TestSuite.FullName = fullName; |
| 13 | 24 | | m_Results.TestSuite.ClassName = className; |
| 13 | 25 | | } |
| | 26 | |
|
| | 27 | | public TestCase AddDurationResult(string name, float seconds, bool passed = true, string failMessage = null, |
| | 28 | | TestSuite testSuite = null) |
| 7 | 29 | | { |
| 7 | 30 | | TestCase testCase = new TestCase |
| | 31 | | { |
| | 32 | | Name = name, |
| | 33 | | Duration = seconds, |
| | 34 | | Result = passed ? PassedString : FailedString, |
| | 35 | | Message = passed ? null : failMessage |
| | 36 | | }; |
| 7 | 37 | | if (testSuite != null) |
| 1 | 38 | | { |
| 1 | 39 | | testSuite.TestCases.Add(testCase); |
| 1 | 40 | | } |
| | 41 | | else |
| 6 | 42 | | { |
| 6 | 43 | | m_Results.TestSuite.TestCases.Add(testCase); |
| 6 | 44 | | } |
| | 45 | |
|
| 7 | 46 | | return testCase; |
| 7 | 47 | | } |
| | 48 | |
|
| | 49 | | public TestCase AddSkippedTest(string name, string skipMessage = null, TestSuite testSuite = null) |
| 5 | 50 | | { |
| 5 | 51 | | TestCase testCase = new TestCase { Name = name, Result = SkippedString, Message = skipMessage }; |
| 5 | 52 | | if (testSuite != null) |
| 2 | 53 | | { |
| 2 | 54 | | testSuite.TestCases.Add(testCase); |
| 2 | 55 | | } |
| | 56 | | else |
| 3 | 57 | | { |
| 3 | 58 | | m_Results.TestSuite.TestCases.Add(testCase); |
| 3 | 59 | | } |
| | 60 | |
|
| 5 | 61 | | return testCase; |
| 5 | 62 | | } |
| | 63 | |
|
| | 64 | | public void SetForceFail() |
| 1 | 65 | | { |
| 1 | 66 | | m_ForceFail = true; |
| 1 | 67 | | } |
| | 68 | |
|
| | 69 | | public int GetResultCount() |
| 2 | 70 | | { |
| 2 | 71 | | return m_Results.TestCaseCount; |
| 2 | 72 | | } |
| | 73 | |
|
| | 74 | | public string GetResult() |
| 1 | 75 | | { |
| 1 | 76 | | if (m_ForceFail) |
| 1 | 77 | | { |
| 1 | 78 | | return FailedString; |
| | 79 | | } |
| | 80 | |
|
| 0 | 81 | | return m_Results.Result; |
| 1 | 82 | | } |
| | 83 | |
|
| | 84 | | public TestSuite GetRootSuite() |
| 9 | 85 | | { |
| 9 | 86 | | return m_Results.TestSuite; |
| 9 | 87 | | } |
| | 88 | |
|
| | 89 | | public void Process() |
| 2 | 90 | | { |
| 2 | 91 | | m_Results.Process(); |
| 2 | 92 | | } |
| | 93 | |
|
| | 94 | | /// <summary> |
| | 95 | | /// Builds and returns the NUnit formatted report. |
| | 96 | | /// </summary> |
| | 97 | | /// <remarks>Can result in a UTF-16 based XML document.</remarks> |
| | 98 | | public override string ToString() |
| 2 | 99 | | { |
| | 100 | | // We need to make sure it is a UTF-8 xml serializer as most parsers wont know what to do with UTF-16 |
| 2 | 101 | | TextGenerator generator = new TextGenerator(); |
| 2 | 102 | | m_Results.Process(); |
| 2 | 103 | | AddToGenerator(generator, m_Results); |
| 2 | 104 | | return generator.ToString(); |
| 2 | 105 | | } |
| | 106 | |
|
| | 107 | | static void AddToGenerator(TextGenerator generator, Property property) |
| 0 | 108 | | { |
| 0 | 109 | | generator.ApplyIndent(); |
| 0 | 110 | | generator.Append("<property"); |
| 0 | 111 | | AddToGeneratorAttribute(generator, "name", property.Name); |
| 0 | 112 | | AddToGeneratorAttribute(generator, "value", property.Value); |
| 0 | 113 | | generator.Append(" />"); |
| 0 | 114 | | generator.NextLine(); |
| 0 | 115 | | } |
| | 116 | |
|
| | 117 | | static void AddToGenerator(TextGenerator generator, Properties properties) |
| 0 | 118 | | { |
| 0 | 119 | | generator.AppendLine("<properties>"); |
| 0 | 120 | | generator.PushIndent(); |
| 0 | 121 | | int count = properties.Property.Count; |
| 0 | 122 | | for (int i = 0; i < count; i++) |
| 0 | 123 | | { |
| 0 | 124 | | AddToGenerator(generator, properties.Property[i]); |
| 0 | 125 | | } |
| | 126 | |
|
| 0 | 127 | | generator.PopIndent(); |
| 0 | 128 | | generator.AppendLine("</properties>"); |
| 0 | 129 | | } |
| | 130 | |
|
| | 131 | | static void AddToGenerator(TextGenerator generator, TestCase testCase) |
| 4 | 132 | | { |
| 4 | 133 | | generator.ApplyIndent(); |
| 4 | 134 | | generator.Append("<test-case"); |
| | 135 | | // ReSharper disable StringLiteralTypo |
| 4 | 136 | | AddToGeneratorAttribute(generator, "id", testCase.Id); |
| 4 | 137 | | AddToGeneratorAttribute(generator, "name", testCase.Name); |
| 4 | 138 | | AddToGeneratorAttribute(generator, "fullname", testCase.FullName); |
| 4 | 139 | | AddToGeneratorAttribute(generator, "methodname", testCase.MethodName); |
| 4 | 140 | | AddToGeneratorAttribute(generator, "classname", testCase.ClassName); |
| 4 | 141 | | AddToGeneratorAttribute(generator, "runstate", testCase.RunState); |
| 4 | 142 | | AddToGeneratorAttribute(generator, "seed", testCase.Seed); |
| 4 | 143 | | AddToGeneratorAttribute(generator, "result", testCase.Result); |
| 4 | 144 | | AddToGeneratorAttribute(generator, "start-time", testCase.StartTime); |
| 4 | 145 | | AddToGeneratorAttribute(generator, "end-time", testCase.EndTime); |
| 4 | 146 | | AddToGeneratorAttribute(generator, "duration", testCase.Duration); |
| 4 | 147 | | AddToGeneratorAttribute(generator, "asserts", testCase.Asserts); |
| 4 | 148 | | AddToGeneratorAttribute(generator, "output", testCase.Output); |
| | 149 | | // ReSharper restore StringLiteralTypo |
| | 150 | |
|
| 4 | 151 | | if ((testCase.Properties != null && testCase.Properties.Property.Count > 0) || |
| | 152 | | testCase.Output != null) |
| 0 | 153 | | { |
| 0 | 154 | | generator.Append(">"); |
| 0 | 155 | | generator.NextLine(); |
| 0 | 156 | | generator.PushIndent(); |
| | 157 | |
|
| 0 | 158 | | if (testCase.Output != null) |
| 0 | 159 | | { |
| 0 | 160 | | generator.AppendLine($"<output><![CDATA[{testCase.Output}]]></output>"); |
| 0 | 161 | | } |
| | 162 | |
|
| 0 | 163 | | if (testCase.Message != null) |
| 0 | 164 | | { |
| 0 | 165 | | generator.AppendLine($"<message><![CDATA[{testCase.Output}]]></message>"); |
| 0 | 166 | | } |
| | 167 | |
|
| 0 | 168 | | if (testCase.StackTrace != null) |
| 0 | 169 | | { |
| 0 | 170 | | generator.AppendLine($"<stack-trace><![CDATA[{testCase.Output}]]></stack-trace>"); |
| 0 | 171 | | } |
| | 172 | |
|
| 0 | 173 | | if (testCase.Properties != null && testCase.Properties.Property.Count > 0) |
| 0 | 174 | | { |
| 0 | 175 | | AddToGenerator(generator, testCase.Properties); |
| 0 | 176 | | } |
| | 177 | |
|
| 0 | 178 | | generator.PopIndent(); |
| 0 | 179 | | generator.AppendLine("</test-case>"); |
| 0 | 180 | | } |
| | 181 | | else |
| 4 | 182 | | { |
| 4 | 183 | | generator.Append(" />"); |
| 4 | 184 | | generator.NextLine(); |
| 4 | 185 | | } |
| 4 | 186 | | } |
| | 187 | |
|
| | 188 | | static void AddToGenerator(TextGenerator generator, TestSuite testSuite) |
| 2 | 189 | | { |
| 2 | 190 | | generator.ApplyIndent(); |
| 2 | 191 | | generator.Append("<test-suite"); |
| | 192 | | // ReSharper disable StringLiteralTypo |
| 2 | 193 | | AddToGeneratorAttribute(generator, "type", testSuite.Type); |
| 2 | 194 | | AddToGeneratorAttribute(generator, "id", testSuite.Id); |
| 2 | 195 | | AddToGeneratorAttribute(generator, "name", testSuite.Name); |
| 2 | 196 | | AddToGeneratorAttribute(generator, "fullname", testSuite.FullName); |
| 2 | 197 | | AddToGeneratorAttribute(generator, "classname", testSuite.ClassName); |
| 2 | 198 | | AddToGeneratorAttribute(generator, "runstate", testSuite.RunState); |
| | 199 | |
|
| 2 | 200 | | AddToGeneratorAttribute(generator, "testcasecount", testSuite.TestCaseCount); |
| 2 | 201 | | AddToGeneratorAttribute(generator, "result", testSuite.Result); |
| 2 | 202 | | AddToGeneratorAttribute(generator, "label", testSuite.Label); |
| 2 | 203 | | AddToGeneratorAttribute(generator, "start-time", testSuite.StartTime); |
| 2 | 204 | | AddToGeneratorAttribute(generator, "end-time", testSuite.EndTime); |
| 2 | 205 | | AddToGeneratorAttribute(generator, "duration", testSuite.Duration); |
| | 206 | |
|
| 2 | 207 | | AddToGeneratorAttribute(generator, "total", testSuite.Total); |
| 2 | 208 | | AddToGeneratorAttribute(generator, "passed", testSuite.Passed); |
| 2 | 209 | | AddToGeneratorAttribute(generator, "failed", testSuite.Failed); |
| 2 | 210 | | AddToGeneratorAttribute(generator, "inconclusive", testSuite.Inconclusive); |
| 2 | 211 | | AddToGeneratorAttribute(generator, "skipped", testSuite.Skipped); |
| 2 | 212 | | AddToGeneratorAttribute(generator, "asserts", testSuite.Asserts); |
| | 213 | | // ReSharper restore StringLiteralTypo |
| | 214 | |
|
| 2 | 215 | | generator.Append(">"); |
| 2 | 216 | | generator.NextLine(); |
| 2 | 217 | | generator.PushIndent(); |
| 2 | 218 | | if (testSuite.Properties != null) |
| 0 | 219 | | { |
| 0 | 220 | | AddToGenerator(generator, testSuite.Properties); |
| 0 | 221 | | } |
| | 222 | |
|
| 14 | 223 | | foreach (TestCase t in testSuite.TestCases) |
| 4 | 224 | | { |
| 4 | 225 | | AddToGenerator(generator, t); |
| 4 | 226 | | } |
| | 227 | |
|
| 6 | 228 | | foreach (TestSuite s in testSuite.TestSuites) |
| 0 | 229 | | { |
| 0 | 230 | | AddToGenerator(generator, s); |
| 0 | 231 | | } |
| | 232 | |
|
| 2 | 233 | | generator.PopIndent(); |
| 2 | 234 | | generator.AppendLine("</test-suite>"); |
| 2 | 235 | | } |
| | 236 | |
|
| | 237 | | static void AddToGenerator(TextGenerator generator, TestRun testRun) |
| 2 | 238 | | { |
| 2 | 239 | | generator.ApplyIndent(); |
| 2 | 240 | | generator.Append("<test-run"); |
| 2 | 241 | | AddToGeneratorAttribute(generator, "id", testRun.Id); |
| | 242 | | // ReSharper disable once StringLiteralTypo |
| 2 | 243 | | AddToGeneratorAttribute(generator, "testcasecount", testRun.TestCaseCount); |
| 2 | 244 | | AddToGeneratorAttribute(generator, "result", testRun.Result); |
| 2 | 245 | | AddToGeneratorAttribute(generator, "total", testRun.Total); |
| 2 | 246 | | AddToGeneratorAttribute(generator, "passed", testRun.Passed); |
| 2 | 247 | | AddToGeneratorAttribute(generator, "failed", testRun.Failed); |
| 2 | 248 | | AddToGeneratorAttribute(generator, "inconclusive", testRun.Inconclusive); |
| 2 | 249 | | AddToGeneratorAttribute(generator, "skipped", testRun.Skipped); |
| 2 | 250 | | AddToGeneratorAttribute(generator, "asserts", testRun.Asserts); |
| 2 | 251 | | AddToGeneratorAttribute(generator, "engine-version", testRun.EngineVersion); |
| 2 | 252 | | AddToGeneratorAttribute(generator, "clr-version", testRun.CLRVersion); |
| 2 | 253 | | AddToGeneratorAttribute(generator, "start-time", testRun.StartTime); |
| 2 | 254 | | AddToGeneratorAttribute(generator, "end-time", testRun.EndTime); |
| 2 | 255 | | AddToGeneratorAttribute(generator, "duration", testRun.Duration); |
| 2 | 256 | | generator.Append(">"); |
| 2 | 257 | | generator.NextLine(); |
| 2 | 258 | | generator.PushIndent(); |
| 2 | 259 | | AddToGenerator(generator, testRun.TestSuite); |
| 2 | 260 | | generator.PopIndent(); |
| 2 | 261 | | generator.AppendLine("</test-run>"); |
| 2 | 262 | | } |
| | 263 | |
|
| | 264 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 265 | | static void AddToGeneratorAttribute(TextGenerator generator, string key, string value) |
| 70 | 266 | | { |
| 70 | 267 | | if (value == default) |
| 52 | 268 | | { |
| 52 | 269 | | return; |
| | 270 | | } |
| | 271 | |
|
| 18 | 272 | | generator.Append($" {key}=\"{value}\""); |
| 70 | 273 | | } |
| | 274 | |
|
| | 275 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 276 | | static void AddToGeneratorAttribute(TextGenerator generator, string key, int value) |
| 38 | 277 | | { |
| 38 | 278 | | if (value == default) |
| 22 | 279 | | { |
| 22 | 280 | | return; |
| | 281 | | } |
| | 282 | |
|
| 16 | 283 | | generator.Append($" {key}=\"{value}\""); |
| 38 | 284 | | } |
| | 285 | |
|
| | 286 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 287 | | static void AddToGeneratorAttribute(TextGenerator generator, string key, float value) |
| 8 | 288 | | { |
| 8 | 289 | | if (value == 0f) |
| 5 | 290 | | { |
| 5 | 291 | | return; |
| | 292 | | } |
| | 293 | |
|
| 3 | 294 | | generator.Append($" {key}=\"{value}\""); |
| 8 | 295 | | } |
| | 296 | | } |
| | 297 | | } |