/// <summary> /// Executes all methods of the type having TestAttribute /// </summary> /// <param name="type"></param> /// <returns></returns> public ClassResult ExecuteTestFixture(Type type) { var testMethods = AttributeParser.ExtractTestMethods<TestAttribute>(type); if (testMethods.Count == 0) return new ClassResult(type); ClassResult classResult = new ClassResult(type); object typeObject = Activator.CreateInstance(type); foreach (MethodInfo method in testMethods) { IgnoreAttribute attributeIgnore = AttributeParser.GetAttribute<IgnoreAttribute>(method); if (attributeIgnore != null) { var methodResult = new MethodResult(method); methodResult.Ignored = true; methodResult.Success = false; classResult.MethodResults.Add(methodResult); continue; } ExpectedExceptionAttribute attributeExpectedException = AttributeParser.GetAttribute<ExpectedExceptionAttribute>(method); if (attributeExpectedException != null && attributeExpectedException.ExpectedException != null) classResult.MethodResults.Add(ExecuteMethodExpectedException(typeObject, method, attributeExpectedException.ExpectedException)); else classResult.MethodResults.Add(ExecuteMethod(typeObject, method)); } return classResult; }
private MethodResult ExecuteMethodExpectedException(object classObject, MethodInfo method, Type expectedException) { var methodResult = new MethodResult(method); try { method.Invoke(classObject, null); } catch (Exception ex) { if (expectedException == ex.GetType() || (ex.InnerException != null && expectedException == ex.InnerException.GetType())) return methodResult; else { methodResult.Success = false; methodResult.Exception = ex; methodResult.FailMessage = "Unexpected exception occured: " + ex.Message; return methodResult; } } methodResult.Success = false; methodResult.FailMessage = "Expected exception didn't occur"; return methodResult; }
private MethodResult ExecuteMethod(object classObject, MethodInfo method) { var methodResult = new MethodResult(method); try { method.Invoke(classObject, null); } catch (Exception ex) { methodResult.Success = false; methodResult.Exception = ex; methodResult.FailMessage = "Unexpected exception occured: " + ex.Message; } return methodResult; }