|
| 1 | +using System.Linq; |
| 2 | +using System.Xml; |
| 3 | +using UnityEditor.TestTools.TestRunner.Api; |
| 4 | + |
| 5 | +public static class Reporter |
| 6 | +{ |
| 7 | + |
| 8 | + public static void ReportJUnitXML(string path, ITestResultAdaptor result) |
| 9 | + { |
| 10 | + XmlDocument xmlDoc = new XmlDocument(); |
| 11 | + XmlDeclaration xmlDecl = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null); |
| 12 | + xmlDoc.AppendChild(xmlDecl); |
| 13 | + |
| 14 | + XmlNode rootNode = xmlDoc.CreateElement("testsuites"); |
| 15 | + XmlAttribute rootAttrTests = xmlDoc.CreateAttribute("tests"); |
| 16 | + rootAttrTests.Value = result.PassCount.ToString(); |
| 17 | + rootNode.Attributes.Append(rootAttrTests); |
| 18 | + XmlAttribute rootAttrSkipped = xmlDoc.CreateAttribute("disabled"); |
| 19 | + rootAttrSkipped.Value = result.SkipCount.ToString(); |
| 20 | + rootNode.Attributes.Append(rootAttrSkipped); |
| 21 | + XmlAttribute rootAttrFailed = xmlDoc.CreateAttribute("failures"); |
| 22 | + rootAttrFailed.Value = result.FailCount.ToString(); |
| 23 | + rootNode.Attributes.Append(rootAttrFailed); |
| 24 | + XmlAttribute rootAttrName = xmlDoc.CreateAttribute("name"); |
| 25 | + rootAttrName.Value = result.Name; |
| 26 | + rootNode.Attributes.Append(rootAttrName); |
| 27 | + XmlAttribute rootAttrTime = xmlDoc.CreateAttribute("time"); |
| 28 | + rootAttrTime.Value = result.Duration.ToString(); |
| 29 | + rootNode.Attributes.Append(rootAttrTime); |
| 30 | + xmlDoc.AppendChild(rootNode); |
| 31 | + |
| 32 | + foreach (ITestResultAdaptor testSuite in result.Children.First().Children) |
| 33 | + { |
| 34 | + XmlNode testSuiteNode = xmlDoc.CreateElement("testsuite"); |
| 35 | + XmlAttribute testSuiteAttrName = xmlDoc.CreateAttribute("name"); |
| 36 | + testSuiteAttrName.Value = testSuite.Name; |
| 37 | + testSuiteNode.Attributes.Append(testSuiteAttrName); |
| 38 | + XmlAttribute testSuiteAttrTests = xmlDoc.CreateAttribute("tests"); |
| 39 | + testSuiteAttrTests.Value = testSuite.Test.TestCaseCount.ToString(); |
| 40 | + testSuiteNode.Attributes.Append(testSuiteAttrTests); |
| 41 | + XmlAttribute testSuiteAttrSkipped = xmlDoc.CreateAttribute("disabled"); |
| 42 | + testSuiteAttrSkipped.Value = testSuite.SkipCount.ToString(); |
| 43 | + testSuiteNode.Attributes.Append(testSuiteAttrSkipped); |
| 44 | + XmlAttribute testSuiteAttrFailed = xmlDoc.CreateAttribute("failures"); |
| 45 | + testSuiteAttrFailed.Value = testSuite.FailCount.ToString(); |
| 46 | + testSuiteNode.Attributes.Append(testSuiteAttrFailed); |
| 47 | + XmlAttribute testSuiteAttrTime = xmlDoc.CreateAttribute("time"); |
| 48 | + testSuiteAttrTime.Value = testSuite.Duration.ToString(); |
| 49 | + testSuiteNode.Attributes.Append(testSuiteAttrTime); |
| 50 | + foreach (ITestResultAdaptor testFixture in testSuite.Children) |
| 51 | + { |
| 52 | + foreach (ITestResultAdaptor testCase in testFixture.Children) |
| 53 | + { |
| 54 | + XmlNode testCaseNode = xmlDoc.CreateElement("testcase"); |
| 55 | + XmlAttribute testAttrName = xmlDoc.CreateAttribute("name"); |
| 56 | + testAttrName.Value = testCase.Name; |
| 57 | + testCaseNode.Attributes.Append(testAttrName); |
| 58 | + XmlAttribute testAttrAssertions = xmlDoc.CreateAttribute("assertions"); |
| 59 | + testAttrAssertions.Value = testCase.AssertCount.ToString(); |
| 60 | + testCaseNode.Attributes.Append(testAttrAssertions); |
| 61 | + XmlAttribute testAttrTime = xmlDoc.CreateAttribute("time"); |
| 62 | + testAttrTime.Value = testCase.Duration.ToString(); |
| 63 | + testCaseNode.Attributes.Append(testAttrTime); |
| 64 | + XmlAttribute testAttrStatus = xmlDoc.CreateAttribute("status"); |
| 65 | + testAttrStatus.Value = testCase.TestStatus.ToString(); |
| 66 | + testCaseNode.Attributes.Append(testAttrStatus); |
| 67 | + testSuiteNode.AppendChild(testCaseNode); |
| 68 | + } |
| 69 | + } |
| 70 | + rootNode.AppendChild(testSuiteNode); |
| 71 | + } |
| 72 | + |
| 73 | + xmlDoc.Save(path); |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments