TestAdaptor.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using NUnit.Framework;
  5. using NUnit.Framework.Interfaces;
  6. using NUnit.Framework.Internal;
  7. using UnityEngine.TestRunner.NUnitExtensions;
  8. using UnityEngine.TestRunner.NUnitExtensions.Runner;
  9. using UnityEngine.TestRunner.TestLaunchers;
  10. using UnityEngine.TestTools.Utils;
  11. namespace UnityEditor.TestTools.TestRunner.Api
  12. {
  13. internal class TestAdaptor : ITestAdaptor
  14. {
  15. internal TestAdaptor(ITest test, ITestAdaptor[] children = null)
  16. {
  17. Id = test.Id;
  18. Name = test.Name;
  19. var childIndex = -1;
  20. if (test.Properties["childIndex"].Count > 0)
  21. {
  22. childIndex = (int)test.Properties["childIndex"][0];
  23. }
  24. FullName = childIndex != -1 ? GetIndexedTestCaseName(test.FullName, childIndex) : test.FullName;
  25. TestCaseCount = test.TestCaseCount;
  26. HasChildren = test.HasChildren;
  27. IsSuite = test.IsSuite;
  28. if (UnityTestExecutionContext.CurrentContext != null)
  29. {
  30. TestCaseTimeout = UnityTestExecutionContext.CurrentContext.TestCaseTimeout;
  31. }
  32. else
  33. {
  34. TestCaseTimeout = CoroutineRunner.k_DefaultTimeout;
  35. }
  36. TypeInfo = test.TypeInfo;
  37. Method = test.Method;
  38. Categories = test.GetAllCategoriesFromTest().Distinct().ToArray();
  39. IsTestAssembly = test is TestAssembly;
  40. RunState = (RunState)Enum.Parse(typeof(RunState), test.RunState.ToString());
  41. Description = (string)test.Properties.Get(PropertyNames.Description);
  42. SkipReason = test.GetSkipReason();
  43. ParentId = test.GetParentId();
  44. ParentFullName = test.GetParentFullName();
  45. UniqueName = test.GetUniqueName();
  46. ParentUniqueName = test.GetParentUniqueName();
  47. ChildIndex = childIndex;
  48. if (test.Parent != null)
  49. {
  50. if (test.Parent.Parent == null) // Assembly level
  51. {
  52. TestMode = (TestMode)Enum.Parse(typeof(TestMode),test.Properties.Get("platform").ToString());
  53. }
  54. }
  55. Children = children;
  56. }
  57. public void SetParent(ITestAdaptor parent)
  58. {
  59. Parent = parent;
  60. if (parent != null)
  61. {
  62. TestMode = parent.TestMode;
  63. }
  64. }
  65. internal TestAdaptor(RemoteTestData test)
  66. {
  67. Id = test.id;
  68. Name = test.name;
  69. FullName = test.ChildIndex != -1 ? GetIndexedTestCaseName(test.fullName, test.ChildIndex) : test.fullName;
  70. TestCaseCount = test.testCaseCount;
  71. HasChildren = test.hasChildren;
  72. IsSuite = test.isSuite;
  73. m_ChildrenIds = test.childrenIds;
  74. TestCaseTimeout = test.testCaseTimeout;
  75. Categories = test.Categories;
  76. IsTestAssembly = test.IsTestAssembly;
  77. RunState = (RunState)Enum.Parse(typeof(RunState), test.RunState.ToString());
  78. Description = test.Description;
  79. SkipReason = test.SkipReason;
  80. ParentId = test.ParentId;
  81. UniqueName = test.UniqueName;
  82. ParentUniqueName = test.ParentUniqueName;
  83. ParentFullName = test.ParentFullName;
  84. ChildIndex = test.ChildIndex;
  85. TestMode = TestMode.PlayMode;
  86. }
  87. internal void ApplyChildren(IEnumerable<TestAdaptor> allTests)
  88. {
  89. Children = m_ChildrenIds.Select(id => allTests.First(t => t.Id == id)).ToArray();
  90. if (!string.IsNullOrEmpty(ParentId))
  91. {
  92. Parent = allTests.FirstOrDefault(t => t.Id == ParentId);
  93. }
  94. }
  95. public string Id { get; private set; }
  96. public string Name { get; private set; }
  97. public string FullName { get; private set; }
  98. public int TestCaseCount { get; private set; }
  99. public bool HasChildren { get; private set; }
  100. public bool IsSuite { get; private set; }
  101. public IEnumerable<ITestAdaptor> Children { get; private set; }
  102. public ITestAdaptor Parent { get; private set; }
  103. public int TestCaseTimeout { get; private set; }
  104. public ITypeInfo TypeInfo { get; private set; }
  105. public IMethodInfo Method { get; private set; }
  106. private string[] m_ChildrenIds;
  107. public string[] Categories { get; private set; }
  108. public bool IsTestAssembly { get; private set; }
  109. public RunState RunState { get; }
  110. public string Description { get; }
  111. public string SkipReason { get; }
  112. public string ParentId { get; }
  113. public string ParentFullName { get; }
  114. public string UniqueName { get; }
  115. public string ParentUniqueName { get; }
  116. public int ChildIndex { get; }
  117. public TestMode TestMode { get; private set; }
  118. private static string GetIndexedTestCaseName(string fullName, int index)
  119. {
  120. var generatedTestSuffix = " GeneratedTestCase" + index;
  121. if (fullName.EndsWith(")"))
  122. {
  123. // Test names from generated TestCaseSource look like Test(TestCaseSourceType)
  124. // This inserts a unique test case index in the name, so that it becomes Test(TestCaseSourceType GeneratedTestCase0)
  125. return fullName.Substring(0, fullName.Length - 1) + generatedTestSuffix + fullName[fullName.Length - 1];
  126. }
  127. // In some cases there can be tests with duplicate names generated in other ways and they won't have () in their name
  128. // We just append a suffix at the end of the name in that case
  129. return fullName + generatedTestSuffix;
  130. }
  131. }
  132. }