TestsCallback.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #if TEST_FRAMEWORK
  2. using System;
  3. using System.Text;
  4. using UnityEditor.TestTools.TestRunner.Api;
  5. using UnityEngine;
  6. namespace Packages.Rider.Editor.UnitTesting
  7. {
  8. public class TestsCallback : ScriptableObject, ICallbacks
  9. {
  10. public void RunFinished(ITestResultAdaptor result)
  11. {
  12. CallbackData.instance.isRider = false;
  13. CallbackData.instance.events.Add(
  14. new TestEvent(EventType.RunFinished, "", "","", 0, ParseTestStatus(result.TestStatus), ""));
  15. CallbackData.instance.RaiseChangedEvent();
  16. }
  17. public void TestStarted(ITestAdaptor result)
  18. {
  19. if (result.Method == null) return;
  20. CallbackData.instance.events.Add(
  21. new TestEvent(EventType.TestStarted, GetUniqueName(result), result.Method.TypeInfo.Assembly.GetName().Name, "", 0, ParseTestStatus(TestStatus.Passed), result.ParentFullName));
  22. CallbackData.instance.RaiseChangedEvent();
  23. }
  24. public void TestFinished(ITestResultAdaptor result)
  25. {
  26. if (result.Test.Method == null) return;
  27. CallbackData.instance.events.Add(
  28. new TestEvent(EventType.TestFinished, GetUniqueName(result.Test), result.Test.Method.TypeInfo.Assembly.GetName().Name, ExtractOutput(result), result.Duration, ParseTestStatus(result.TestStatus), result.Test.ParentFullName));
  29. CallbackData.instance.RaiseChangedEvent();
  30. }
  31. // todo: reimplement JetBrains.Rider.Unity.Editor.AfterUnity56.UnitTesting.TestEventsSender.GetUniqueName
  32. private static string GetUniqueName(ITestAdaptor test)
  33. {
  34. string str = test.FullName;
  35. return str;
  36. }
  37. public void RunStarted(ITestAdaptor testsToRun)
  38. {
  39. }
  40. private static NUnit.Framework.Interfaces.TestStatus ParseTestStatus(TestStatus testStatus)
  41. {
  42. return (NUnit.Framework.Interfaces.TestStatus)Enum.Parse(typeof(NUnit.Framework.Interfaces.TestStatus), testStatus.ToString());
  43. }
  44. private static string ExtractOutput(ITestResultAdaptor testResult)
  45. {
  46. var stringBuilder = new StringBuilder();
  47. if (testResult.Message != null)
  48. {
  49. stringBuilder.AppendLine("Message: ");
  50. stringBuilder.AppendLine(testResult.Message);
  51. }
  52. if (!string.IsNullOrEmpty(testResult.Output))
  53. {
  54. stringBuilder.AppendLine("Output: ");
  55. stringBuilder.AppendLine(testResult.Output);
  56. }
  57. if (!string.IsNullOrEmpty(testResult.StackTrace))
  58. {
  59. stringBuilder.AppendLine("Stacktrace: ");
  60. stringBuilder.AppendLine(testResult.StackTrace);
  61. }
  62. var result = stringBuilder.ToString();
  63. if (result.Length > 0)
  64. return result;
  65. return testResult.Output ?? string.Empty;
  66. }
  67. }
  68. }
  69. #endif