Skip to content

Commit 0ea1494

Browse files
committed
feat(tests): add test reporting with custom JUnitXML exporter
1 parent 6922bc0 commit 0ea1494

File tree

4 files changed

+107
-1
lines changed

4 files changed

+107
-1
lines changed

Assets/Tests/EditorTests/Reporter.cs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
}

Assets/Tests/EditorTests/Reporter.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Tests/EditorTests/Runner.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,23 @@ public void RunStarted(ITestAdaptor testsToRun)
1414

1515
public void RunFinished(ITestResultAdaptor result)
1616
{
17+
// export test results to JUnit XML format if
18+
// using -testsOutput argument
19+
string reportPath = null;
20+
string[] args = System.Environment.GetCommandLineArgs();
21+
for (int i = 0; i < args.Length; i++)
22+
{
23+
if (args[i] == "-testsOutput")
24+
{
25+
reportPath = args[i + 1];
26+
break;
27+
}
28+
}
29+
30+
if (reportPath != null) {
31+
Reporter.ReportJUnitXML(reportPath, result);
32+
}
33+
1734
_runner.UnregisterCallbacks(this);
1835
if (result.ResultState != "Passed")
1936
{

codemagic.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ workflows:
88
- unity # <-- (Includes UNITY_SERIAL, UNITY_USERNAME, UNITY_PASSWORD)
99
vars:
1010
UNITY_BIN: /Applications/Unity/Hub/Editor/2020.3.20f1/Unity.app/Contents/MacOS/Unity
11+
TEST_REPORT_PATH: tests.xml
1112
scripts:
1213
- name: Activate License
1314
script: $UNITY_BIN -batchmode -quit -logFile -serial ${UNITY_SERIAL?} -username ${UNITY_USERNAME?} -password ${UNITY_PASSWORD?}
1415
- name: Run Unit Tests
15-
script: $UNITY_BIN -batchmode -executeMethod Runner.RunUnitTests -logFile -nographics -projectPath .
16+
script: $UNITY_BIN -batchmode -executeMethod Runner.RunUnitTests -logFile -nographics -projectPath . -testsOutput ${TEST_REPORT_PATH}
17+
test_report: ${TEST_REPORT_PATH}
1618
- name: Build
1719
script: $UNITY_BIN -batchmode -quit -logFile -projectPath . -executeMethod BuildScript.BuildMac -nographics
1820
artifacts:

0 commit comments

Comments
 (0)