TestResultExtensions.cs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. using System;
  2. using NUnit.Framework;
  3. using NUnit.Framework.Interfaces;
  4. using NUnit.Framework.Internal;
  5. namespace UnityEngine.TestRunner.NUnitExtensions
  6. {
  7. internal static class TestResultExtensions
  8. {
  9. public static void RecordPrefixedException(this TestResult testResult, string prefix, Exception ex, ResultState resultState = null)
  10. {
  11. if (ex is NUnitException)
  12. {
  13. ex = ex.InnerException;
  14. }
  15. if (resultState == null)
  16. {
  17. resultState = testResult.ResultState == ResultState.Cancelled
  18. ? ResultState.Cancelled
  19. : ResultState.Error;
  20. }
  21. var exceptionMessage = ExceptionHelper.BuildMessage(ex);
  22. string stackTrace = "--" + prefix + NUnit.Env.NewLine + ExceptionHelper.BuildStackTrace(ex);
  23. if (testResult.StackTrace != null)
  24. {
  25. stackTrace = testResult.StackTrace + NUnit.Env.NewLine + stackTrace;
  26. }
  27. if (testResult.Test.IsSuite)
  28. {
  29. resultState = resultState.WithSite(FailureSite.TearDown);
  30. }
  31. if (ex is ResultStateException)
  32. {
  33. exceptionMessage = ex.Message;
  34. resultState = ((ResultStateException)ex).ResultState;
  35. stackTrace = StackFilter.Filter(ex.StackTrace);
  36. }
  37. string message = (string.IsNullOrEmpty(prefix) ? "" : (prefix + " : ")) + exceptionMessage;
  38. if (testResult.Message != null)
  39. {
  40. message = testResult.Message + NUnit.Env.NewLine + message;
  41. }
  42. testResult.SetResult(resultState, message, stackTrace);
  43. }
  44. public static void RecordPrefixedError(this TestResult testResult, string prefix, string error, ResultState resultState = null)
  45. {
  46. if (resultState == null)
  47. {
  48. resultState = testResult.ResultState == ResultState.Cancelled
  49. ? ResultState.Cancelled
  50. : ResultState.Error;
  51. }
  52. if (testResult.Test.IsSuite)
  53. {
  54. resultState = resultState.WithSite(FailureSite.TearDown);
  55. }
  56. string message = (string.IsNullOrEmpty(prefix) ? "" : (prefix + " : ")) + error;
  57. if (testResult.Message != null)
  58. {
  59. message = testResult.Message + NUnit.Env.NewLine + message;
  60. }
  61. testResult.SetResult(resultState, message);
  62. }
  63. }
  64. }