Initial Commit

This commit is contained in:
Sebastian Cabrera 2021-08-02 05:44:37 -04:00
parent 53eb92e9af
commit 270ab7d11f
15341 changed files with 700234 additions and 0 deletions

View file

@ -0,0 +1,359 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Commands;
using NUnit.Framework.Internal.Execution;
using UnityEngine.TestTools.Logging;
using UnityEngine.TestTools.TestRunner;
namespace UnityEngine.TestRunner.NUnitExtensions.Runner
{
internal class CompositeWorkItem : UnityWorkItem
{
private readonly TestSuite _suite;
private readonly TestSuiteResult _suiteResult;
private readonly ITestFilter _childFilter;
private TestCommand _setupCommand;
private TestCommand _teardownCommand;
public List<UnityWorkItem> Children { get; private set; }
private int _countOrder;
private CountdownEvent _childTestCountdown;
public CompositeWorkItem(TestSuite suite, ITestFilter childFilter, WorkItemFactory factory)
: base(suite, factory)
{
_suite = suite;
_suiteResult = Result as TestSuiteResult;
_childFilter = childFilter;
_countOrder = 0;
}
protected override IEnumerable PerformWork()
{
InitializeSetUpAndTearDownCommands();
if (UnityTestExecutionContext.CurrentContext != null && m_DontRunRestoringResult && EditModeTestCallbacks.RestoringTestContext != null)
{
EditModeTestCallbacks.RestoringTestContext();
}
if (!CheckForCancellation())
if (Test.RunState == RunState.Explicit && !_childFilter.IsExplicitMatch(Test))
SkipFixture(ResultState.Explicit, GetSkipReason(), null);
else
switch (Test.RunState)
{
default:
case RunState.Runnable:
case RunState.Explicit:
Result.SetResult(ResultState.Success);
CreateChildWorkItems();
if (Children.Count > 0)
{
if (!m_DontRunRestoringResult)
{
//This is needed to give the editor a chance to go out of playmode if needed before creating objects.
//If we do not, the objects could be automatically destroyed when exiting playmode and could result in errors later on
yield return null;
PerformOneTimeSetUp();
}
if (!CheckForCancellation())
{
switch (Result.ResultState.Status)
{
case TestStatus.Passed:
foreach (var child in RunChildren())
{
if (CheckForCancellation())
{
yield break;
}
yield return child;
}
break;
case TestStatus.Skipped:
case TestStatus.Inconclusive:
case TestStatus.Failed:
SkipChildren(_suite, Result.ResultState.WithSite(FailureSite.Parent), "OneTimeSetUp: " + Result.Message);
break;
}
}
if (Context.ExecutionStatus != TestExecutionStatus.AbortRequested && !m_DontRunRestoringResult)
{
PerformOneTimeTearDown();
}
}
break;
case RunState.Skipped:
SkipFixture(ResultState.Skipped, GetSkipReason(), null);
break;
case RunState.Ignored:
SkipFixture(ResultState.Ignored, GetSkipReason(), null);
break;
case RunState.NotRunnable:
SkipFixture(ResultState.NotRunnable, GetSkipReason(), GetProviderStackTrace());
break;
}
if (!ResultedInDomainReload)
{
WorkItemComplete();
}
}
private bool CheckForCancellation()
{
if (Context.ExecutionStatus != TestExecutionStatus.Running)
{
Result.SetResult(ResultState.Cancelled, "Test cancelled by user");
return true;
}
return false;
}
private void InitializeSetUpAndTearDownCommands()
{
List<SetUpTearDownItem> setUpTearDownItems = _suite.TypeInfo != null
? CommandBuilder.BuildSetUpTearDownList(_suite.TypeInfo.Type, typeof(OneTimeSetUpAttribute), typeof(OneTimeTearDownAttribute))
: new List<SetUpTearDownItem>();
var actionItems = new List<TestActionItem>();
foreach (ITestAction action in Actions)
{
bool applyToSuite = (action.Targets & ActionTargets.Suite) == ActionTargets.Suite
|| action.Targets == ActionTargets.Default && !(Test is ParameterizedMethodSuite);
bool applyToTest = (action.Targets & ActionTargets.Test) == ActionTargets.Test
&& !(Test is ParameterizedMethodSuite);
if (applyToSuite)
actionItems.Add(new TestActionItem(action));
if (applyToTest)
Context.UpstreamActions.Add(action);
}
_setupCommand = CommandBuilder.MakeOneTimeSetUpCommand(_suite, setUpTearDownItems, actionItems);
_teardownCommand = CommandBuilder.MakeOneTimeTearDownCommand(_suite, setUpTearDownItems, actionItems);
}
private void PerformOneTimeSetUp()
{
var logScope = new LogScope();
try
{
_setupCommand.Execute(Context);
}
catch (Exception ex)
{
if (ex is NUnitException || ex is TargetInvocationException)
ex = ex.InnerException;
Result.RecordException(ex, FailureSite.SetUp);
}
if (logScope.AnyFailingLogs())
{
Result.RecordException(new UnhandledLogMessageException(logScope.FailingLogs.First()));
}
logScope.Dispose();
}
private IEnumerable RunChildren()
{
int childCount = Children.Count;
if (childCount == 0)
throw new InvalidOperationException("RunChildren called but item has no children");
_childTestCountdown = new CountdownEvent(childCount);
foreach (UnityWorkItem child in Children)
{
if (CheckForCancellation())
{
yield break;
}
var unityTestExecutionContext = new UnityTestExecutionContext(Context);
child.InitializeContext(unityTestExecutionContext);
var enumerable = child.Execute().GetEnumerator();
while (true)
{
if (!enumerable.MoveNext())
{
break;
}
ResultedInDomainReload |= child.ResultedInDomainReload;
yield return enumerable.Current;
}
_suiteResult.AddResult(child.Result);
childCount--;
}
if (childCount > 0)
{
while (childCount-- > 0)
CountDownChildTest();
}
}
private void CreateChildWorkItems()
{
Children = new List<UnityWorkItem>();
var testSuite = _suite;
foreach (ITest test in testSuite.Tests)
{
if (_childFilter.Pass(test))
{
var child = m_Factory.Create(test, _childFilter);
if (test.Properties.ContainsKey(PropertyNames.Order))
{
Children.Insert(0, child);
_countOrder++;
}
else
{
Children.Add(child);
}
}
}
if (_countOrder != 0) SortChildren();
}
private class UnityWorkItemOrderComparer : IComparer<UnityWorkItem>
{
public int Compare(UnityWorkItem x, UnityWorkItem y)
{
var xKey = int.MaxValue;
var yKey = int.MaxValue;
if (x.Test.Properties.ContainsKey(PropertyNames.Order))
xKey = (int)x.Test.Properties[PropertyNames.Order][0];
if (y.Test.Properties.ContainsKey(PropertyNames.Order))
yKey = (int)y.Test.Properties[PropertyNames.Order][0];
return xKey.CompareTo(yKey);
}
}
private void SortChildren()
{
Children.Sort(0, _countOrder, new UnityWorkItemOrderComparer());
}
private void SkipFixture(ResultState resultState, string message, string stackTrace)
{
Result.SetResult(resultState.WithSite(FailureSite.SetUp), message, StackFilter.Filter(stackTrace));
SkipChildren(_suite, resultState.WithSite(FailureSite.Parent), "OneTimeSetUp: " + message);
}
private void SkipChildren(TestSuite suite, ResultState resultState, string message)
{
foreach (Test child in suite.Tests)
{
if (_childFilter.Pass(child))
{
Context.Listener.TestStarted(child);
TestResult childResult = child.MakeTestResult();
childResult.SetResult(resultState, message);
_suiteResult.AddResult(childResult);
if (child.IsSuite)
SkipChildren((TestSuite)child, resultState, message);
Context.Listener.TestFinished(childResult);
}
}
}
private void PerformOneTimeTearDown()
{
var logScope = new LogScope();
try
{
_teardownCommand.Execute(Context);
}
catch (Exception ex)
{
if (ex is NUnitException || ex is TargetInvocationException)
ex = ex.InnerException;
Result.RecordException(ex, FailureSite.SetUp);
}
if (logScope.AnyFailingLogs())
{
Result.RecordException(new UnhandledLogMessageException(logScope.FailingLogs.First()));
}
logScope.Dispose();
}
private string GetSkipReason()
{
return (string)Test.Properties.Get(PropertyNames.SkipReason);
}
private string GetProviderStackTrace()
{
return (string)Test.Properties.Get(PropertyNames.ProviderStackTrace);
}
private void CountDownChildTest()
{
_childTestCountdown.Signal();
if (_childTestCountdown.CurrentCount == 0)
{
if (Context.ExecutionStatus != TestExecutionStatus.AbortRequested)
PerformOneTimeTearDown();
foreach (var childResult in _suiteResult.Children)
if (childResult.ResultState == ResultState.Cancelled)
{
this.Result.SetResult(ResultState.Cancelled, "Cancelled by user");
break;
}
WorkItemComplete();
}
}
public override void Cancel(bool force)
{
if (Children == null)
return;
foreach (var child in Children)
{
var ctx = child.Context;
if (ctx != null)
ctx.ExecutionStatus = force ? TestExecutionStatus.AbortRequested : TestExecutionStatus.StopRequested;
if (child.State == WorkItemState.Running)
child.Cancel(force);
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 110d5035a36a6a34580fb65bb40cd78f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,76 @@
using System;
using System.Collections;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Commands;
using NUnit.Framework.Internal.Execution;
using UnityEngine.TestTools.TestRunner;
using UnityEngine.TestTools.Utils;
namespace UnityEngine.TestRunner.NUnitExtensions.Runner
{
internal class CoroutineTestWorkItem : UnityWorkItem
{
private static MonoBehaviour m_MonoBehaviourCoroutineRunner;
private TestCommand m_Command;
public static MonoBehaviour monoBehaviourCoroutineRunner
{
get
{
if (m_MonoBehaviourCoroutineRunner == null)
{
throw new NullReferenceException("MonoBehaviour coroutine runner not set");
}
return m_MonoBehaviourCoroutineRunner;
}
set { m_MonoBehaviourCoroutineRunner = value; }
}
public CoroutineTestWorkItem(TestMethod test, ITestFilter filter)
: base(test, null)
{
m_Command = m_Command = TestCommandBuilder.BuildTestCommand(test, filter);
}
protected override IEnumerable PerformWork()
{
if (m_Command is SkipCommand)
{
m_Command.Execute(Context);
Result = Context.CurrentResult;
WorkItemComplete();
yield break;
}
if (m_Command is ApplyChangesToContextCommand)
{
var applyChangesToContextCommand = (ApplyChangesToContextCommand)m_Command;
applyChangesToContextCommand.ApplyChanges(Context);
m_Command = applyChangesToContextCommand.GetInnerCommand();
}
var enumerableTestMethodCommand = (IEnumerableTestMethodCommand)m_Command;
try
{
var executeEnumerable = enumerableTestMethodCommand.ExecuteEnumerable(Context).GetEnumerator();
var coroutineRunner = new CoroutineRunner(monoBehaviourCoroutineRunner, Context);
yield return coroutineRunner.HandleEnumerableTest(executeEnumerable);
if (coroutineRunner.HasFailedWithTimeout())
{
Context.CurrentResult.SetResult(ResultState.Failure, new UnityTestTimeoutException(Context.TestCaseTimeout).Message);
}
while (executeEnumerable.MoveNext()) {}
Result = Context.CurrentResult;
}
finally
{
WorkItemComplete();
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b557515fff172984e8c4400b43f1c631
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,98 @@
using System;
using System.Collections;
using System.Linq;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Commands;
using NUnit.Framework.Internal.Execution;
using UnityEngine.TestTools;
using SetUpTearDownCommand = NUnit.Framework.Internal.Commands.SetUpTearDownCommand;
using TestActionCommand = NUnit.Framework.Internal.Commands.TestActionCommand;
namespace UnityEngine.TestRunner.NUnitExtensions.Runner
{
internal class EditModeTestCallbacks
{
public static Action RestoringTestContext { get; set; }
}
internal class DefaultTestWorkItem : UnityWorkItem
{
private TestCommand _command;
public DefaultTestWorkItem(TestMethod test, ITestFilter filter)
: base(test, null)
{
_command = TestCommandBuilder.BuildTestCommand(test, filter);
}
protected override IEnumerable PerformWork()
{
if (m_DontRunRestoringResult && EditModeTestCallbacks.RestoringTestContext != null)
{
EditModeTestCallbacks.RestoringTestContext();
Result = Context.CurrentResult;
yield break;
}
try
{
if (_command is SkipCommand || _command is FailCommand)
{
Result = _command.Execute(Context);
yield break;
}
if (!(_command is IEnumerableTestMethodCommand))
{
Debug.LogError("Cannot perform work on " + _command.GetType().Name);
yield break;
}
if (Context.TestCaseTimeout == 0)
{
Context.TestCaseTimeout = k_DefaultTimeout;
}
foreach (var workItemStep in ((IEnumerableTestMethodCommand)_command).ExecuteEnumerable(Context))
{
ResultedInDomainReload = false;
if (workItemStep is IEditModeTestYieldInstruction)
{
var editModeTestYieldInstruction = (IEditModeTestYieldInstruction)workItemStep;
yield return editModeTestYieldInstruction;
var enumerator = editModeTestYieldInstruction.Perform();
while (true)
{
bool moveNext;
try
{
moveNext = enumerator.MoveNext();
}
catch (Exception e)
{
Context.CurrentResult.RecordException(e);
break;
}
if (!moveNext)
{
break;
}
yield return null;
}
}
else
{
yield return workItemStep;
}
}
Result = Context.CurrentResult;
}
finally
{
WorkItemComplete();
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c7cfda246e604b945b12b7afedb094ce
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,34 @@
using System.Collections;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Commands;
namespace UnityEngine.TestRunner.NUnitExtensions.Runner
{
internal class FailCommand : TestCommand, IEnumerableTestMethodCommand
{
private ResultState m_ResultState;
private string m_Message;
public FailCommand(Test test, ResultState resultState, string message)
: base(test)
{
m_ResultState = resultState;
m_Message = message;
}
public override TestResult Execute(ITestExecutionContext context)
{
context.CurrentResult.SetResult(m_ResultState, m_Message);
return context.CurrentResult;
}
public IEnumerable ExecuteEnumerable(ITestExecutionContext context)
{
context.CurrentResult.SetResult(m_ResultState, m_Message);
yield return null;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 68e5dc8bfd5d72647a93b7f2e1da831a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,10 @@
using System.Collections;
using NUnit.Framework.Internal;
namespace UnityEngine.TestRunner.NUnitExtensions.Runner
{
internal interface IEnumerableTestMethodCommand
{
IEnumerable ExecuteEnumerable(ITestExecutionContext context);
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: dbd43d8a3b8122d4e89b055f53382b11
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,13 @@
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
namespace UnityEngine.TestRunner.NUnitExtensions.Runner
{
internal class PlaymodeWorkItemFactory : WorkItemFactory
{
protected override UnityWorkItem Create(TestMethod method, ITestFilter filter, ITest loadedTest)
{
return new CoroutineTestWorkItem(method, filter);
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 7ef6801a8b664544aa9f2ab1bc1f8b60
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,4 @@
namespace UnityEngine.TestRunner.NUnitExtensions.Runner
{
internal class RestoreTestContextAfterDomainReload {}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 26721f9940339264fb14bdbfe1290e21
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,127 @@
using System;
using System.Collections;
using System.Linq;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Commands;
using UnityEngine.TestTools;
namespace UnityEngine.TestRunner.NUnitExtensions.Runner
{
internal static class TestCommandBuilder
{
public static TestCommand BuildTestCommand(TestMethod test, ITestFilter filter)
{
if (test.RunState != RunState.Runnable &&
!(test.RunState == RunState.Explicit && filter.IsExplicitMatch(test)))
{
return new SkipCommand(test);
}
var testReturnsIEnumerator = test.Method.ReturnType.Type == typeof(IEnumerator);
TestCommand command;
if (!testReturnsIEnumerator)
{
command = new UnityTestMethodCommand(test);
}
else
{
command = new EnumerableTestMethodCommand(test);
}
command = new UnityLogCheckDelegatingCommand(command);
foreach (var wrapper in test.Method.GetCustomAttributes<IWrapTestMethod>(true))
{
command = wrapper.Wrap(command);
if (command == null)
{
var message = String.Format("IWrapTestMethod implementation '{0}' returned null as command.",
wrapper.GetType().FullName);
return new FailCommand(test, ResultState.Failure, message);
}
if (testReturnsIEnumerator && !(command is IEnumerableTestMethodCommand))
{
command = TryReplaceWithEnumerableCommand(command);
if (command != null)
{
continue;
}
var message = String.Format("'{0}' is not supported on {1} as it does not handle returning IEnumerator.",
wrapper.GetType().FullName,
GetTestBuilderName(test));
return new FailCommand(test, ResultState.Failure, message);
}
}
command = new UnityEngine.TestTools.TestActionCommand(command);
command = new UnityEngine.TestTools.SetUpTearDownCommand(command);
if (!testReturnsIEnumerator)
{
command = new ImmediateEnumerableCommand(command);
}
foreach (var wrapper in test.Method.GetCustomAttributes<IWrapSetUpTearDown>(true))
{
command = wrapper.Wrap(command);
if (command == null)
{
var message = String.Format("IWrapSetUpTearDown implementation '{0}' returned null as command.",
wrapper.GetType().FullName);
return new FailCommand(test, ResultState.Failure, message);
}
if (testReturnsIEnumerator && !(command is IEnumerableTestMethodCommand))
{
command = TryReplaceWithEnumerableCommand(command);
if (command != null)
{
continue;
}
var message = String.Format("'{0}' is not supported on {1} as it does not handle returning IEnumerator.",
wrapper.GetType().FullName,
GetTestBuilderName(test));
return new FailCommand(test, ResultState.Failure, message);
}
}
command = new EnumerableSetUpTearDownCommand(command);
command = new OuterUnityTestActionCommand(command);
IApplyToContext[] changes = test.Method.GetCustomAttributes<IApplyToContext>(true);
if (changes.Length > 0)
{
command = new EnumerableApplyChangesToContextCommand(command, changes);
}
return command;
}
private static string GetTestBuilderName(TestMethod testMethod)
{
return new[]
{
testMethod.Method.GetCustomAttributes<ITestBuilder>(true).Select(attribute => attribute.GetType().Name),
testMethod.Method.GetCustomAttributes<ISimpleTestBuilder>(true).Select(attribute => attribute.GetType().Name)
}.SelectMany(v => v).FirstOrDefault();
}
private static TestCommand TryReplaceWithEnumerableCommand(TestCommand command)
{
switch (command.GetType().Name)
{
case nameof(RepeatAttribute.RepeatedTestCommand):
return new EnumerableRepeatedTestCommand(command as RepeatAttribute.RepeatedTestCommand);
case nameof(RetryAttribute.RetryCommand):
return new EnumerableRetryTestCommand(command as RetryAttribute.RetryCommand);
default:
return null;
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f40df9c8cf926b241b093a37028d8815
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,143 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Commands;
using UnityEngine.TestTools;
using UnityEngine.TestTools.Logging;
using UnityEngine.TestTools.TestRunner;
namespace UnityEngine.TestRunner.NUnitExtensions.Runner
{
class UnityLogCheckDelegatingCommand : DelegatingTestCommand, IEnumerableTestMethodCommand
{
static Dictionary<object, bool?> s_AttributeCache = new Dictionary<object, bool?>();
public UnityLogCheckDelegatingCommand(TestCommand innerCommand)
: base(innerCommand) {}
public override TestResult Execute(ITestExecutionContext context)
{
using (var logScope = new LogScope())
{
if (ExecuteAndCheckLog(logScope, context.CurrentResult, () => innerCommand.Execute(context)))
PostTestValidation(logScope, innerCommand, context.CurrentResult);
}
return context.CurrentResult;
}
public IEnumerable ExecuteEnumerable(ITestExecutionContext context)
{
if (!(innerCommand is IEnumerableTestMethodCommand enumerableTestMethodCommand))
{
Execute(context);
yield break;
}
using (var logScope = new LogScope())
{
IEnumerable executeEnumerable = null;
if (!ExecuteAndCheckLog(logScope, context.CurrentResult,
() => executeEnumerable = enumerableTestMethodCommand.ExecuteEnumerable(context)))
yield break;
foreach (var step in executeEnumerable)
{
// do not check expected logs here - we want to permit expecting and receiving messages to run
// across frames. (but we do always want to catch a fail immediately.)
if (!CheckFailingLogs(logScope, context.CurrentResult))
yield break;
yield return step;
}
if (!CheckLogs(context.CurrentResult, logScope))
yield break;
PostTestValidation(logScope, innerCommand, context.CurrentResult);
}
}
static bool CaptureException(TestResult result, Action action)
{
try
{
action();
return true;
}
catch (Exception e)
{
result.RecordException(e);
return false;
}
}
static bool ExecuteAndCheckLog(LogScope logScope, TestResult result, Action action)
=> CaptureException(result, action) && CheckLogs(result, logScope);
static void PostTestValidation(LogScope logScope, TestCommand command, TestResult result)
{
if (MustExpect(command.Test.Method.MethodInfo))
CaptureException(result, logScope.NoUnexpectedReceived);
}
static bool CheckLogs(TestResult result, LogScope logScope)
=> CheckFailingLogs(logScope, result) && CheckExpectedLogs(logScope, result);
static bool CheckFailingLogs(LogScope logScope, TestResult result)
{
if (!logScope.AnyFailingLogs())
return true;
var failingLog = logScope.FailingLogs.First();
result.RecordException(new UnhandledLogMessageException(failingLog));
return false;
}
static bool CheckExpectedLogs(LogScope logScope, TestResult result)
{
if (!logScope.ExpectedLogs.Any())
return true;
var expectedLog = logScope.ExpectedLogs.Peek();
result.RecordException(new UnexpectedLogMessageException(expectedLog));
return false;
}
static bool MustExpect(MemberInfo method)
{
// method
var methodAttr = method.GetCustomAttributes<TestMustExpectAllLogsAttribute>(true).FirstOrDefault();
if (methodAttr != null)
return methodAttr.MustExpect;
// fixture
var fixture = method.DeclaringType;
if (!s_AttributeCache.TryGetValue(fixture, out var mustExpect))
{
var fixtureAttr = fixture.GetCustomAttributes<TestMustExpectAllLogsAttribute>(true).FirstOrDefault();
mustExpect = s_AttributeCache[fixture] = fixtureAttr?.MustExpect;
}
if (mustExpect != null)
return mustExpect.Value;
// assembly
var assembly = fixture.Assembly;
if (!s_AttributeCache.TryGetValue(assembly, out mustExpect))
{
var assemblyAttr = assembly.GetCustomAttributes<TestMustExpectAllLogsAttribute>().FirstOrDefault();
mustExpect = s_AttributeCache[assembly] = assemblyAttr?.MustExpect;
}
return mustExpect == true;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 48230e4e90fb4d14a9d56bddea898413
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,98 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NUnit;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using UnityEngine.TestTools;
using UnityEngine.TestTools.NUnitExtensions;
namespace UnityEngine.TestRunner.NUnitExtensions.Runner
{
internal interface IUnityTestAssemblyRunner
{
ITest LoadedTest { get; }
ITestResult Result { get; }
bool IsTestLoaded { get; }
bool IsTestRunning { get; }
bool IsTestComplete { get; }
UnityWorkItem TopLevelWorkItem { get; set; }
UnityTestExecutionContext GetCurrentContext();
ITest Load(Assembly[] assemblies, TestPlatform testPlatform, IDictionary<string, object> settings);
IEnumerable Run(ITestListener listener, ITestFilter filter);
void StopRun();
}
internal class UnityTestAssemblyRunner : IUnityTestAssemblyRunner
{
private readonly UnityTestAssemblyBuilder unityBuilder;
private readonly WorkItemFactory m_Factory;
protected UnityTestExecutionContext Context { get; set; }
public UnityTestExecutionContext GetCurrentContext()
{
return UnityTestExecutionContext.CurrentContext;
}
protected IDictionary<string, object> Settings { get; set; }
public ITest LoadedTest { get; protected set; }
public ITestResult Result
{
get { return TopLevelWorkItem == null ? null : TopLevelWorkItem.Result; }
}
public bool IsTestLoaded
{
get { return LoadedTest != null; }
}
public bool IsTestRunning
{
get { return TopLevelWorkItem != null && TopLevelWorkItem.State == NUnit.Framework.Internal.Execution.WorkItemState.Running; }
}
public bool IsTestComplete
{
get { return TopLevelWorkItem != null && TopLevelWorkItem.State == NUnit.Framework.Internal.Execution.WorkItemState.Complete; }
}
public UnityTestAssemblyRunner(UnityTestAssemblyBuilder builder, WorkItemFactory factory)
{
unityBuilder = builder;
m_Factory = factory;
Context = new UnityTestExecutionContext();
}
public ITest Load(Assembly[] assemblies, TestPlatform testPlatform, IDictionary<string, object> settings)
{
Settings = settings;
if (settings.ContainsKey(FrameworkPackageSettings.RandomSeed))
Randomizer.InitialSeed = (int)settings[FrameworkPackageSettings.RandomSeed];
return LoadedTest = unityBuilder.Build(assemblies, Enumerable.Repeat(testPlatform, assemblies.Length).ToArray(), settings);
}
public IEnumerable Run(ITestListener listener, ITestFilter filter)
{
TopLevelWorkItem = m_Factory.Create(LoadedTest, filter);
TopLevelWorkItem.InitializeContext(Context);
UnityTestExecutionContext.CurrentContext = Context;
Context.Listener = listener;
return TopLevelWorkItem.Execute();
}
public UnityWorkItem TopLevelWorkItem { get; set; }
public void StopRun()
{
if (IsTestRunning)
{
TopLevelWorkItem.Cancel(false);
}
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 874e40a588dbb1e48bc128d686337d4e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,130 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using NUnit.Framework;
using NUnit.Framework.Constraints;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Execution;
using UnityEngine.TestTools;
namespace UnityEngine.TestRunner.NUnitExtensions.Runner
{
internal class UnityTestExecutionContext : ITestExecutionContext
{
private readonly UnityTestExecutionContext _priorContext;
private TestResult _currentResult;
private int _assertCount;
public static UnityTestExecutionContext CurrentContext { get; set; }
public UnityTestExecutionContext Context { get; private set; }
public Test CurrentTest { get; set; }
public DateTime StartTime { get; set; }
public long StartTicks { get; set; }
public TestResult CurrentResult
{
get { return _currentResult; }
set
{
_currentResult = value;
if (value != null)
OutWriter = value.OutWriter;
}
}
public object TestObject { get; set; }
public string WorkDirectory { get; set; }
private TestExecutionStatus _executionStatus;
public TestExecutionStatus ExecutionStatus
{
get
{
// ExecutionStatus may have been set to StopRequested or AbortRequested
// in a prior context. If so, reflect the same setting in this context.
if (_executionStatus == TestExecutionStatus.Running && _priorContext != null)
_executionStatus = _priorContext.ExecutionStatus;
return _executionStatus;
}
set
{
_executionStatus = value;
// Push the same setting up to all prior contexts
if (_priorContext != null)
_priorContext.ExecutionStatus = value;
}
}
public List<ITestAction> UpstreamActions { get; private set; }
public int TestCaseTimeout { get; set; }
public CultureInfo CurrentCulture { get; set; }
public CultureInfo CurrentUICulture { get; set; }
public ITestListener Listener { get; set; }
public UnityTestExecutionContext()
{
UpstreamActions = new List<ITestAction>();
CurrentContext = this;
}
public UnityTestExecutionContext(UnityTestExecutionContext other)
{
_priorContext = other;
CurrentTest = other.CurrentTest;
CurrentResult = other.CurrentResult;
TestObject = other.TestObject;
WorkDirectory = other.WorkDirectory;
Listener = other.Listener;
TestCaseTimeout = other.TestCaseTimeout;
UpstreamActions = new List<ITestAction>(other.UpstreamActions);
SetUpTearDownState = other.SetUpTearDownState;
OuterUnityTestActionState = other.OuterUnityTestActionState;
EnumerableTestState = other.EnumerableTestState;
TestContext.CurrentTestExecutionContext = this;
CurrentCulture = other.CurrentCulture;
CurrentUICulture = other.CurrentUICulture;
CurrentContext = this;
}
public TextWriter OutWriter { get; private set; }
public bool StopOnError { get; set; }
public IWorkItemDispatcher Dispatcher { get; set; }
public ParallelScope ParallelScope { get; set; }
public string WorkerId { get; private set; }
public Randomizer RandomGenerator { get; private set; }
public ValueFormatter CurrentValueFormatter { get; private set; }
public bool IsSingleThreaded { get; set; }
public BeforeAfterTestCommandState SetUpTearDownState { get; set; }
public BeforeAfterTestCommandState OuterUnityTestActionState { get; set; }
public EnumerableTestState EnumerableTestState { get; set; }
internal int AssertCount
{
get
{
return _assertCount;
}
}
public void IncrementAssertCount()
{
_assertCount += 1;
}
public void AddFormatter(ValueFormatterFactory formatterFactory)
{
throw new NotImplementedException();
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 59ff995fabb3bac45afa0f96f333e5dc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,117 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using NUnit.Framework;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
using NUnit.Framework.Internal.Execution;
namespace UnityEngine.TestRunner.NUnitExtensions.Runner
{
internal abstract class UnityWorkItem
{
protected readonly WorkItemFactory m_Factory;
protected bool m_ExecuteTestStartEvent;
protected bool m_DontRunRestoringResult;
protected const int k_DefaultTimeout = 1000 * 180;
public event EventHandler Completed;
public bool ResultedInDomainReload { get; internal set; }
public UnityTestExecutionContext Context { get; private set; }
public Test Test { get; private set; }
public TestResult Result { get; protected set; }
public WorkItemState State { get; private set; }
public List<ITestAction> Actions { get; private set; }
protected UnityWorkItem(Test test, WorkItemFactory factory)
{
m_Factory = factory;
Test = test;
Actions = new List<ITestAction>();
Result = test.MakeTestResult();
State = WorkItemState.Ready;
m_ExecuteTestStartEvent = ShouldExecuteStartEvent();
m_DontRunRestoringResult = ShouldRestore(test);
}
protected static bool ShouldRestore(ITest loadedTest)
{
return UnityWorkItemDataHolder.alreadyExecutedTests != null &&
UnityWorkItemDataHolder.alreadyExecutedTests.Contains(loadedTest.GetUniqueName());
}
protected bool ShouldExecuteStartEvent()
{
return UnityWorkItemDataHolder.alreadyStartedTests != null &&
UnityWorkItemDataHolder.alreadyStartedTests.All(x => x != Test.GetUniqueName()) &&
!ShouldRestore(Test);
}
protected abstract IEnumerable PerformWork();
public void InitializeContext(UnityTestExecutionContext context)
{
Context = context;
if (Test is TestAssembly)
Actions.AddRange(ActionsHelper.GetActionsFromTestAssembly((TestAssembly)Test));
else if (Test is ParameterizedMethodSuite)
Actions.AddRange(ActionsHelper.GetActionsFromTestMethodInfo(Test.Method));
else if (Test.TypeInfo != null)
Actions.AddRange(ActionsHelper.GetActionsFromTypesAttributes(Test.TypeInfo.Type));
}
public virtual IEnumerable Execute()
{
Context.CurrentTest = this.Test;
Context.CurrentResult = this.Result;
if (m_ExecuteTestStartEvent)
{
Context.Listener.TestStarted(Test);
}
Context.StartTime = DateTime.UtcNow;
Context.StartTicks = Stopwatch.GetTimestamp();
State = WorkItemState.Running;
return PerformWork();
}
protected void WorkItemComplete()
{
State = WorkItemState.Complete;
Result.StartTime = Context.StartTime;
Result.EndTime = DateTime.UtcNow;
long tickCount = Stopwatch.GetTimestamp() - Context.StartTicks;
double seconds = (double)tickCount / Stopwatch.Frequency;
Result.Duration = seconds;
//Result.AssertCount += Context.AssertCount;
Context.Listener.TestFinished(Result);
if (Completed != null)
Completed(this, EventArgs.Empty);
Context.TestObject = null;
Test.Fixture = null;
}
public virtual void Cancel(bool force)
{
Result.SetResult(ResultState.Cancelled, "Cancelled by user");
Context.Listener.TestFinished(Result);
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 79ced2556f0af814a840b86232613ff1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,10 @@
using System.Collections.Generic;
namespace UnityEngine.TestRunner.NUnitExtensions.Runner
{
internal class UnityWorkItemDataHolder
{
public static List<string> alreadyStartedTests = new List<string>();
public static List<string> alreadyExecutedTests;
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5b3e90046c38f1d4dad2e0d5a79e871c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -0,0 +1,28 @@
using System.Collections;
using NUnit.Framework.Interfaces;
using NUnit.Framework.Internal;
namespace UnityEngine.TestRunner.NUnitExtensions.Runner
{
internal abstract class WorkItemFactory
{
public UnityWorkItem Create(ITest loadedTest, ITestFilter filter)
{
TestSuite suite = loadedTest as TestSuite;
if (suite != null)
{
return new CompositeWorkItem(suite, filter, this);
}
var testMethod = (TestMethod)loadedTest;
if (testMethod.Method.ReturnType.Type != typeof(IEnumerator))
{
return new DefaultTestWorkItem(testMethod, filter);
}
return Create(testMethod, filter, loadedTest);
}
protected abstract UnityWorkItem Create(TestMethod method, ITestFilter filter, ITest loadedTest);
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 5c15bf0966eb95847a4260d830a30d30
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: