No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestResultRenderer.cs 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using NUnit.Framework.Interfaces;
  5. using NUnit.Framework.Internal;
  6. namespace UnityEngine.TestTools.TestRunner.Callbacks
  7. {
  8. internal class TestResultRenderer
  9. {
  10. private static class Styles
  11. {
  12. public static readonly GUIStyle SucceedLabelStyle;
  13. public static readonly GUIStyle FailedLabelStyle;
  14. public static readonly GUIStyle FailedMessagesStyle;
  15. static Styles()
  16. {
  17. SucceedLabelStyle = new GUIStyle("label");
  18. SucceedLabelStyle.normal.textColor = Color.green;
  19. SucceedLabelStyle.fontSize = 48;
  20. FailedLabelStyle = new GUIStyle("label");
  21. FailedLabelStyle.normal.textColor = Color.red;
  22. FailedLabelStyle.fontSize = 32;
  23. FailedMessagesStyle = new GUIStyle("label");
  24. FailedMessagesStyle.wordWrap = false;
  25. FailedMessagesStyle.richText = true;
  26. }
  27. }
  28. private readonly List<ITestResult> m_FailedTestCollection;
  29. private bool m_ShowResults;
  30. private Vector2 m_ScrollPosition;
  31. public TestResultRenderer(ITestResult testResults)
  32. {
  33. m_FailedTestCollection = new List<ITestResult>();
  34. GetFailedTests(testResults);
  35. }
  36. private void GetFailedTests(ITestResult testResults)
  37. {
  38. if (testResults is TestCaseResult)
  39. {
  40. if (testResults.ResultState.Status == TestStatus.Failed)
  41. m_FailedTestCollection.Add(testResults);
  42. }
  43. else if (testResults.HasChildren)
  44. {
  45. foreach (var testResultsChild in testResults.Children)
  46. {
  47. GetFailedTests(testResultsChild);
  48. }
  49. }
  50. }
  51. private const int k_MaxStringLength = 15000;
  52. public void ShowResults()
  53. {
  54. m_ShowResults = true;
  55. Cursor.visible = true;
  56. }
  57. public void Draw()
  58. {
  59. if (!m_ShowResults) return;
  60. if (m_FailedTestCollection.Count == 0)
  61. {
  62. GUILayout.Label("All test(s) succeeded", Styles.SucceedLabelStyle, GUILayout.Width(600));
  63. }
  64. else
  65. {
  66. int count = m_FailedTestCollection.Count;
  67. GUILayout.Label(count + " tests failed!", Styles.FailedLabelStyle);
  68. m_ScrollPosition = GUILayout.BeginScrollView(m_ScrollPosition, GUILayout.ExpandWidth(true));
  69. var text = "";
  70. text += "<b><size=18>Code-based tests</size></b>\n";
  71. text += string.Join("\n", m_FailedTestCollection
  72. .Select(result => result.Name + " " + result.ResultState + "\n" + result.Message)
  73. .ToArray());
  74. if (text.Length > k_MaxStringLength)
  75. text = text.Substring(0, k_MaxStringLength);
  76. GUILayout.TextArea(text, Styles.FailedMessagesStyle);
  77. GUILayout.EndScrollView();
  78. }
  79. if (GUILayout.Button("Close"))
  80. Application.Quit();
  81. }
  82. }
  83. }