public static TestResult RunTest(IKontrolModule module, IKontrolFunction testFunction, TestContextFactory contextFactory) { TestRunnerContext testContext = contextFactory(); try { testContext.ResetTimeout(); object testReturn = testFunction.Invoke(testContext); switch (testReturn) { case bool booleanResult when !booleanResult: return(new TestResult(module.Name + "::" + testFunction.Name, testContext.AssertionsCount, "Returned false", testContext.Messages)); case IAnyOption option when !option.Defined: return(new TestResult(module.Name + "::" + testFunction.Name, testContext.AssertionsCount, "Returned None", testContext.Messages)); case IAnyResult result when !result.Success: return(new TestResult(module.Name + "::" + testFunction.Name, testContext.AssertionsCount, $"Returned Err({result.ErrorString})", testContext.Messages)); case IAnyFuture future: ContextHolder.CurrentContext.Value = testContext; for (int i = 0; i < 100; i++) { testContext.IncrYield(); testContext.ResetTimeout(); IAnyFutureResult result = future.Poll(); if (result.IsReady) { return(new TestResult(module.Name + "::" + testFunction.Name, testContext.AssertionsCount, testContext.Messages)); } } return(new TestResult(module.Name + "::" + testFunction.Name, testContext.AssertionsCount, "Future did not become ready", testContext.Messages)); default: return(new TestResult(module.Name + "::" + testFunction.Name, testContext.AssertionsCount, testContext.Messages)); } } catch (AssertException e) { return(new TestResult(module.Name + "::" + testFunction.Name, testContext.AssertionsCount, e.Message, testContext.Messages)); } catch (Exception e) { Console.Error.WriteLine(e); return(new TestResult(module.Name + "::" + testFunction.Name, testContext.AssertionsCount, e, testContext.Messages)); } finally { ContextHolder.CurrentContext.Value = null; } }
public void RunTests(IEnumerable <TestCase> tests, IRunContext runContext, IFrameworkHandle frameworkHandle) { var testExecutor = new TestRunner(); var testCases = tests as IList <TestCase> ?? tests.ToList(); var allTestClassInstances = testCases.Select(testCase => testCase.LocalExtensionData).ToList(); var uniqueTestClassInstances = allTestClassInstances.DistinctBy(o => o.GetType().FullName).ToList(); RunMethodsWithAttribute <BeforeAllAttribute>(uniqueTestClassInstances); foreach (var testCase in testCases) { var instance = testCase.LocalExtensionData; var instanceAsArray = new[] { instance }; RunMethodsWithAttribute <BeforeEachAttribute>(instanceAsArray); var methodInfo = instance.GetType().GetMethod(testCase.FullyQualifiedName); frameworkHandle.RecordStart(testCase); var testRunnerContext = new TestRunnerContext(instance, methodInfo, testCase, frameworkHandle, _testingStopped); testExecutor.Run(testRunnerContext); RunMethodsWithAttribute <AfterEachAttribute>(instanceAsArray); } RunMethodsWithAttribute <AfterAllAttribute>(uniqueTestClassInstances); }