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.

TestRunCallbackAttribute.cs 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using UnityEngine.Scripting;
  3. namespace UnityEngine.TestRunner
  4. {
  5. /// <summary>
  6. /// An assembly level attribute that indicates that a given type implementing <see cref = "ITestRunCallback"/> should be subscribed to updates on the test progress. You can invoke the callbacks with [NUnit](http://www.nunit.org/) `ITest` and `ITestResult` classes.
  7. ///
  8. /// At the `RunStarted` and `RunFinished` methods, the test and test results are for the whole test tree. These methods invoke at each node in the test tree; first with the whole test assembly, then with the test class, and last with the test method.
  9. ///
  10. /// From these callbacks, it's possible to read the partial or the full results, and to save the XML version of the result for further processing or continuous integration.
  11. /// </summary>
  12. /// <example>
  13. /// <code>
  14. /// <![CDATA[
  15. /// using NUnit.Framework.Interfaces;
  16. /// using UnityEngine;
  17. /// using UnityEngine.TestRunner;
  18. ///
  19. /// [assembly:TestRunCallback(typeof(TestListener))]
  20. ///
  21. /// public class TestListener : ITestRunCallback
  22. /// {
  23. /// public void RunStarted(ITest testsToRun)
  24. /// {
  25. ///
  26. /// }
  27. ///
  28. /// public void RunFinished(ITestResult testResults)
  29. /// {
  30. /// Debug.Log($"Run finished with result {testResults.ResultState}.");
  31. /// }
  32. ///
  33. /// public void TestStarted(ITest test)
  34. /// {
  35. ///
  36. /// }
  37. ///
  38. /// public void TestFinished(ITestResult result)
  39. /// {
  40. ///
  41. /// }
  42. ///}
  43. /// ]]>
  44. /// </code>
  45. /// > Note: The `TestRunCallback` does not need any references to the `UnityEditor` namespace and can run in standalone Players on the Player side.
  46. /// </example>
  47. [AttributeUsage(AttributeTargets.Assembly)]
  48. public class TestRunCallbackAttribute : Attribute
  49. {
  50. private Type m_Type;
  51. /// <summary>
  52. /// Constructs a new instance of the <see cref="TestRunCallbackAttribute"/> class.
  53. /// </summary>
  54. /// <param name="type">A target type that implements <see cref="ITestRunCallback"/>.</param>
  55. /// <exception cref="ArgumentException">Throws an ArgumentException if the provided type does not implement <see cref="ITestRunCallback"/>.</exception>
  56. public TestRunCallbackAttribute(Type type)
  57. {
  58. var interfaceType = typeof(ITestRunCallback);
  59. if (!interfaceType.IsAssignableFrom(type))
  60. {
  61. throw new ArgumentException(string.Format(
  62. "Type {2} provided to {0} does not implement {1}. If the stripping level is set to high, the implementing class should have the {3}.",
  63. GetType().Name, interfaceType.Name, type.Name, typeof(PreserveAttribute).Name));
  64. }
  65. m_Type = type;
  66. }
  67. internal ITestRunCallback ConstructCallback()
  68. {
  69. return Activator.CreateInstance(m_Type) as ITestRunCallback;
  70. }
  71. }
  72. }