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.

TestRunnerResult.cs 6.2KB

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