TestRunnerResult.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor.TestTools.TestRunner.Api;
  5. using UnityEngine.TestTools.TestRunner.GUI;
  6. namespace UnityEditor.TestTools.TestRunner.GUI
  7. {
  8. [Serializable]
  9. internal class TestRunnerResult : TestRunnerFilter.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 Update(TestRunnerResult result)
  73. {
  74. if (ReferenceEquals(result, null))
  75. return;
  76. resultStatus = result.resultStatus;
  77. duration = result.duration;
  78. messages = result.messages;
  79. output = result.output;
  80. stacktrace = result.stacktrace;
  81. ignoredOrSkipped = result.ignoredOrSkipped;
  82. notRunnable = result.notRunnable;
  83. description = result.description;
  84. notOutdated = result.notOutdated;
  85. if (m_OnResultUpdate != null)
  86. m_OnResultUpdate(this);
  87. }
  88. public void SetResultChangedCallback(Action<TestRunnerResult> resultUpdated)
  89. {
  90. m_OnResultUpdate = resultUpdated;
  91. }
  92. [Serializable]
  93. internal enum ResultStatus
  94. {
  95. NotRun,
  96. Passed,
  97. Failed,
  98. Inconclusive,
  99. Skipped
  100. }
  101. private static ResultStatus ParseNUnitResultStatus(TestStatus status)
  102. {
  103. switch (status)
  104. {
  105. case TestStatus.Passed:
  106. return ResultStatus.Passed;
  107. case TestStatus.Failed:
  108. return ResultStatus.Failed;
  109. case TestStatus.Inconclusive:
  110. return ResultStatus.Inconclusive;
  111. case TestStatus.Skipped:
  112. return ResultStatus.Skipped;
  113. default:
  114. return ResultStatus.NotRun;
  115. }
  116. }
  117. public override string ToString()
  118. {
  119. return string.Format("{0} ({1})", name, fullName);
  120. }
  121. public string Id { get { return uniqueId; } }
  122. public string FullName { get { return fullName; } }
  123. public string ParentId { get { return parentUniqueId; } }
  124. public bool IsSuite { get { return isSuite; } }
  125. public List<string> Categories { get { return categories; } }
  126. public void Clear()
  127. {
  128. resultStatus = ResultStatus.NotRun;
  129. if (m_OnResultUpdate != null)
  130. m_OnResultUpdate(this);
  131. }
  132. }
  133. }