public void PerformTests() { foreach (var testType in _tests) { var suite = new TestSuite { Name = testType.Name }; var testObject = Activator.CreateInstance(testType); var methods = testType.GetMethods(); var tests = (from methodInfo in methods from customAttribute in methodInfo.GetCustomAttributes(true) where customAttribute.GetType() == typeof(NUnit.Framework.TestAttribute) select methodInfo); var setupMethod = (from methodInfo in methods from customAttribute in methodInfo.GetCustomAttributes(true) where customAttribute.GetType() == typeof(NUnit.Framework.SetUpAttribute) select methodInfo).FirstOrDefault(); var teardownMethod = (from methodInfo in methods from customAttribute in methodInfo.GetCustomAttributes(true) where customAttribute.GetType() == typeof(NUnit.Framework.TearDownAttribute) select methodInfo).FirstOrDefault(); foreach (var testMethod in tests) { InvokeMethodAndLogResult(suite, testType, testMethod, testObject, setupMethod, teardownMethod); } _suites.Add(suite); } }
private static void InvokeMethodAndLogResult(TestSuite suite, Type testClass, MethodInfo testMethod, object testObject, MethodInfo setupMethod, MethodInfo teardownMethod) { var result = new TestResult { Test = testMethod.Name }; try { if (setupMethod != null) { testClass.InvokeMember(setupMethod.Name, BindingFlags.Default | BindingFlags.InvokeMethod, null, testObject, null); } testClass.InvokeMember(testMethod.Name, BindingFlags.Default | BindingFlags.InvokeMethod, null, testObject, null); if (teardownMethod != null) { testClass.InvokeMember(teardownMethod.Name, BindingFlags.Default | BindingFlags.InvokeMethod, null, testObject, null); } result.Pass = true; } catch (Exception e) { result.Pass = false; result.Exception = e.GetBaseException().Message; result.StackTrace = e.GetBaseException().StackTrace; } suite.Results.Add(result); }