Skip to content

Instantly share code, notes, and snippets.

@jrreed
Last active August 18, 2024 08:23
Show Gist options
  • Save jrreed/f42938492dcf77fe1a4e43ecf2d62bc3 to your computer and use it in GitHub Desktop.
Save jrreed/f42938492dcf77fe1a4e43ecf2d62bc3 to your computer and use it in GitHub Desktop.
Convert Unity test results XML file into proper NUnit format
#!/usr/bin/env bash
#
# Usage: ./fix-unity-test-results.sh
#
# Unity uses NUnit under the hood for its test framework. Their command line tool outputs a NUnit
# test result XML file. See documentation:
# * https://github.com/nunit/docs/wiki/Test-Result-XML-Format#test-suite
#
# The format of this file is currently incorrect. The root element for NUnit XML test results files
# should be a '<test-run>' tag, which is missing from Unity's test results file. See issue on forum:
# * https://forum.unity3d.com/threads/unity-test-tools.218287/page-6#post-3188590
#
# This script uses the XMLStarlet command line tool and 'script/build/fix-unity-test-results.xslt'
# to convert the incorrect test results XML file into the correct format. This allows the Jenkins
# NUnit plugin to convert the NUnit test results file into a JUnit test results file so Jenkins can
# understand and record test results.
#
set -euo pipefail
xml tr fix-unity-test-results.xslt unity-test-results.xml > nunit-test-results.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes" cdata-section-elements="message"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<test-run id="2" engine-version="3.0" clr-version="4.0">
<xsl:copy-of select="(test-suite/@*)[contains('|testcasecount|result|start-time|end-time|duration|total|passed|failed|inconclusive|skipped|asserts|', concat('|', name(), '|'))]" />
<xsl:apply-templates select="@*|node()"/>
</test-run>
</xsl:template>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
@jrreed
Copy link
Author

jrreed commented Aug 18, 2017

Script assumptions:

  • the script file is in root project directory
  • the XSLT file is in root project directory
  • the Unity test resuls XML file is named unity-test-results.xml and in root project directory

You can change the locations of these files to whatever you want, you just need to update the shell script.

We keep the XSLT file and script under script/build/....

When executing tests from the command line we output the results file to build/tests/...

@jrreed
Copy link
Author

jrreed commented Aug 31, 2017

@trungnhm1998
Copy link

trungnhm1998 commented Aug 18, 2024

Been awhile but, I want to mention that with the current version I'm using 1.4.5 the root tag has been corrected to <test-run>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment