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

TestsCallback.cs 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #if TEST_FRAMEWORK
  2. using System;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEditor.TestTools.TestRunner.Api;
  6. using UnityEngine;
  7. namespace Packages.Rider.Editor.UnitTesting
  8. {
  9. internal class TestsCallback : ScriptableObject, IErrorCallbacks
  10. {
  11. public void RunFinished(ITestResultAdaptor result)
  12. {
  13. CallbackData.instance.isRider = false;
  14. CallbackData.instance.events.Add(
  15. new TestEvent(EventType.RunFinished, "", "","", 0, ParseTestStatus(result.TestStatus), ""));
  16. CallbackData.instance.RaiseChangedEvent();
  17. }
  18. public void RunStarted(ITestAdaptor testsToRun)
  19. {
  20. CallbackData.instance.events.Add(
  21. new TestEvent(EventType.RunStarted, "", "","", 0, NUnit.Framework.Interfaces.TestStatus.Passed, ""));
  22. CallbackData.instance.RaiseChangedEvent();
  23. }
  24. public void TestStarted(ITestAdaptor result)
  25. {
  26. // RIDER-69927 "Test not run" status is shown for the test suite when running unit tests for Unity project
  27. var method = result.Method ?? result.Children.Select(a=>a.Method).FirstOrDefault(b => b != null);
  28. if (method == null) return;
  29. CallbackData.instance.events.Add(
  30. new TestEvent(EventType.TestStarted, GenerateId(result), method.TypeInfo.Assembly.GetName().Name, "", 0, NUnit.Framework.Interfaces.TestStatus.Passed, GenerateId(result.Parent)));
  31. CallbackData.instance.RaiseChangedEvent();
  32. }
  33. public void TestFinished(ITestResultAdaptor result)
  34. {
  35. var method = result.Test.Method ?? result.Children.Select(a=>a.Test.Method).FirstOrDefault(b => b != null);
  36. if (method == null) return;
  37. CallbackData.instance.events.Add(
  38. new TestEvent(EventType.TestFinished, GenerateId(result.Test), method.TypeInfo.Assembly.GetName().Name, ExtractOutput(result), (result.EndTime-result.StartTime).Milliseconds, ParseTestStatus(result.TestStatus), GenerateId(result.Test.Parent)));
  39. CallbackData.instance.RaiseChangedEvent();
  40. }
  41. public void OnError(string message)
  42. {
  43. CallbackData.instance.isRider = false;
  44. CallbackData.instance.events.Add(
  45. new TestEvent(EventType.RunFinished, "", "",message, 0, NUnit.Framework.Interfaces.TestStatus.Failed, ""));
  46. CallbackData.instance.RaiseChangedEvent();
  47. }
  48. // see explanation in https://jetbrains.team/p/net/code/dotnet-libs/files/f04cde7d1dd70ee13bf5532e30f929b9b5ed08a4/ReSharperTestRunner/src/Adapters/TestRunner.Adapters.NUnit3/RemoteTaskDepot.cs?tab=source&line=129
  49. private static string GenerateId(ITestAdaptor node)
  50. {
  51. // ES: Parameterized tests defined in a parametrized test fixture, though
  52. // constructed for a particular test fixture with the given parameter, have identical fullname that does
  53. // not include parameters of parent testfixture (name of the without parameters is used instead).
  54. // This leads to 'Test with {id} id is already running' message.
  55. if (node.TypeInfo == null)
  56. return $"{node.Parent.FullName}.{node.Name}";
  57. return node.FullName;
  58. }
  59. private static NUnit.Framework.Interfaces.TestStatus ParseTestStatus(TestStatus testStatus)
  60. {
  61. return (NUnit.Framework.Interfaces.TestStatus)Enum.Parse(typeof(NUnit.Framework.Interfaces.TestStatus), testStatus.ToString());
  62. }
  63. private static string ExtractOutput(ITestResultAdaptor testResult)
  64. {
  65. var stringBuilder = new StringBuilder();
  66. if (testResult.Message != null)
  67. {
  68. stringBuilder.AppendLine("Message: ");
  69. stringBuilder.AppendLine(testResult.Message);
  70. }
  71. if (!string.IsNullOrEmpty(testResult.Output))
  72. {
  73. stringBuilder.AppendLine("Output: ");
  74. stringBuilder.AppendLine(testResult.Output);
  75. }
  76. if (!string.IsNullOrEmpty(testResult.StackTrace))
  77. {
  78. stringBuilder.AppendLine("Stacktrace: ");
  79. stringBuilder.AppendLine(testResult.StackTrace);
  80. }
  81. var result = stringBuilder.ToString();
  82. if (result.Length > 0)
  83. return result;
  84. return testResult.Output ?? string.Empty;
  85. }
  86. }
  87. }
  88. #endif