UnitTestData() { foreach (var testType in GetTestTargetTypes()) { var setupFixture = testType.GetCustomAttribute <SetUpFixtureAttribute>(true); if (setupFixture != null) { var instance = Activator.CreateInstance(testType); var methods = testType.GetMethods(System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public); foreach (var item in methods) { RegisterAttributeAction <OneTimeSetUpAttribute>(item, instance, globalSetups); RegisterAttributeAction <OneTimeTearDownAttribute>(item, instance, globalTearDowns); } continue; } else { var group = new TestGroup(testType); testGroups[testType.FullName] = group; } } }
IEnumerator RunTestInCoroutine(string key, TestGroup testGroup, bool withoutGlobalSetup) { Button self = null; foreach (var btn in list.GetComponentsInChildren <Button>()) { btn.interactable = false; if (btn.name == key) { self = btn; } } if (self != null) { self.GetComponent <Image>().color = normalColor; } var allGreen = true; AppendToGraphicText("<color=yellow>" + testGroup.Name + "</color>\n"); WriteToConsole("Begin Test Class: " + testGroup.Name); yield return(null); // GlobalSetup if (!withoutGlobalSetup) { foreach (var item in this.testData.GlobalSetups) { item(); } } // OnetimeSetup foreach (var item in testGroup.OnetimeSetups) { item(); } var totalExecutionTime = new List <double>(); foreach (var item2 in testGroup.Tests) { try { AppendToGraphicText("<color=teal>" + item2.name + "</color>\n"); yield return(null); // SetUp foreach (var setup in testGroup.Setups) { setup(); } // UnitySetUp foreach (var setup in testGroup.UnitySetUps) { yield return(StartCoroutine(setup())); } // before start, cleanup GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); var v = item2.test; var methodStopwatch = System.Diagnostics.Stopwatch.StartNew(); Exception exception = null; if (v is Action) { try { ((Action)v).Invoke(); } catch (Exception ex) { exception = ex; } } else { var coroutineFactory = (Func <IEnumerator>)v; IEnumerator coroutine = null; try { coroutine = coroutineFactory(); } catch (Exception ex) { exception = ex; } if (exception == null) { yield return(StartCoroutine(UnwrapEnumerator(coroutine, ex => { exception = ex; }))); } } methodStopwatch.Stop(); totalExecutionTime.Add(methodStopwatch.Elapsed.TotalMilliseconds); if (exception == null) { AppendToGraphicText("OK, " + methodStopwatch.Elapsed.TotalMilliseconds.ToString("0.00") + "ms\n"); WriteToConsoleResult(item2.name + ", " + methodStopwatch.Elapsed.TotalMilliseconds.ToString("0.00") + "ms", true); } else { AppendToGraphicText("<color=red>" + exception.ToString() + "</color>\n"); WriteToConsoleResult(item2.name + ", " + exception.ToString(), false); allGreen = false; allTestGreen = false; } } finally { // TearDown foreach (var teardown in testGroup.TearDowns) { teardown(); } } // UnityTearDown foreach (var teardown in testGroup.UnityTearDowns) { yield return(StartCoroutine(teardown())); } } // OnetimeTearDown foreach (var item in testGroup.OneTimeTearDowns) { item(); } // GlobalTeardown if (!withoutGlobalSetup) { foreach (var item in this.testData.GlobalTearDowns) { item(); } } AppendToGraphicText("[" + testGroup.Name + "]" + totalExecutionTime.Sum().ToString("0.00") + "ms\n\n"); foreach (var btn in list.GetComponentsInChildren <Button>()) { btn.interactable = true; } if (self != null) { self.GetComponent <Image>().color = allGreen ? passColor : failColor; } yield return(StartCoroutine(ScrollLogToEndNextFrame())); }