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.

TestRunnerApiMapper.cs 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Xml;
  6. using UnityEditor.TestTools.TestRunner.Api;
  7. using UnityEditor.TestTools.TestRunner.GUI;
  8. namespace UnityEditor.TestTools.TestRunner.UnityTestProtocol
  9. {
  10. internal class TestRunnerApiMapper : ITestRunnerApiMapper
  11. {
  12. internal IGuiHelper guiHelper = new GuiHelper(new MonoCecilHelper(), new AssetsDatabaseHelper());
  13. private readonly string _projectRepoPath;
  14. public TestRunnerApiMapper(string projectRepoPath)
  15. {
  16. _projectRepoPath = projectRepoPath;
  17. }
  18. public TestPlanMessage MapTestToTestPlanMessage(ITestAdaptor testsToRun)
  19. {
  20. var testsNames = testsToRun != null ? FlattenTestNames(testsToRun) : new List<string>();
  21. var msg = new TestPlanMessage
  22. {
  23. tests = testsNames
  24. };
  25. return msg;
  26. }
  27. public TestStartedMessage MapTestToTestStartedMessage(ITestAdaptor test)
  28. {
  29. return new TestStartedMessage
  30. {
  31. name = test.FullName
  32. };
  33. }
  34. public TestFinishedMessage TestResultToTestFinishedMessage(ITestResultAdaptor result)
  35. {
  36. string filePathString = default;
  37. int lineNumber = default;
  38. if (result.Test.Method != null && result.Test.TypeInfo != null)
  39. {
  40. var method = result.Test.Method.MethodInfo;
  41. var type = result.Test.TypeInfo.Type;
  42. var fileOpenInfo = guiHelper.GetFileOpenInfo(type, method);
  43. filePathString = !string.IsNullOrEmpty(_projectRepoPath) ? Path.Combine(_projectRepoPath, fileOpenInfo.FilePath) : fileOpenInfo.FilePath;
  44. lineNumber = fileOpenInfo.LineNumber;
  45. }
  46. var iteration = 0;
  47. if(result is TestResultAdaptor)
  48. {
  49. var adaptor = ((TestResultAdaptor)result);
  50. iteration = adaptor.RepeatIteration == 0 ? adaptor.RetryIteration : adaptor.RepeatIteration;
  51. }
  52. return new TestFinishedMessage
  53. {
  54. name = result.Test.FullName,
  55. duration = Convert.ToUInt64(result.Duration * 1000),
  56. durationMicroseconds = Convert.ToUInt64(result.Duration * 1000000),
  57. message = result.Message,
  58. state = GetTestStateFromResult(result),
  59. stackTrace = result.StackTrace,
  60. fileName = filePathString,
  61. lineNumber = lineNumber,
  62. iteration = iteration
  63. };
  64. }
  65. public string GetRunStateFromResultNunitXml(ITestResultAdaptor result)
  66. {
  67. var doc = new XmlDocument();
  68. doc.LoadXml(result.ToXml().OuterXml);
  69. return doc.FirstChild.Attributes["runstate"].Value;
  70. }
  71. public TestState GetTestStateFromResult(ITestResultAdaptor result)
  72. {
  73. var state = TestState.Failure;
  74. if (result.TestStatus == TestStatus.Passed)
  75. {
  76. state = TestState.Success;
  77. }
  78. else if (result.TestStatus == TestStatus.Skipped)
  79. {
  80. state = TestState.Skipped;
  81. if (result.ResultState.ToLowerInvariant().EndsWith("ignored"))
  82. {
  83. state = TestState.Ignored;
  84. }
  85. }
  86. else
  87. {
  88. if (result.ResultState.ToLowerInvariant().Equals("inconclusive"))
  89. {
  90. state = TestState.Inconclusive;
  91. }
  92. if (result.ResultState.ToLowerInvariant().EndsWith("cancelled") ||
  93. result.ResultState.ToLowerInvariant().EndsWith("error"))
  94. {
  95. state = TestState.Error;
  96. }
  97. }
  98. return state;
  99. }
  100. public List<string> FlattenTestNames(ITestAdaptor test)
  101. {
  102. var results = new List<string>();
  103. if (!test.IsSuite)
  104. results.Add(test.FullName);
  105. if (test.Children != null && test.Children.Any())
  106. foreach (var child in test.Children)
  107. results.AddRange(FlattenTestNames(child));
  108. return results;
  109. }
  110. }
  111. }