-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #176 from Algoryx/fix/unit-test-setup
Update unit test setup for better compatibility with CI pipeline
- Loading branch information
Showing
13 changed files
with
218 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters