Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update unit test setup for better compatibility with CI pipeline #176

Merged
merged 8 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 4 additions & 9 deletions Tests/Editor/AGXUnityEditorTests.asmdef
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
{
"name": "AGXUnityEditorTests",
"name": "AGXUnity.Editor.Tests",
"rootNamespace": "",
"references": [
"UnityEngine.TestRunner",
"UnityEditor.TestRunner",
"AGXUnity",
"AGXUnityEditor",
"TestingCommon"
],
"optionalUnityReferences": [
"TestAssemblies"
],
"includePlatforms": [
"Editor"
],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": true,
"precompiledReferences": [
"nunit.framework.dll"
],
"autoReferenced": false,
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
],
"versionDefines": [],
"noEngineReferences": false
}
23 changes: 23 additions & 0 deletions Tests/Editor/DisableLoggingFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using NUnit.Framework;
using UnityEngine;

/// <summary>
/// Unity adds it's log messages to the test results. To have the results be usable by external tools
/// we therefore disable logging while running tests.
/// </summary>
[SetUpFixture]
public class DisableLoggingFixture
{
[OneTimeSetUp]
public void Setup()
{
Debug.unityLogger.logEnabled = false;
}

[OneTimeTearDown]
public void Teardown()
{
Debug.unityLogger.logEnabled = true;
}
}

11 changes: 11 additions & 0 deletions Tests/Editor/DisableLoggingFixture.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Tests/Editor/EditorMetadataTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

using Assembly = System.Reflection.Assembly;

