No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UnityTestAttribute.cs 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using NUnit.Framework;
  5. using NUnit.Framework.Interfaces;
  6. using NUnit.Framework.Internal;
  7. using NUnit.Framework.Internal.Builders;
  8. namespace UnityEngine.TestTools
  9. {
  10. /// <summary>
  11. /// `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.
  12. /// 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.
  13. /// 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).
  14. /// <example>
  15. /// ## Edit Mode example
  16. /// 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:
  17. /// <code>
  18. /// [UnityTest]
  19. /// public IEnumerator EditorUtility_WhenExecuted_ReturnsSuccess()
  20. /// {
  21. /// var utility = RunEditorUtilityInTheBackground();
  22. ///
  23. /// while (utility.isRunning)
  24. /// {
  25. /// yield return null;
  26. /// }
  27. ///
  28. /// Assert.IsTrue(utility.isSuccess);
  29. /// }
  30. /// </code>
  31. /// </example>
  32. /// <example>
  33. /// ## Play Mode example
  34. ///
  35. /// 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.
  36. ///
  37. /// From a Play Mode test you can use one of Unity’s [Yield Instructions](https://docs.unity3d.com/ScriptReference/YieldInstruction.html):
  38. ///
  39. /// - [WaitForFixedUpdate](https://docs.unity3d.com/ScriptReference/WaitForFixedUpdate.html): to ensure changes expected within the next cycle of physics calculations.
  40. /// - [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.
  41. ///
  42. /// The simplest example is to yield to `WaitForFixedUpdate`:
  43. /// <code>
  44. /// [UnityTest]
  45. /// public IEnumerator GameObject_WithRigidBody_WillBeAffectedByPhysics()
  46. /// {
  47. /// var go = new GameObject();
  48. /// go.AddComponent&lt;Rigidbody&gt;();
  49. /// var originalPosition = go.transform.position.y;
  50. ///
  51. /// yield return new WaitForFixedUpdate();
  52. ///
  53. /// Assert.AreNotEqual(originalPosition, go.transform.position.y);
  54. /// }
  55. /// </code>
  56. /// </example>
  57. /// </summary>
  58. [AttributeUsage(AttributeTargets.Method)]
  59. public class UnityTestAttribute : CombiningStrategyAttribute, IImplyFixture, ISimpleTestBuilder, ITestBuilder, IApplyToTest
  60. {
  61. private const string k_MethodMarkedWithUnitytestMustReturnIenumerator = "Method marked with UnityTest must return IEnumerator.";
  62. /// <summary>
  63. /// Initializes and returns an instance of UnityTestAttribute.
  64. /// </summary>
  65. public UnityTestAttribute() : base(new UnityCombinatorialStrategy(), new ParameterDataSourceProvider()) {}
  66. private readonly NUnitTestCaseBuilder _builder = new NUnitTestCaseBuilder();
  67. /// <summary>
  68. /// This method builds the TestMethod from the Test and the method info. In addition it removes the expected result of the test.
  69. /// </summary>
  70. /// <param name="method">The method info.</param>
  71. /// <param name="suite">The test.</param>
  72. /// <returns>A TestMethod object</returns>
  73. TestMethod ISimpleTestBuilder.BuildFrom(IMethodInfo method, Test suite)
  74. {
  75. var t = CreateTestMethod(method, suite);
  76. AdaptToUnityTestMethod(t);
  77. return t;
  78. }
  79. /// <summary>
  80. /// This method hides the base method from CombiningStrategyAttribute.
  81. /// It builds a TestMethod from a Parameterized Test and the method info.
  82. /// In addition it removes the expected result of the test.
  83. /// </summary>
  84. /// <param name="method">The method info.</param>
  85. /// <param name="suite">The test.</param>
  86. /// <returns>A TestMethod object</returns>
  87. IEnumerable<TestMethod> ITestBuilder.BuildFrom(IMethodInfo method, Test suite)
  88. {
  89. var testMethods = base.BuildFrom(method, suite);
  90. foreach (var t in testMethods)
  91. {
  92. AdaptToUnityTestMethod(t);
  93. }
  94. return testMethods;
  95. }
  96. private TestMethod CreateTestMethod(IMethodInfo method, Test suite)
  97. {
  98. TestCaseParameters parms = new TestCaseParameters
  99. {
  100. ExpectedResult = new object(),
  101. HasExpectedResult = true
  102. };
  103. var t = _builder.BuildTestMethod(method, suite, parms);
  104. return t;
  105. }
  106. private static void AdaptToUnityTestMethod(TestMethod t)
  107. {
  108. if (t.parms != null)
  109. {
  110. t.parms.HasExpectedResult = false;
  111. }
  112. }
  113. private static bool IsMethodReturnTypeIEnumerator(IMethodInfo method)
  114. {
  115. return !method.ReturnType.IsType(typeof(IEnumerator));
  116. }
  117. /// <summary>
  118. /// This method hides the base method ApplyToTest from CombiningStrategyAttribute.
  119. /// In addition it ensures that the test with the `UnityTestAttribute` has an IEnumerator as return type.
  120. /// </summary>
  121. /// <param name="test">The test.</param>
  122. public new void ApplyToTest(Test test)
  123. {
  124. if (IsMethodReturnTypeIEnumerator(test.Method))
  125. {
  126. test.RunState = RunState.NotRunnable;
  127. test.Properties.Set(PropertyNames.SkipReason, k_MethodMarkedWithUnitytestMustReturnIenumerator);
  128. }
  129. base.ApplyToTest(test);
  130. }
  131. }
  132. }