暂无描述
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

TestRunnerApiMapper.cs 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. }
  51. else if (result.TestStatus == TestStatus.Skipped)
  52. {
  53. state = TestState.Skipped;
  54. if (result.ResultState.ToLowerInvariant().EndsWith("ignored"))
  55. state = TestState.Ignored;
  56. }
  57. else
  58. {
  59. if (result.ResultState.ToLowerInvariant().Equals("inconclusive"))
  60. state = TestState.Inconclusive;
  61. if (result.ResultState.ToLowerInvariant().EndsWith("cancelled") ||
  62. result.ResultState.ToLowerInvariant().EndsWith("error"))
  63. state = TestState.Error;
  64. }
  65. return state;
  66. }
  67. public List<string> FlattenTestNames(ITestAdaptor test)
  68. {
  69. var results = new List<string>();
  70. if (!test.IsSuite)
  71. results.Add(test.FullName);
  72. if (test.Children != null && test.Children.Any())
  73. foreach (var child in test.Children)
  74. results.AddRange(FlattenTestNames(child));
  75. return results;
  76. }
  77. }
  78. }