Açıklama Yok
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.

ConditionalIgnoreAttribute.cs 3.3KB

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