namespace AGXUnityTesting
namespace AGXUnityTesting.Editor
{
public class EditorMetadataTests
{
Expand Down
39 changes: 39 additions & 0 deletions Tests/Editor/SetupPlaymodeTestPlayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using AGXUnity.IO;
using System.IO;
using System.Linq;
using UnityEditor;
using UnityEditor.TestTools;
using UnityEngine.TestTools;

[assembly: TestPlayerBuildModifier( typeof( SetupPlaymodeTestPlayer ) )]
[assembly: PostBuildCleanup( typeof( SetupPlaymodeTestPlayer ) )]

public class SetupPlaymodeTestPlayer : ITestPlayerBuildModifier, IPostBuildCleanup
{
const string CLI_ARG = "onlyBuildTestsTo";

public BuildPlayerOptions ModifyOptions( BuildPlayerOptions playerOptions )
{
var CLI = Environment.CommandLine;
if ( CLI.HasArg( CLI_ARG ) ) {
playerOptions.options &= ~( BuildOptions.AutoRunPlayer | BuildOptions.ConnectToHost );
string path = (CLI.GetValues(CLI_ARG) ? [0]) ?? "TestPlayers";
var buildLocation = Path.GetFullPath( path );
var fileName = Path.GetFileName(playerOptions.locationPathName);
if ( !string.IsNullOrEmpty( fileName ) )
buildLocation = Path.Combine( buildLocation, fileName );
playerOptions.locationPathName = buildLocation;
}

var scenes = playerOptions.scenes.Where( s => s.StartsWith("Assets/InitTestScene"));

playerOptions.scenes = scenes.ToArray();
return playerOptions;
}

public void Cleanup()
{
if ( Environment.CommandLine.HasArg( CLI_ARG ) )
EditorApplication.Exit( 0 );
}
}
11 changes: 11 additions & 0 deletions Tests/Editor/SetupPlaymodeTestPlayer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Tests/Editor/TerrainGetSetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using NUnit.Framework;
using UnityEngine;

namespace AGXUnityTesting
namespace AGXUnityTesting.Editor
{
public class TerrainGetSetTests
{
Expand Down
13 changes: 4 additions & 9 deletions Tests/Runtime/AGXUnityTests.asmdef
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "AGXUnityTests",
"name": "AGXUnity.Tests",
"rootNamespace": "",
"references": [
"UnityEngine.TestRunner",
"UnityEditor.TestRunner",
"AGXUnity",
"TestingCommon"
],
"optionalUnityReferences": [
"TestAssemblies"
],
"includePlatforms": [
"Editor",
"LinuxStandalone64",
Expand All @@ -15,13 +16,7 @@
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [
"nunit.framework.dll"
],
"autoReferenced": false,
"defineConstraints": [
"UNITY_INCLUDE_TESTS"
],
"versionDefines": [],
"noEngineReferences": false
}
23 changes: 23 additions & 0 deletions Tests/Runtime/DisableLoggingFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using NUnit.Framework;
using UnityEngine;

/// <summary>
/// Unity adds it's log messages to the test results. To have the results be usable by external tools
/// we therefore disable logging while running tests.
/// </summary>
[SetUpFixture]
public class DisableLoggingFixture
{
[OneTimeSetUp]
public void Setup()
{
Debug.unityLogger.logEnabled = false;
}

[OneTimeTearDown]
public void Teardown()
{
Debug.unityLogger.logEnabled = true;
}
}

11 changes: 11 additions & 0 deletions Tests/Runtime/DisableLoggingFixture.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

77 changes: 77 additions & 0 deletions Tests/Runtime/ResultSerializer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
using AGXUnity.IO;
using NUnit.Framework.Interfaces;
using System.IO;
using System.Xml;
using UnityEngine;
using UnityEngine.TestRunner;

[assembly: TestRunCallback( typeof( ResultSerializer ) )]

public class ResultSerializer : ITestRunCallback
{
public void RunStarted( ITest testsToRun ) { }
public void TestFinished( ITestResult result ) { }
public void TestStarted( ITest test ) { }

public void RunFinished( ITestResult testResults )
{
if ( Environment.CommandLine.HasArg( "testResults" ) ) {
var path = Environment.CommandLine.GetValues( "testResults" )?[ 0 ] ?? "results.xml";
var fullPath = Path.GetFullPath(path);
using ( var xmlWriter = XmlWriter.Create( fullPath, new XmlWriterSettings { Indent = true } ) )
WriteResultsToXml( testResults, xmlWriter );

System.Console.WriteLine( $"\n Test results written to: {fullPath}\n" );
}
Application.Quit( testResults.FailCount > 0 ? 1 : 0 );
}

// The code below this point is taken and modified from the UnityEditor.TestTools.TestRunner.Api.ResultsWriter class to
// output valid NUnit reports
private const string k_nUnitVersion = "3.5.0.0";

private const string k_TestRunNode = "test-run";
private const string k_Id = "id";
private const string k_Testcasecount = "testcasecount";
private const string k_Result = "result";
private const string k_Total = "total";
private const string k_Passed = "passed";
private const string k_Failed = "failed";
private const string k_Inconclusive = "inconclusive";
private const string k_Skipped = "skipped";
private const string k_Asserts = "asserts";
private const string k_EngineVersion = "engine-version";
private const string k_ClrVersion = "clr-version";
private const string k_StartTime = "start-time";
private const string k_EndTime = "end-time";
private const string k_Duration = "duration";

private const string k_TimeFormat = "u";

private void WriteResultsToXml( ITestResult result, XmlWriter xmlWriter )
{
// XML format as specified at https://github.com/nunit/docs/wiki/Test-Result-XML-Format

var testRunNode = new TNode(k_TestRunNode);

testRunNode.AddAttribute( k_Id, "2" );
testRunNode.AddAttribute( k_Testcasecount, ( result.PassCount + result.FailCount + result.SkipCount + result.InconclusiveCount ).ToString() );
testRunNode.AddAttribute( k_Result, result.ResultState.Label );
testRunNode.AddAttribute( k_Total, ( result.PassCount + result.FailCount + result.SkipCount + result.InconclusiveCount ).ToString() );
testRunNode.AddAttribute( k_Passed, result.PassCount.ToString() );
testRunNode.AddAttribute( k_Failed, result.FailCount.ToString() );
testRunNode.AddAttribute( k_Inconclusive, result.InconclusiveCount.ToString() );
testRunNode.AddAttribute( k_Skipped, result.SkipCount.ToString() );
testRunNode.AddAttribute( k_Asserts, result.AssertCount.ToString() );
testRunNode.AddAttribute( k_EngineVersion, k_nUnitVersion );
testRunNode.AddAttribute( k_ClrVersion, System.Environment.Version.ToString() );
testRunNode.AddAttribute( k_StartTime, result.StartTime.ToString( k_TimeFormat ) );
testRunNode.AddAttribute( k_EndTime, result.EndTime.ToString( k_TimeFormat ) );
testRunNode.AddAttribute( k_Duration, result.Duration.ToString() );

var resultNode = result.ToXml( true );
testRunNode.ChildNodes.Add( resultNode );

testRunNode.WriteTo( xmlWriter );
}
}
11 changes: 11 additions & 0 deletions Tests/Runtime/ResultSerializer.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Tests/Runtime/TerrainGetSetTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
using AGXUnity.Model;

using NUnit.Framework;
using System.Collections;
using UnityEngine;
using UnityEngine.TestTools;

namespace AGXUnityTesting
namespace AGXUnityTesting.Runtime
{
public class TerrainGetSetTests
{
Expand Down
Loading