Ingen beskrivning
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 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. return new TestFinishedMessage
  47. {
  48. name = result.Test.FullName,
  49. duration = Convert.ToUInt64(result.Duration * 1000),
  50. durationMicroseconds = Convert.ToUInt64(result.Duration * 1000000),
  51. message = result.Message,
  52. state = GetTestStateFromResult(result),
  53. stackTrace = result.StackTrace,
  54. fileName = filePathString,
  55. lineNumber = lineNumber
  56. };
  57. }
  58. public string GetRunStateFromResultNunitXml(ITestResultAdaptor result)
  59. {
  60. var doc = new XmlDocument();
  61. doc.LoadXml(result.ToXml().OuterXml);
  62. return doc.FirstChild.Attributes["runstate"].Value;
  63. }
  64. public TestState GetTestStateFromResult(ITestResultAdaptor result)
  65. {
  66. var state = TestState.Failure;
  67. if (result.TestStatus == TestStatus.Passed)
  68. {
  69. state = TestState.Success;
  70. }
  71. else if (result.TestStatus == TestStatus.Skipped)
  72. {
  73. state = TestState.Skipped;
  74. if (result.ResultState.ToLowerInvariant().EndsWith("ignored"))
  75. state = TestState.Ignored;
  76. }
  77. else
  78. {
  79. if (result.ResultState.ToLowerInvariant().Equals("inconclusive"))
  80. state = TestState.Inconclusive;
  81. if (result.ResultState.ToLowerInvariant().EndsWith("cancelled") ||
  82. result.ResultState.ToLowerInvariant().EndsWith("error"))
  83. state = TestState.Error;
  84. }
  85. return state;
  86. }
  87. public List<string> FlattenTestNames(ITestAdaptor test)
  88. {
  89. var results = new List<string>();
  90. if (!test.IsSuite)
  91. results.Add(test.FullName);
  92. if (test.Children != null && test.Children.Any())
  93. foreach (var child in test.Children)
  94. results.AddRange(FlattenTestNames(child));
  95. return results;
  96. }
  97. }
  98. }