Enabling Gitlab Tests Reports with Unity Test Runner results
Here is a step-by-step to activate the gitlab test report.
1- gitlab uses JUnit reports and unity test runner uses NUnit, so we'll have to convert from one to the other.
-
we need to retrieve nunit3-junit.xslt file from here : https://github.com/nunit/nunit-transforms/tree/master/nunit3-junit
-
and create a powershell script, let's call it "nunit2junit.ps1':
Param(
[string]$nunit,
[string]$junit
)
$xml = Resolve-Path $nunit
$output = Join-Path ($pwd) $junit
$xslt = New-Object System.Xml.Xsl.XslCompiledTransform;
$xsltpath = Join-Path $PSScriptRoot "nunit3-junit.xslt"
$xslt.Load($xsltpath);
$xslt.Transform($xml, $output);
now, from our .yml pipeline : note that our unity project is at Path ./Unity in this example (well it's always the cas in our studio anyway)
# inside test stage
script:
- # typical call to unity.exe cli to call tests with arg: -testResults $TESTS_RESULTS
after_script: # call it here to convert even if the script step failed due to test failure
- powershell -File ci/nunit2junit.ps1 -nunit "./Unity/$TESTS_RESULTS" -junit "/Unity/junit-$TESTS_RESULTS"
artifacts:
when: always #required to report when test fails
paths:
- ./Unity/$TESTS_RESULTS #optional
- ./Unity/junit-$TESTS_RESULTS #optional
- $LOG_FILE # optional
reports:
junit:
- ./Unity/junit-$TESTS_RESULTS #required
expire_in: 1 week
2- also the debug.log from unity seems to interfer with the xml report, so we'll have to disable logs during test run.
this can be done using a SetupFixture with OneTImeSetup and OneTimeTeardown just add a .cs file in global namespace
using UnityEngine;
using NUnit.Framework;
[SetUpFixture]
public class TestInitializerInNoNamespace
{
[OneTimeSetUp]
public void Setup()
{
Debug.unityLogger.logEnabled = false;
}
[OneTimeTearDown]
public void Teardown()
{
Debug.unityLogger.logEnabled = true;
}
}
It's working well so far, may have to tweak a few things later on.