inicial
This commit is contained in:
+86
@@ -0,0 +1,86 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using NUnit.Framework;
|
||||
using NUnit.Framework.Interfaces;
|
||||
using NUnit.Framework.Internal;
|
||||
|
||||
namespace UnityEngine.TestTools
|
||||
{
|
||||
/// <summary>
|
||||
/// This attribute is an alternative to the standard `Ignore` attribute in [NUnit](https://nunit.org/). It allows for ignoring tests only under a specified condition. The condition evaluates during `OnLoad`, referenced by ID.
|
||||
/// </summary>
|
||||
public class ConditionalIgnoreAttribute : NUnitAttribute, IApplyToTest
|
||||
{
|
||||
private string m_ConditionKey;
|
||||
private string m_IgnoreReason;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ConditionalIgnoreAttribute"/> class with a condition key.
|
||||
/// </summary>
|
||||
/// <param name="conditionKey">The key to check for enabling the conditional ignore. The condition is set with the static <see cref="AddConditionalIgnoreMapping"/> method.</param>
|
||||
/// <param name="ignoreReason">The reason for the ignore.</param>
|
||||
public ConditionalIgnoreAttribute(string conditionKey, string ignoreReason)
|
||||
{
|
||||
m_ConditionKey = conditionKey;
|
||||
m_IgnoreReason = ignoreReason;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Modifies a test as defined for the specific attribute.
|
||||
/// </summary>
|
||||
/// <param name="test">The test to modify</param>
|
||||
public void ApplyToTest(Test test)
|
||||
{
|
||||
var key = m_ConditionKey.ToLowerInvariant();
|
||||
if (m_ConditionMap.ContainsKey(key) && m_ConditionMap[key])
|
||||
{
|
||||
test.RunState = RunState.Ignored;
|
||||
string skipReason = string.Format(m_IgnoreReason);
|
||||
test.Properties.Add(PropertyNames.SkipReason, skipReason);
|
||||
}
|
||||
}
|
||||
|
||||
private static Dictionary<string, bool> m_ConditionMap = new Dictionary<string, bool>();
|
||||
|
||||
/// <summary>
|
||||
/// Adds a flag indicating whether tests with the same key should be ignored.
|
||||
/// </summary>
|
||||
/// <param name="key">The key to ignore tests for.</param>
|
||||
/// <param name="value">A boolean value indicating whether the tests should be ignored.</param>
|
||||
/// <example>
|
||||
/// <code>
|
||||
/// // An example in which tests are ignored in the Mac editor only.
|
||||
/// using UnityEditor;
|
||||
/// using NUnit.Framework;
|
||||
/// using UnityEngine.TestTools;
|
||||
///
|
||||
/// [InitializeOnLoad]
|
||||
/// public class OnLoad
|
||||
/// {
|
||||
/// static OnLoad()
|
||||
/// {
|
||||
/// var editorIsOSX = false;
|
||||
/// #if UNITY_EDITOR_OSX
|
||||
/// editorIsOSX = true;
|
||||
/// #endif
|
||||
///
|
||||
/// ConditionalIgnoreAttribute.AddConditionalIgnoreMapping("IgnoreInMacEditor", editorIsOSX);
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// public class MyTestClass
|
||||
/// {
|
||||
/// [Test, ConditionalIgnore("IgnoreInMacEditor", "Ignored on Mac editor.")]
|
||||
/// public void TestNeverRunningInMacEditor()
|
||||
/// {
|
||||
/// Assert.Pass();
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
/// </example>
|
||||
public static void AddConditionalIgnoreMapping(string key, bool value)
|
||||
{
|
||||
m_ConditionMap.Add(key.ToLowerInvariant(), value);
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: c82a8473f4a8f7b42a004c91e06d2f2b
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace UnityEngine.TestTools
|
||||
{
|
||||
internal class EnumeratorHelper
|
||||
{
|
||||
public static bool IsRunningNestedEnumerator => enumeratorStack.Count > 0;
|
||||
|
||||
private static IEnumerator currentEnumerator;
|
||||
private static Stack<IEnumerator> enumeratorStack = new Stack<IEnumerator>();
|
||||
|
||||
/// <summary>
|
||||
/// This method executes a given enumerator and all nested enumerators.
|
||||
/// If any resuming (setting of pc) is needed, it needs to be done before being passed to this method.
|
||||
/// </summary>
|
||||
public static IEnumerator UnpackNestedEnumerators(IEnumerator testEnumerator)
|
||||
{
|
||||
if (testEnumerator == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(testEnumerator));
|
||||
}
|
||||
|
||||
currentEnumerator = testEnumerator;
|
||||
enumeratorStack.Clear();
|
||||
|
||||
return ProgressOnEnumerator();
|
||||
}
|
||||
|
||||
private static IEnumerator ProgressOnEnumerator()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (!currentEnumerator.MoveNext())
|
||||
{
|
||||
if (enumeratorStack.Count == 0)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
currentEnumerator = enumeratorStack.Pop();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (currentEnumerator.Current is IEnumerator nestedEnumerator)
|
||||
{
|
||||
enumeratorStack.Push(currentEnumerator);
|
||||
currentEnumerator = nestedEnumerator;
|
||||
}
|
||||
else
|
||||
{
|
||||
yield return currentEnumerator.Current;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void SetEnumeratorPC(int pc)
|
||||
{
|
||||
if (currentEnumerator == null)
|
||||
{
|
||||
throw new Exception("No enumerator is currently running.");
|
||||
}
|
||||
|
||||
if (IsRunningNestedEnumerator)
|
||||
{
|
||||
throw new Exception("Cannot set the enumerator PC while running nested enumerators.");
|
||||
}
|
||||
|
||||
ActivePcHelper.SetEnumeratorPC(currentEnumerator, pc);
|
||||
}
|
||||
|
||||
public static int GetEnumeratorPC()
|
||||
{
|
||||
if (currentEnumerator == null)
|
||||
{
|
||||
throw new Exception("No enumerator is currently running.");
|
||||
}
|
||||
|
||||
if (IsRunningNestedEnumerator)
|
||||
{
|
||||
// Restrict the getting of PC, as it will not reflect what is currently running;
|
||||
throw new Exception("Cannot get the enumerator PC while running nested enumerators.");
|
||||
}
|
||||
|
||||
return ActivePcHelper.GetEnumeratorPC(currentEnumerator);
|
||||
}
|
||||
|
||||
private static TestCommandPcHelper pcHelper;
|
||||
internal static TestCommandPcHelper ActivePcHelper
|
||||
{
|
||||
get
|
||||
{
|
||||
if (pcHelper == null)
|
||||
{
|
||||
pcHelper = new TestCommandPcHelper();
|
||||
}
|
||||
|
||||
return pcHelper;
|
||||
}
|
||||
set
|
||||
{
|
||||
pcHelper = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ff0aad009559b814ffc27001341fc1c3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+82
@@ -0,0 +1,82 @@
|
||||
using NUnit.Framework;
|
||||
using NUnit.Framework.Interfaces;
|
||||
using NUnit.Framework.Internal.Commands;
|
||||
using System;
|
||||
|
||||
namespace UnityEngine.TestTools
|
||||
{
|
||||
/// <summary>
|
||||
/// This attribute is an alternative to the standard `Ignore` attribute in [NUnit](http://www.nunit.org/). It allows for ignoring tests based on arguments which were passed to the test method.
|
||||
/// </summary>
|
||||
/// <example>
|
||||
/// <para>The following example shows a method to use the `ParametrizedIgnore` attribute to ignore only one test with specific combination of arguments, where someString is `b` and someInt is `10`.</para>
|
||||
/// <code>using NUnit.Framework;
|
||||
/// using System.Collections.Generic;
|
||||
/// using UnityEngine.TestTools;
|
||||
///
|
||||
/// public class MyTestsClass
|
||||
/// {
|
||||
/// public static IEnumerable<TestCaseData> MyTestCaseSource()
|
||||
/// {
|
||||
/// for (int i = 0; i < 5; i++)
|
||||
/// {
|
||||
/// yield return new TestCaseData("a", i);
|
||||
/// yield return new TestCaseData("b", i);
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// [Test, TestCaseSource("MyTestCaseSource")]
|
||||
/// [ParametrizedIgnore("b", 3)]
|
||||
/// public void Test(string someString, int someInt)
|
||||
/// {
|
||||
/// Assert.Pass();
|
||||
/// }
|
||||
/// }</code>
|
||||
/// <para>It could also be used together with `Values` attribute in [NUnit](http://www.nunit.org/).</para>
|
||||
/// <code>using NUnit.Framework;
|
||||
/// using UnityEngine.TestTools;
|
||||
///
|
||||
/// public class MyTestsClass
|
||||
/// {
|
||||
/// [Test]
|
||||
/// [ParametrizedIgnore("b", 10)]
|
||||
/// public void Test(
|
||||
/// [Values("a", "b")] string someString,
|
||||
/// [Values(5, 10)] int someInt)
|
||||
/// {
|
||||
/// Assert.Pass();
|
||||
/// }
|
||||
/// }</code>
|
||||
/// </example>
|
||||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true, Inherited = true)]
|
||||
public class ParametrizedIgnoreAttribute : NUnitAttribute, IWrapTestMethod
|
||||
{
|
||||
/// <summary>
|
||||
/// Argument combination for the test case to ignore.
|
||||
/// </summary>
|
||||
public object[] Arguments { get; }
|
||||
/// <summary>
|
||||
/// Reason for the ignore.
|
||||
/// </summary>
|
||||
public string Reason { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ParametrizedIgnoreAttribute"/> class with a argument combination that is ignored.
|
||||
/// </summary>
|
||||
/// <param name="arguments">The argument combination to ignore</param>
|
||||
public ParametrizedIgnoreAttribute(params object[] arguments)
|
||||
{
|
||||
this.Arguments = arguments;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Wraps a test command with the command to handled parametrized ignore.
|
||||
/// </summary>
|
||||
/// <param name="command">The command to wrap.</param>
|
||||
/// <returns>A command handling parametrized ignore, with respect to the inner command.</returns>
|
||||
public TestCommand Wrap(TestCommand command)
|
||||
{
|
||||
return new ParametrizedIgnoreCommand(command, Arguments, Reason);
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: e9c53c5109a7498ca4408e53f2593b1a
|
||||
timeCreated: 1699350131
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
using NUnit.Framework;
|
||||
using UnityEngine.Scripting;
|
||||
|
||||
namespace UnityEngine.TestTools
|
||||
{
|
||||
/// <summary>
|
||||
/// Like the ValuesAttribute it is used to provide literal arguments for
|
||||
/// an individual parameter of a test. It has the Preserve attribute added,
|
||||
/// allowing it to persist in players build at a high stripping level.
|
||||
/// </summary>
|
||||
[Preserve]
|
||||
public class PreservedValuesAttribute : ValuesAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Construct the values attribute with a set of arguments.
|
||||
/// </summary>
|
||||
/// <param name="args">The set of arguments for the test parameter.</param>
|
||||
public PreservedValuesAttribute(params object[] args) : base(args)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 528c8790cf844716aead80624be893aa
|
||||
timeCreated: 1699350924
|
||||
+92
@@ -0,0 +1,92 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using NUnit.Framework.Interfaces;
|
||||
using NUnit.Framework.Internal;
|
||||
|
||||
namespace UnityEngine.TestTools
|
||||
{
|
||||
internal class TestEnumerator
|
||||
{
|
||||
private readonly ITestExecutionContext m_Context;
|
||||
private static IEnumerator m_TestEnumerator;
|
||||
|
||||
public static IEnumerator Enumerator { get { return m_TestEnumerator; } }
|
||||
|
||||
public static void Reset()
|
||||
{
|
||||
m_TestEnumerator = null;
|
||||
}
|
||||
|
||||
public TestEnumerator(ITestExecutionContext context, IEnumerator testEnumerator)
|
||||
{
|
||||
m_Context = context;
|
||||
m_TestEnumerator = testEnumerator;
|
||||
}
|
||||
|
||||
public IEnumerator Execute()
|
||||
{
|
||||
m_Context.CurrentResult.SetResult(ResultState.Success);
|
||||
|
||||
return Execute(m_TestEnumerator, new EnumeratorContext(m_Context));
|
||||
}
|
||||
|
||||
private IEnumerator Execute(IEnumerator enumerator, EnumeratorContext context)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
if (context.ExceptionWasRecorded)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (!enumerator.MoveNext())
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
context.RecordExceptionWithHint(ex);
|
||||
break;
|
||||
}
|
||||
|
||||
if (enumerator.Current is IEnumerator nestedEnumerator)
|
||||
{
|
||||
yield return Execute(nestedEnumerator, context);
|
||||
}
|
||||
else
|
||||
{
|
||||
yield return enumerator.Current;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private class EnumeratorContext
|
||||
{
|
||||
private readonly ITestExecutionContext m_Context;
|
||||
|
||||
public EnumeratorContext(ITestExecutionContext context)
|
||||
{
|
||||
m_Context = context;
|
||||
}
|
||||
|
||||
public bool ExceptionWasRecorded
|
||||
{
|
||||
get;
|
||||
private set;
|
||||
}
|
||||
|
||||
public void RecordExceptionWithHint(Exception ex)
|
||||
{
|
||||
if (ExceptionWasRecorded)
|
||||
{
|
||||
return;
|
||||
}
|
||||
m_Context.CurrentResult.RecordException(ex);
|
||||
ExceptionWasRecorded = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 750aad009559b814dbc27001341fc1c3
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
|
||||
namespace UnityEngine.TestTools
|
||||
{
|
||||
/// <summary>
|
||||
/// The presence of this attribute makes the Test Runner expect every single log. By
|
||||
/// default, the runner only fails automatically on any error logs, so this adds warnings and infos as well.
|
||||
/// It is the same as calling `LogAssert.NoUnexpectedReceived()` at the bottom of every affected test.
|
||||
///
|
||||
/// This attribute can be applied to test assemblies (affects every test in the assembly), fixtures (affects every test in the fixture), or on individual test methods. It is also automatically inherited from base
|
||||
/// fixtures.
|
||||
///
|
||||
/// The `MustExpect` property (on by default) lets you selectively enable or disable the higher level value. For
|
||||
/// example when migrating an assembly to this more strict checking method, you might attach
|
||||
/// `[assembly:TestMustExpectAllLogs]` to the assembly itself, but whitelist individual failing fixtures and test methods
|
||||
/// by attaching `[TestMustExpectAllLogs(MustExpect=false)]` until they can be migrated. This also means new tests in that
|
||||
/// assembly would be required to have the more strict checking.
|
||||
/// </summary>
|
||||
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method)]
|
||||
public class TestMustExpectAllLogsAttribute : Attribute
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes and returns an instance of TestMustExpectAllLogsAttribute.
|
||||
/// </summary>
|
||||
/// <param name="mustExpect">
|
||||
/// A value indicating whether the test must expect all logs.
|
||||
/// </param>
|
||||
public TestMustExpectAllLogsAttribute(bool mustExpect = true)
|
||||
=> MustExpect = mustExpect;
|
||||
/// <summary>
|
||||
/// Returns the flag of whether the test must expect all logs.
|
||||
/// </summary>
|
||||
public bool MustExpect { get; }
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 3803f736886e77842995ddbc3531afaa
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using NUnit.Framework.Interfaces;
|
||||
using NUnit.Framework.Internal.Builders;
|
||||
|
||||
namespace UnityEngine.TestTools
|
||||
{
|
||||
internal class UnityCombinatorialStrategy : CombinatorialStrategy, ICombiningStrategy
|
||||
{
|
||||
public new IEnumerable<ITestCaseData> GetTestCases(IEnumerable[] sources)
|
||||
{
|
||||
var testCases = base.GetTestCases(sources);
|
||||
foreach (var testCase in testCases)
|
||||
{
|
||||
testCase.GetType().GetProperty("ExpectedResult").SetValue(testCase, new object(), null);
|
||||
}
|
||||
return testCases;
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7af6ac3e6b51b8d4aab04adc85b8de2f
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+102
@@ -0,0 +1,102 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using NUnit.Framework;
|
||||
using NUnit.Framework.Interfaces;
|
||||
using NUnit.Framework.Internal;
|
||||
|
||||
namespace UnityEngine.TestTools
|
||||
{
|
||||
/// <summary>
|
||||
/// Use this attribute to define a specific set of platforms you want or do not want your test(s) to run on.
|
||||
///
|
||||
/// You can use this attribute on the test method, test class, or test assembly level. Use the supported <see cref="RuntimePlatform"/> enumeration values to specify the platforms. You can also specify which platforms to test by passing one or more `RuntimePlatform` values along with or without the include or exclude properties as parameters to the [Platform](https://github.com/nunit/docs/wiki/Platform-Attribute) attribute constructor.
|
||||
///
|
||||
/// The test(s) skips if the current target platform is:
|
||||
/// - Not explicitly specified in the included platforms list
|
||||
/// - In the excluded platforms list
|
||||
/// </summary>
|
||||
/// <example>
|
||||
/// <code>
|
||||
/// <![CDATA[
|
||||
/// using UnityEngine;
|
||||
/// using UnityEngine.TestTools;
|
||||
/// using NUnit.Framework;
|
||||
///
|
||||
/// [TestFixture]
|
||||
/// public class TestClass
|
||||
/// {
|
||||
/// [Test]
|
||||
/// [UnityPlatform(RuntimePlatform.WindowsPlayer)]
|
||||
/// public void TestMethod()
|
||||
/// {
|
||||
/// Assert.AreEqual(Application.platform, RuntimePlatform.WindowsPlayer);
|
||||
/// }
|
||||
/// }
|
||||
/// ]]>
|
||||
/// </code>
|
||||
/// </example>
|
||||
[AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = true)]
|
||||
public class UnityPlatformAttribute : NUnitAttribute, IApplyToTest
|
||||
{
|
||||
/// <summary>
|
||||
/// A subset of platforms you need to have your tests run on.
|
||||
/// </summary>
|
||||
public RuntimePlatform[] include { get; set; }
|
||||
/// <summary>
|
||||
/// List the platforms you do not want to have your tests run on.
|
||||
/// </summary>
|
||||
public RuntimePlatform[] exclude { get; set; }
|
||||
|
||||
private string m_skippedReason;
|
||||
|
||||
/// <summary>
|
||||
/// Constructs a new instance of the <see cref="UnityPlatformAttribute"/> class.
|
||||
/// </summary>
|
||||
public UnityPlatformAttribute()
|
||||
{
|
||||
include = new List<RuntimePlatform>().ToArray();
|
||||
exclude = new List<RuntimePlatform>().ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Constructs a new instance of the <see cref="UnityPlatformAttribute"/> class with a list of platforms to include.
|
||||
/// </summary>
|
||||
/// <param name="include">The different <see cref="RuntimePlatform"/> to run the test on.</param>
|
||||
public UnityPlatformAttribute(params RuntimePlatform[] include)
|
||||
: this()
|
||||
{
|
||||
this.include = include;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Modifies a test as defined for the specific attribute.
|
||||
/// </summary>
|
||||
/// <param name="test">The test to modify</param>
|
||||
public void ApplyToTest(Test test)
|
||||
{
|
||||
if (test.RunState == RunState.NotRunnable || test.RunState == RunState.Ignored || IsPlatformSupported(Application.platform))
|
||||
{
|
||||
return;
|
||||
}
|
||||
test.RunState = RunState.Skipped;
|
||||
test.Properties.Add(PropertyNames.SkipReason, m_skippedReason);
|
||||
}
|
||||
|
||||
internal bool IsPlatformSupported(RuntimePlatform testTargetPlatform)
|
||||
{
|
||||
if (include.Any() && !include.Any(x => x == testTargetPlatform))
|
||||
{
|
||||
m_skippedReason = string.Format("Only supported on {0}", string.Join(", ", include.Select(x => x.ToString()).ToArray()));
|
||||
return false;
|
||||
}
|
||||
|
||||
if (exclude.Any(x => x == testTargetPlatform))
|
||||
{
|
||||
m_skippedReason = string.Format("Not supported on {0}", string.Join(", ", exclude.Select(x => x.ToString()).ToArray()));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 5440c1153b397e14c9c7b1d6eb83b9f9
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+229
@@ -0,0 +1,229 @@
|
||||
using System;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace UnityEngine.TestTools
|
||||
{
|
||||
/// <summary>
|
||||
/// The `UnitySetUp` and <see cref="UnityTearDownAttribute"/> attributes are identical to the standard `SetUp` and `TearDown` attributes, with the exception that they allow for <see cref="IEditModeTestYieldInstruction"/>. The `UnitySetUp` and `UnityTearDown` attributes expect a return type of [IEnumerator](https://docs.microsoft.com/en-us/dotnet/api/system.collections.ienumerator?view=netframework-4.8).
|
||||
/// </summary>
|
||||
/// <example>
|
||||
/// <code>
|
||||
/// <![CDATA[
|
||||
/// public class SetUpTearDownExample
|
||||
/// {
|
||||
/// [UnitySetUp]
|
||||
/// public IEnumerator SetUp()
|
||||
/// {
|
||||
/// yield return new EnterPlayMode();
|
||||
/// }
|
||||
///
|
||||
/// [Test]
|
||||
/// public void MyTest()
|
||||
/// {
|
||||
/// Debug.Log("This runs inside playmode");
|
||||
/// }
|
||||
///
|
||||
/// [UnityTearDown]
|
||||
/// public IEnumerator TearDown()
|
||||
/// {
|
||||
/// yield return new ExitPlayMode();
|
||||
/// }
|
||||
/// }
|
||||
/// ]]>
|
||||
/// </code>
|
||||
/// <para>## Base and Derived class example</para>
|
||||
/// <code>
|
||||
/// <![CDATA[
|
||||
/// public class BaseClass
|
||||
/// {
|
||||
/// [OneTimeSetUp]
|
||||
/// public void OneTimeSetUp()
|
||||
/// {
|
||||
/// Debug.Log("OneTimeSetUp Base");
|
||||
/// }
|
||||
///
|
||||
/// [SetUp]
|
||||
/// public void SetUp()
|
||||
/// {
|
||||
/// Debug.Log("SetUp Base");
|
||||
/// }
|
||||
///
|
||||
/// [UnitySetUp]
|
||||
/// public IEnumerator UnitySetUp()
|
||||
/// {
|
||||
/// Debug.Log("UnitySetup Base");
|
||||
/// yield return null;
|
||||
/// }
|
||||
///
|
||||
/// [TearDown]
|
||||
/// public void TearDown()
|
||||
/// {
|
||||
/// Debug.Log("TearDown Base");
|
||||
/// }
|
||||
///
|
||||
/// [UnityTearDown]
|
||||
/// public IEnumerator UnityTearDown()
|
||||
/// {
|
||||
/// Debug.Log("UnityTearDown Base");
|
||||
/// yield return null;
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// public class DerivedClass : BaseClass
|
||||
/// {
|
||||
/// [OneTimeSetUp]
|
||||
/// public new void OneTimeSetUp()
|
||||
/// {
|
||||
/// Debug.Log("OneTimeSetUp");
|
||||
/// }
|
||||
///
|
||||
/// [SetUp]
|
||||
/// public new void SetUp()
|
||||
/// {
|
||||
/// Debug.Log("SetUp");
|
||||
/// }
|
||||
///
|
||||
/// [UnitySetUp]
|
||||
/// public new IEnumerator UnitySetUp()
|
||||
/// {
|
||||
/// Debug.Log("UnitySetup");
|
||||
/// yield return null;
|
||||
/// }
|
||||
///
|
||||
/// [Test]
|
||||
/// public void UnitTest()
|
||||
/// {
|
||||
/// Debug.Log("Test");
|
||||
/// }
|
||||
///
|
||||
/// [UnityTest]
|
||||
/// public IEnumerator UnityTest()
|
||||
/// {
|
||||
/// Debug.Log("UnityTest before yield");
|
||||
/// yield return null;
|
||||
/// Debug.Log("UnityTest after yield");
|
||||
/// }
|
||||
///
|
||||
/// [TearDown]
|
||||
/// public new void TearDown()
|
||||
/// {
|
||||
/// Debug.Log("TearDown");
|
||||
/// }
|
||||
///
|
||||
/// [UnityTearDown]
|
||||
/// public new IEnumerator UnityTearDown()
|
||||
/// {
|
||||
/// Debug.Log("UnityTearDown");
|
||||
/// yield return null;
|
||||
/// }
|
||||
///
|
||||
/// [OneTimeTearDown]
|
||||
/// public void OneTimeTearDown()
|
||||
/// {
|
||||
/// Debug.Log("OneTimeTearDown");
|
||||
/// }
|
||||
/// }
|
||||
/// ]]>
|
||||
/// </code>
|
||||
/// <para>## Domain reload example</para>
|
||||
/// <code>
|
||||
/// <![CDATA[
|
||||
/// public class BaseClass
|
||||
/// {
|
||||
/// [OneTimeSetUp]
|
||||
/// public void OneTimeSetUp()
|
||||
/// {
|
||||
/// Debug.Log("OneTimeSetUp Base");
|
||||
/// }
|
||||
///
|
||||
/// [SetUp]
|
||||
/// public void SetUp()
|
||||
/// {
|
||||
/// Debug.Log("SetUp Base");
|
||||
/// }
|
||||
///
|
||||
/// [UnitySetUp]
|
||||
/// public IEnumerator UnitySetUp()
|
||||
/// {
|
||||
/// Debug.Log("UnitySetup Base");
|
||||
/// yield return null;
|
||||
/// }
|
||||
///
|
||||
/// [TearDown]
|
||||
/// public void TearDown()
|
||||
/// {
|
||||
/// Debug.Log("TearDown Base");
|
||||
/// }
|
||||
///
|
||||
/// [UnityTearDown]
|
||||
/// public IEnumerator UnityTearDown()
|
||||
/// {
|
||||
/// Debug.Log("UnityTearDown Base");
|
||||
/// yield return null;
|
||||
/// }
|
||||
/// }
|
||||
///
|
||||
/// public class DerivedClass : BaseClass
|
||||
/// {
|
||||
/// [OneTimeSetUp]
|
||||
/// public new void OneTimeSetUp()
|
||||
/// {
|
||||
/// Debug.Log("OneTimeSetUp");
|
||||
/// }
|
||||
///
|
||||
/// [SetUp]
|
||||
/// public new void SetUp()
|
||||
/// {
|
||||
/// Debug.Log("SetUp");
|
||||
/// }
|
||||
///
|
||||
/// [UnitySetUp]
|
||||
/// public new IEnumerator UnitySetUp()
|
||||
/// {
|
||||
/// Debug.Log("UnitySetup");
|
||||
/// yield return null;
|
||||
/// }
|
||||
///
|
||||
/// [Test]
|
||||
/// public void UnitTest()
|
||||
/// {
|
||||
/// Debug.Log("Test");
|
||||
/// }
|
||||
///
|
||||
/// [UnityTest]
|
||||
/// public IEnumerator UnityTest()
|
||||
/// {
|
||||
/// Debug.Log("UnityTest before yield");
|
||||
/// yield return new EnterPlayMode();
|
||||
/// //Domain reload happening
|
||||
/// yield return new ExitPlayMode();
|
||||
/// Debug.Log("UnityTest after yield");
|
||||
/// }
|
||||
///
|
||||
/// [TearDown]
|
||||
/// public new void TearDown()
|
||||
/// {
|
||||
/// Debug.Log("TearDown");
|
||||
/// }
|
||||
///
|
||||
/// [UnityTearDown]
|
||||
/// public new IEnumerator UnityTearDown()
|
||||
/// {
|
||||
/// Debug.Log("UnityTearDown");
|
||||
/// yield return null;
|
||||
/// }
|
||||
///
|
||||
/// [OneTimeTearDown]
|
||||
/// public void OneTimeTearDown()
|
||||
/// {
|
||||
/// Debug.Log("OneTimeTearDown");
|
||||
/// }
|
||||
/// }
|
||||
/// ]]>
|
||||
/// </code>
|
||||
/// </example>
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public class UnitySetUpAttribute : NUnitAttribute
|
||||
{
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cc6401f13df54ba44bfd7cdc93c7d64d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
using System;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace UnityEngine.TestTools
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="UnitySetUpAttribute"/> and `UnityTearDown` attributes are identical to the standard `SetUp` and `TearDown` attributes, with the exception that they allow for <see cref="IEditModeTestYieldInstruction"/>. The `UnitySetUp` and `UnityTearDown` attributes expect a return type of [IEnumerator](https://docs.microsoft.com/en-us/dotnet/api/system.collections.ienumerator?view=netframework-4.8).
|
||||
/// </summary>
|
||||
/// <example>
|
||||
/// <code>
|
||||
///public class SetUpTearDownExample
|
||||
/// {
|
||||
/// [UnitySetUp]
|
||||
/// public IEnumerator SetUp()
|
||||
/// {
|
||||
/// yield return new EnterPlayMode();
|
||||
/// }
|
||||
///
|
||||
/// [Test]
|
||||
/// public void MyTest()
|
||||
/// {
|
||||
/// Debug.Log("This runs inside playmode");
|
||||
/// }
|
||||
///
|
||||
/// [UnityTearDown]
|
||||
/// public IEnumerator TearDown()
|
||||
/// {
|
||||
/// yield return new ExitPlayMode();
|
||||
/// }
|
||||
/// }
|
||||
/// </code>
|
||||
/// </example>
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public class UnityTearDownAttribute : NUnitAttribute
|
||||
{
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 600f4b74746dbf944901257f81a8af6d
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
+149
@@ -0,0 +1,149 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using NUnit.Framework;
|
||||
using NUnit.Framework.Interfaces;
|
||||
using NUnit.Framework.Internal;
|
||||
using NUnit.Framework.Internal.Builders;
|
||||
|
||||
namespace UnityEngine.TestTools
|
||||
{
|
||||
/// <summary>
|
||||
/// `UnityTest` attribute is the main addition to the standard [NUnit](http://www.nunit.org/) library for the Unity Test Framework. This type of unit test allows you to skip a frame from within a test (so background tasks can finish) or give certain commands to the Unity **Editor**, such as performing a domain reload or entering **Play Mode** from an **Edit Mode** test.
|
||||
/// In Play Mode, the `UnityTest` attribute runs as a [coroutine](https://docs.unity3d.com/Manual/Coroutines.html). Whereas Edit Mode tests run in the [EditorApplication.update](https://docs.unity3d.com/ScriptReference/EditorApplication-update.html) callback loop.
|
||||
/// The `UnityTest` attribute is, in fact, an alternative to the `NUnit` [Test attribute](https://github.com/nunit/docs/wiki/Test-Attribute), which allows yielding instructions back to the framework. Once the instruction is complete, the test run continues. If you `yield return null`, you skip a frame. That might be necessary to ensure that some changes do happen on the next iteration of either the `EditorApplication.update` loop or the [game loop](https://docs.unity3d.com/Manual/ExecutionOrder.html).
|
||||
/// </summary>
|
||||
/// <example>
|
||||
/// <para>
|
||||
/// ## Edit Mode example
|
||||
/// The most simple example of an Edit Mode test could be the one that yields `null` to skip the current frame and then continues to run:
|
||||
/// </para>
|
||||
/// <code>
|
||||
/// [UnityTest]
|
||||
/// public IEnumerator EditorUtility_WhenExecuted_ReturnsSuccess()
|
||||
/// {
|
||||
/// var utility = RunEditorUtilityInTheBackground();
|
||||
///
|
||||
/// while (utility.isRunning)
|
||||
/// {
|
||||
/// yield return null;
|
||||
/// }
|
||||
///
|
||||
/// Assert.IsTrue(utility.isSuccess);
|
||||
/// }
|
||||
/// </code>
|
||||
/// <para>
|
||||
/// ## Play Mode example
|
||||
///
|
||||
/// In Play Mode, a test runs as a coroutine attached to a [MonoBehaviour](https://docs.unity3d.com/ScriptReference/MonoBehaviour.html). So all the yield instructions available in coroutines, are also available in your test.
|
||||
///
|
||||
/// From a Play Mode test you can use one of Unity’s [Yield Instructions](https://docs.unity3d.com/ScriptReference/YieldInstruction.html):
|
||||
///
|
||||
/// - [WaitForFixedUpdate](https://docs.unity3d.com/ScriptReference/WaitForFixedUpdate.html): to ensure changes expected within the next cycle of physics calculations.
|
||||
/// - [WaitForSeconds](https://docs.unity3d.com/ScriptReference/WaitForSeconds.html): if you want to pause your test coroutine for a fixed amount of time. Be careful about creating long-running tests.
|
||||
///
|
||||
/// The simplest example is to yield to `WaitForFixedUpdate`:
|
||||
/// </para>
|
||||
/// <code>
|
||||
/// [UnityTest]
|
||||
/// public IEnumerator GameObject_WithRigidBody_WillBeAffectedByPhysics()
|
||||
/// {
|
||||
/// var go = new GameObject();
|
||||
/// go.AddComponent<Rigidbody>();
|
||||
/// var originalPosition = go.transform.position.y;
|
||||
///
|
||||
/// yield return new WaitForFixedUpdate();
|
||||
///
|
||||
/// Assert.AreNotEqual(originalPosition, go.transform.position.y);
|
||||
/// }
|
||||
/// </code>
|
||||
/// </example>
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public class UnityTestAttribute : CombiningStrategyAttribute, IImplyFixture, ISimpleTestBuilder, ITestBuilder, IApplyToTest
|
||||
{
|
||||
private const string k_MethodMarkedWithUnitytestMustReturnIenumerator = "Method marked with UnityTest must return IEnumerator.";
|
||||
|
||||
/// <summary>
|
||||
/// Initializes and returns an instance of UnityTestAttribute.
|
||||
/// </summary>
|
||||
public UnityTestAttribute() : base(new UnityCombinatorialStrategy(), new ParameterDataSourceProvider()) {}
|
||||
|
||||
private readonly NUnitTestCaseBuilder _builder = new NUnitTestCaseBuilder();
|
||||
|
||||
/// <summary>
|
||||
/// This method builds the TestMethod from the Test and the method info. In addition it removes the expected result of the test.
|
||||
/// </summary>
|
||||
/// <param name="method">The method info.</param>
|
||||
/// <param name="suite">The test.</param>
|
||||
/// <returns>A TestMethod object</returns>
|
||||
TestMethod ISimpleTestBuilder.BuildFrom(IMethodInfo method, Test suite)
|
||||
{
|
||||
var t = CreateTestMethod(method, suite);
|
||||
|
||||
AdaptToUnityTestMethod(t);
|
||||
|
||||
return t;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method hides the base method from CombiningStrategyAttribute.
|
||||
/// It builds a TestMethod from a Parameterized Test and the method info.
|
||||
/// In addition it removes the expected result of the test.
|
||||
/// </summary>
|
||||
/// <param name="method">The method info.</param>
|
||||
/// <param name="suite">The test.</param>
|
||||
/// <returns>A TestMethod object</returns>
|
||||
IEnumerable<TestMethod> ITestBuilder.BuildFrom(IMethodInfo method, Test suite)
|
||||
{
|
||||
var testMethods = base.BuildFrom(method, suite);
|
||||
|
||||
foreach (var t in testMethods)
|
||||
{
|
||||
AdaptToUnityTestMethod(t);
|
||||
}
|
||||
|
||||
return testMethods;
|
||||
}
|
||||
|
||||
private TestMethod CreateTestMethod(IMethodInfo method, Test suite)
|
||||
{
|
||||
TestCaseParameters parms = new TestCaseParameters
|
||||
{
|
||||
ExpectedResult = new object(),
|
||||
HasExpectedResult = true
|
||||
};
|
||||
|
||||
var t = _builder.BuildTestMethod(method, suite, parms);
|
||||
return t;
|
||||
}
|
||||
|
||||
private static void AdaptToUnityTestMethod(TestMethod t)
|
||||
{
|
||||
if (t.parms != null)
|
||||
{
|
||||
t.parms.HasExpectedResult = false;
|
||||
}
|
||||
}
|
||||
|
||||
private static bool IsMethodReturnTypeIEnumerator(IMethodInfo method)
|
||||
{
|
||||
return !method.ReturnType.IsType(typeof(IEnumerator));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method hides the base method ApplyToTest from CombiningStrategyAttribute.
|
||||
/// In addition it ensures that the test with the `UnityTestAttribute` has an IEnumerator as return type.
|
||||
/// </summary>
|
||||
/// <param name="test">The test.</param>
|
||||
public new void ApplyToTest(Test test)
|
||||
{
|
||||
if (IsMethodReturnTypeIEnumerator(test.Method))
|
||||
{
|
||||
test.RunState = RunState.NotRunnable;
|
||||
test.Properties.Set(PropertyNames.SkipReason, k_MethodMarkedWithUnitytestMustReturnIenumerator);
|
||||
}
|
||||
|
||||
base.ApplyToTest(test);
|
||||
}
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: fedb0f9e5006b1943abae52f52f08a1a
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user