TestRunnerResult.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor.TestTools.TestRunner.Api;
  5. namespace UnityEditor.TestTools.TestRunner.GUI
  6. {
  7. [Serializable]
  8. internal class TestRunnerResult : UITestRunnerFilter.IClearableResult
  9. {
  10. public string id;
  11. public string uniqueId;
  12. public string name;
  13. public string fullName;
  14. public ResultStatus resultStatus = ResultStatus.NotRun;
  15. public float duration;
  16. public string messages;
  17. public string output;
  18. public string stacktrace;
  19. public bool notRunnable;
  20. public bool ignoredOrSkipped;
  21. public string description;
  22. public bool isSuite;
  23. public List<string> categories;
  24. public string parentId;
  25. public string parentUniqueId;
  26. //This field is suppose to mark results from before domain reload
  27. //Such result is outdated because the code might haev changed
  28. //This field will get reset every time a domain reload happens
  29. [NonSerialized]
  30. public bool notOutdated;
  31. protected Action<TestRunnerResult> m_OnResultUpdate;
  32. internal TestRunnerResult(ITestAdaptor test)
  33. {
  34. id = test.Id;
  35. uniqueId = test.UniqueName;
  36. fullName = test.FullName;
  37. name = test.Name;
  38. description = test.Description;
  39. isSuite = test.IsSuite;
  40. ignoredOrSkipped = test.RunState == RunState.Ignored || test.RunState == RunState.Skipped;
  41. notRunnable = test.RunState == RunState.NotRunnable;
  42. if (ignoredOrSkipped)
  43. {
  44. messages = test.SkipReason;
  45. }
  46. if (notRunnable)
  47. {
  48. resultStatus = ResultStatus.Failed;
  49. messages = test.SkipReason;
  50. }
  51. categories = test.Categories.ToList();
  52. parentId = test.ParentId;
  53. parentUniqueId = test.ParentUniqueName;
  54. }
  55. internal TestRunnerResult(ITestResultAdaptor testResult) : this(testResult.Test)
  56. {
  57. notOutdated = true;
  58. messages = testResult.Message;
  59. output = testResult.Output;
  60. stacktrace = testResult.StackTrace;
  61. duration = (float)testResult.Duration;
  62. if (testResult.Test.IsSuite && testResult.ResultState == "Ignored")
  63. {
  64. resultStatus = ResultStatus.Passed;
  65. }
  66. else
  67. {
  68. resultStatus = ParseNUnitResultStatus(testResult.TestStatus);
  69. }
  70. }
  71. public void Update(TestRunnerResult result)
  72. {
  73. if (ReferenceEquals(result, null))
  74. return;
  75. resultStatus = result.resultStatus;
  76. duration = result.duration;
  77. messages = result.messages;
  78. output = result.output;
  79. stacktrace = result.stacktrace;
  80. ignoredOrSkipped = result.ignoredOrSkipped;
  81. notRunnable = result.notRunnable;
  82. description = result.description;
  83. notOutdated = result.notOutdated;
  84. if (m_OnResultUpdate != null)
  85. m_OnResultUpdate(this);
  86. }
  87. public void SetResultChangedCallback(Action<TestRunnerResult> resultUpdated)
  88. {
  89. m_OnResultUpdate = resultUpdated;
  90. }
  91. [Serializable]
  92. internal enum ResultStatus
  93. {
  94. NotRun,
  95. Passed,
  96. Failed,
  97. Inconclusive,
  98. Skipped
  99. }
  100. private static ResultStatus ParseNUnitResultStatus(TestStatus status)
  101. {
  102. switch (status)
  103. {
  104. case TestStatus.Passed:
  105. return ResultStatus.Passed;
  106. case TestStatus.Failed:
  107. return ResultStatus.Failed;
  108. case TestStatus.Inconclusive:
  109. return ResultStatus.Inconclusive;
  110. case TestStatus.Skipped:
  111. return ResultStatus.Skipped;
  112. default:
  113. return ResultStatus.NotRun;
  114. }
  115. }
  116. public override string ToString()
  117. {
  118. return string.Format("{0} ({1})", name, fullName);
  119. }
  120. public string Id { get { return uniqueId; } }
  121. public string FullName { get { return fullName; } }
  122. public string ParentId { get { return parentUniqueId; } }
  123. public bool IsSuite { get { return isSuite; } }
  124. public List<string> Categories { get { return categories; } }
  125. public void Clear()
  126. {
  127. resultStatus = ResultStatus.NotRun;
  128. if (m_OnResultUpdate != null)
  129. m_OnResultUpdate(this);
  130. }
  131. }
  132. }