TestRunnerApiMapper.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Xml;
  5. using UnityEditor.TestTools.TestRunner.Api;
  6. namespace UnityEditor.TestTools.TestRunner.UnityTestProtocol
  7. {
  8. internal class TestRunnerApiMapper : ITestRunnerApiMapper
  9. {
  10. public TestPlanMessage MapTestToTestPlanMessage(ITestAdaptor testsToRun)
  11. {
  12. var testsNames = testsToRun != null ? FlattenTestNames(testsToRun) : new List<string>();
  13. var msg = new TestPlanMessage
  14. {
  15. tests = testsNames
  16. };
  17. return msg;
  18. }
  19. public TestStartedMessage MapTestToTestStartedMessage(ITestAdaptor test)
  20. {
  21. return new TestStartedMessage
  22. {
  23. name = test.FullName
  24. };
  25. }
  26. public TestFinishedMessage TestResultToTestFinishedMessage(ITestResultAdaptor result)
  27. {
  28. return new TestFinishedMessage
  29. {
  30. name = result.Test.FullName,
  31. duration = Convert.ToUInt64(result.Duration * 1000),
  32. durationMicroseconds = Convert.ToUInt64(result.Duration * 1000000),
  33. message = result.Message,
  34. state = GetTestStateFromResult(result),
  35. stackTrace = result.StackTrace
  36. };
  37. }
  38. public string GetRunStateFromResultNunitXml(ITestResultAdaptor result)
  39. {
  40. var doc = new XmlDocument();
  41. doc.LoadXml(result.ToXml().OuterXml);
  42. return doc.FirstChild.Attributes["runstate"].Value;
  43. }
  44. public TestState GetTestStateFromResult(ITestResultAdaptor result)
  45. {
  46. var state = TestState.Failure;
  47. if (result.TestStatus == TestStatus.Passed)
  48. {
  49. state = TestState.Success;
  50. var runstate = GetRunStateFromResultNunitXml(result);
  51. runstate = runstate ?? String.Empty;
  52. if (runstate.ToLowerInvariant().Equals("explicit"))
  53. state = TestState.Skipped;
  54. }
  55. else if (result.TestStatus == TestStatus.Skipped)
  56. {
  57. state = TestState.Skipped;
  58. if (result.ResultState.ToLowerInvariant().EndsWith("ignored"))
  59. state = TestState.Ignored;
  60. }
  61. else
  62. {
  63. if (result.ResultState.ToLowerInvariant().Equals("inconclusive"))
  64. state = TestState.Inconclusive;
  65. if (result.ResultState.ToLowerInvariant().EndsWith("cancelled") ||
  66. result.ResultState.ToLowerInvariant().EndsWith("error"))
  67. state = TestState.Error;
  68. }
  69. return state;
  70. }
  71. public List<string> FlattenTestNames(ITestAdaptor test)
  72. {
  73. var results = new List<string>();
  74. if (!test.IsSuite)
  75. results.Add(test.FullName);
  76. if (test.Children != null && test.Children.Any())
  77. foreach (var child in test.Children)
  78. results.AddRange(FlattenTestNames(child));
  79. return results;
  80. }
  81. }
  82. }