예제 #1
0
        /// <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;
        }
예제 #2
0
 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;
 }
예제 #3
0
 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;
 }