Skip to content

Commit

Permalink
reporter test
Browse files Browse the repository at this point in the history
  • Loading branch information
Santarh committed Mar 27, 2024
1 parent 69c518c commit 409682a
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 2 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/create-unitypackage-test-report.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Create UnityPackage Test Report

on:
workflow_run:
workflows: ["Create UnityPackage"]
types:
- completed

permissions:
contents: read
actions: read
pull-requests: write

jobs:
report:
runs-on: ubuntu-latest
steps:
- uses: dorny/test-reporter@v1
with:
artifact: nunit-test-results
name: UnityPackage Test Report
path: "*.xml"
reporter: dotnet-trx
13 changes: 11 additions & 2 deletions .github/workflows/create-unitypackage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
workflow_dispatch:
push:
branches:
- workflow-wip
- workflow-wip-2

env:
UNITY_PROJECT_PATH: .
Expand Down Expand Up @@ -70,7 +70,8 @@ jobs:
-silent-crashes \
-projectPath "${UNITY_PROJECT_PATH}" \
-executeMethod "UniGLTF.TestRunner.RunEditModeTests" \
-logFile output.log
-logFile output.log \
-testRunnerNUnitXmlFile output.xml
RET=$?
set -e
Expand All @@ -85,6 +86,14 @@ jobs:
exit 1
fi
- name: Upload Test Results
id: upload-test-results
uses: actions/upload-artifact@v4
if: success() || failure()
with:
name: nunit-test-results
path: output.xml




61 changes: 61 additions & 0 deletions Assets/UniGLTF/Editor/TestRunner/TestRunner.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@

using System.Globalization;
using System.IO;
using System.Text;
using System.Xml;
using NUnit.Framework.Interfaces;
using UnityEditor;
using UnityEditor.TestTools.TestRunner.Api;
using UnityEngine;
using TestStatus = UnityEditor.TestTools.TestRunner.Api.TestStatus;

namespace UniGLTF
{
Expand All @@ -21,8 +27,28 @@ private class TestCallback : ICallbacks
{
private static readonly string LogPrefix = $"[[TestRunnerLog]] ";
private static readonly string ResultLogPrefix = $"[[TestRunnerResult]] ";

private readonly string _xmlFilePath;
private StackTraceLogType _tmpStackTraceLogType;

public TestCallback()
{
if (!Application.isBatchMode) return;

var arguments = System.Environment.GetCommandLineArgs();
for (var idx = 0; idx < arguments.Length; idx++)
{
if (arguments[idx] == "-testRunnerNUnitXmlFile" && idx + 1 < arguments.Length)
{
if (!arguments[idx + 1].StartsWith("-"))
{
_xmlFilePath = arguments[idx + 1];
break;
}
}
}
}

public void RunStarted(ITestAdaptor testsToRun)
{
_tmpStackTraceLogType = Application.GetStackTraceLogType(LogType.Log);
Expand All @@ -33,13 +59,22 @@ public void RunStarted(ITestAdaptor testsToRun)

public void RunFinished(ITestResultAdaptor result)
{
Debug.Log($"{LogPrefix}Edit Mode Tests Finished.");
Debug.Log($"{LogPrefix}Passed: {result.PassCount}, Skipped: {result.SkipCount}, Failed: {result.FailCount}");
Debug.Log($"{ResultLogPrefix}{result.FailCount}");

Application.SetStackTraceLogType(LogType.Log, _tmpStackTraceLogType);

if (Application.isBatchMode)
{
if (!string.IsNullOrEmpty(_xmlFilePath))
{
Debug.Log($"{LogPrefix}Write NUnit XML to {_xmlFilePath}");
var xmlNode = CreateNUnitXmlTree(result);
using var xmlWriter = new XmlTextWriter(_xmlFilePath, Encoding.UTF8);
xmlWriter.Formatting = Formatting.Indented;
xmlNode.WriteTo(xmlWriter);
}
EditorApplication.Exit(result.FailCount > 0 ? 1 : 0);
}
}
Expand All @@ -58,6 +93,32 @@ public void TestFinished(ITestResultAdaptor result)
Debug.Log($"{LogPrefix}{result.StackTrace}");
}
}

/// <summary>
/// https://forum.unity.com/threads/generating-nunit-compatible-xml-output.769757/
/// </summary>
private static TNode CreateNUnitXmlTree(ITestResultAdaptor result)
{
var testRunNode = new TNode("test-run");
testRunNode.AddAttribute("id", "2");
testRunNode.AddAttribute("testcasecount", (result.PassCount + result.FailCount + result.SkipCount + result.InconclusiveCount).ToString());
testRunNode.AddAttribute("result", result.ResultState);
testRunNode.AddAttribute("total", (result.PassCount + result.FailCount + result.SkipCount + result.InconclusiveCount).ToString());
testRunNode.AddAttribute("passed", result.PassCount.ToString());
testRunNode.AddAttribute("failed", result.FailCount.ToString());
testRunNode.AddAttribute("inconclusive", result.InconclusiveCount.ToString());
testRunNode.AddAttribute("skipped", result.SkipCount.ToString());
testRunNode.AddAttribute("asserts", result.AssertCount.ToString());
testRunNode.AddAttribute("engine-version", "3.5.0.0");
testRunNode.AddAttribute("clr-version", System.Environment.Version.ToString());
testRunNode.AddAttribute("start-time", result.StartTime.ToString("u"));
testRunNode.AddAttribute("end-time", result.EndTime.ToString("u"));
testRunNode.AddAttribute("duration", result.Duration.ToString(CultureInfo.InvariantCulture));

testRunNode.ChildNodes.Add(result.ToXml());

return testRunNode;
}
}
}
}

0 comments on commit 409682a

Please sign in to comment.