/// <summary> /// Executes the actual test and returns a result. /// </summary> /// <param name="method"> /// The TestMethod to execute. /// </param> /// <param name="instance"> /// The class instance the method resides in. /// </param> /// <param name="testExecuting"> /// Callback when the test is executing. /// </param> /// <param name="testExecuted"> /// Callback when the test has completed. /// </param> private void RunTest(MethodInfo method, object instance, OnTestExecuting testExecuting = null, OnTestExecuted testExecuted = null) { bool result = false; Exception error = null; string classname = instance.GetType().Name; string methodname = method.Name; if (testExecuting != null) { testExecuting(classname, methodname); } // PREPARE var tokenSource = new CancellationTokenSource(); var token = tokenSource.Token; var execute_test = new Task(() => { try { method.Invoke(instance, null); result = true; } catch (Exception ex) { result = false; error = ex; } if (token.IsCancellationRequested) { /* cancel currently running operations and clean up as needed here.. */ } token.ThrowIfCancellationRequested(); }); // EXECUTE execute_test.Start(); execute_test.Wait(UnitTester.Timeout, token); if (testExecuted != null) { testExecuted(classname, methodname, result, error); } }
/// <summary> /// Runs all the TestMethods in the TestClass /// </summary> /// <param name="testExecuting"> /// Callback when the test is executing. /// </param> /// <param name="testExecuted"> /// Callback when the test has completed. /// </param> public void RunTests(OnTestExecuting testExecuting = null, OnTestExecuted testExecuted = null) { if (!(this.TestMethods != null && this.TestMethods.Count > 0)) { return; } foreach (var method in this.TestMethods) { RunTest(method, instance, testExecuting, testExecuted); } }