ExecutionSettings.cs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using System.Linq;
  3. using NUnit.Framework.Interfaces;
  4. using NUnit.Framework.Internal.Filters;
  5. using UnityEngine;
  6. namespace UnityEditor.TestTools.TestRunner.Api
  7. {
  8. /// <summary>
  9. /// A set of execution settings defining how to run tests, using the <see cref="TestRunnerApi"/>.
  10. /// </summary>
  11. [Serializable]
  12. public class ExecutionSettings
  13. {
  14. /// <summary>
  15. /// Creates an instance with a given set of filters, if any.
  16. /// </summary>
  17. /// <param name="filtersToExecute">Set of filters</param>
  18. public ExecutionSettings(params Filter[] filtersToExecute)
  19. {
  20. filters = filtersToExecute;
  21. }
  22. [SerializeField]
  23. internal BuildTarget? targetPlatform;
  24. /// <summary>
  25. /// An instance of <see cref="ITestRunSettings"/> to set up before running tests on a Player.
  26. /// </summary>
  27. // Note: Is not available after serialization
  28. public ITestRunSettings overloadTestRunSettings;
  29. [SerializeField]
  30. internal Filter filter;
  31. ///<summary>
  32. ///A collection of <see cref="Filter"/> to execute tests on.
  33. ///</summary>
  34. [SerializeField]
  35. public Filter[] filters;
  36. /// <summary>
  37. /// Note that this is only supported for EditMode tests, and that tests which take multiple frames (i.e. [UnityTest] tests, or tests with [UnitySetUp] or [UnityTearDown] scaffolding) will be filtered out.
  38. /// </summary>
  39. /// <returns>If true, the call to Execute() will run tests synchronously, guaranteeing that all tests have finished running by the time the call returns.</returns>
  40. [SerializeField]
  41. public bool runSynchronously;
  42. /// <summary>
  43. /// The time, in seconds, the editor should wait for heartbeats after starting a test run on a player. This defaults to 10 minutes.
  44. /// </summary>
  45. [SerializeField]
  46. public int playerHeartbeatTimeout = 60*10;
  47. internal bool EditModeIncluded()
  48. {
  49. return filters.Any(f => IncludesTestMode(f.testMode, TestMode.EditMode));
  50. }
  51. internal bool PlayModeInEditorIncluded()
  52. {
  53. return filters.Any(f => IncludesTestMode(f.testMode, TestMode.PlayMode) && targetPlatform == null);
  54. }
  55. internal bool PlayerIncluded()
  56. {
  57. return filters.Any(f => IncludesTestMode(f.testMode, TestMode.PlayMode) && targetPlatform != null);
  58. }
  59. private static bool IncludesTestMode(TestMode testMode, TestMode modeToCheckFor)
  60. {
  61. return (testMode & modeToCheckFor) == modeToCheckFor;
  62. }
  63. internal ITestFilter BuildNUnitFilter()
  64. {
  65. return new OrFilter(filters.Select(f => f.ToRuntimeTestRunnerFilter(runSynchronously).BuildNUnitFilter()).ToArray());
  66. }
  67. }
  68. }