ConditionalIgnoreAttribute.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System.Collections.Generic;
  2. using NUnit.Framework;
  3. using NUnit.Framework.Interfaces;
  4. using NUnit.Framework.Internal;
  5. namespace UnityEngine.TestTools
  6. {
  7. /// <summary>
  8. /// 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.
  9. /// </summary>
  10. public class ConditionalIgnoreAttribute : NUnitAttribute, IApplyToTest
  11. {
  12. string m_ConditionKey;
  13. string m_IgnoreReason;
  14. /// <summary>
  15. /// Initializes a new instance of the <see cref="ConditionalIgnoreAttribute"/> class with a condition key.
  16. /// </summary>
  17. /// <param name="conditionKey">The key to check for enabling the conditional ignore. The condition is set with the static <see cref="AddConditionalIgnoreMapping"/> method.</param>
  18. /// <param name="ignoreReason">The reason for the ignore.</param>
  19. public ConditionalIgnoreAttribute(string conditionKey, string ignoreReason)
  20. {
  21. m_ConditionKey = conditionKey;
  22. m_IgnoreReason = ignoreReason;
  23. }
  24. /// <summary>
  25. /// Modifies a test as defined for the specific attribute.
  26. /// </summary>
  27. /// <param name="test">The test to modify</param>
  28. public void ApplyToTest(Test test)
  29. {
  30. var key = m_ConditionKey.ToLowerInvariant();
  31. if (m_ConditionMap.ContainsKey(key) && m_ConditionMap[key])
  32. {
  33. test.RunState = RunState.Ignored;
  34. string skipReason = string.Format(m_IgnoreReason);
  35. test.Properties.Add(PropertyNames.SkipReason, skipReason);
  36. }
  37. }
  38. static Dictionary<string, bool> m_ConditionMap = new Dictionary<string, bool>();
  39. /// <summary>
  40. /// Adds a flag indicating whether tests with the same key should be ignored.
  41. /// </summary>
  42. /// <param name="key">The key to ignore tests for.</param>
  43. /// <param name="value">A boolean value indicating whether the tests should be ignored.</param>
  44. /// <example>
  45. /// An example in which tests are ignored in the Mac editor only.
  46. /// <code>
  47. /// using UnityEditor;
  48. /// using NUnit.Framework;
  49. /// using UnityEngine.TestTools;
  50. ///
  51. /// [InitializeOnLoad]
  52. /// public class OnLoad
  53. /// {
  54. /// static OnLoad()
  55. /// {
  56. /// var editorIsOSX = false;
  57. /// #if UNITY_EDITOR_OSX
  58. /// editorIsOSX = true;
  59. /// #endif
  60. ///
  61. /// ConditionalIgnoreAttribute.AddConditionalIgnoreMapping("IgnoreInMacEditor", editorIsOSX);
  62. /// }
  63. /// }
  64. ///
  65. /// public class MyTestClass
  66. /// {
  67. /// [Test, ConditionalIgnore("IgnoreInMacEditor", "Ignored on Mac editor.")]
  68. /// public void TestNeverRunningInMacEditor()
  69. /// {
  70. /// Assert.Pass();
  71. /// }
  72. /// }
  73. /// </code>
  74. /// </example>
  75. public static void AddConditionalIgnoreMapping(string key, bool value)
  76. {
  77. m_ConditionMap.Add(key.ToLowerInvariant(), value);
  78. }
  79. }
  80. }