Beispiel #1
0
        /// <summary>
        /// Обзор атрибутов метода, добавление информации о методе в соответсвующую коллекцию в
        /// <see cref="MethodsClassification"/>.
        /// </summary>
        /// <param name="methodInfo">Информация о методе.</param>
        /// <param name="methods">Коллекции данных, содержащих информацию о методах типа.</param>
        private static void AttributesEnumeration(MethodInfo methodInfo,
                                                  MethodsClassification methods)
        {
            foreach (var attribute in Attribute.GetCustomAttributes(methodInfo))
            {
                var attributeType = attribute.GetType();

                if (attributeType == typeof(TestAttribute))
                {
                    methods.TestMethods.Add(new TestMetadata(methodInfo, attribute as TestAttribute));
                }
                else if (attributeType == typeof(BeforeClassAttribute))
                {
                    methods.BeforeClassMethods.Add(methodInfo);
                }
                else if (attributeType == typeof(AfterClassAttribute))
                {
                    methods.AfterClassMethods.Add(methodInfo);
                }
                else if (attributeType == typeof(AfterAttribute))
                {
                    methods.AfterMethods.Add(methodInfo);
                }
                else if (attributeType == typeof(BeforeAttribute))
                {
                    methods.BeforeMethods.Add(methodInfo);
                }
                else
                {
                    continue;
                }

                return;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Параллельный запуск тестов.
        /// </summary>
        /// <param name="methods">Коллекции данных, содержащих информацию о методах типа.</param>
        /// <param name="type">Тип, содержащий тестовые методы.</param>
        /// <returns>Информация о результатах выполнения тестов.</returns>
        private static List <ITestExecutionInfo> RunTests(MethodsClassification methods, Type type)
        {
            var tasks = new Task <ITestExecutionInfo> [methods.TestMethods.Count];

            for (int i = 0; i < tasks.Length; i++)
            {
                int j = i;
                tasks[j] = Task.Factory.StartNew(()
                                                 => RunTest(methods.TestMethods[j], methods, type));
            }

            Task.WaitAll(tasks);

            var testsResultInfo = new List <ITestExecutionInfo>();

            for (int i = 0; i < tasks.Length; i++)
            {
                testsResultInfo.Add(tasks[i].Result);
            }

            return(testsResultInfo);
        }
Beispiel #3
0
        /// <summary>
        /// Запуск тестовых методов в заданном типе.
        /// </summary>
        /// <param name="type">Тип, в котором выполняется запуск тестовых методов.</param>
        private static void RunTestsInType(Type type)
        {
            try
            {
                Activator.CreateInstance(type);
            }
            catch (Exception)
            {
                return;
            }

            var methods = new MethodsClassification();

            foreach (var methodInfo in type.GetMethods())
            {
                AttributesEnumeration(methodInfo, methods);
            }

            if (methods.TestMethods.Count == 0 ||
                !ExecuteAuxiliaryClassMethods(methods.BeforeClassMethods, methods.TestMethods, type))
            {
                return;
            }

            var testsResultInfo = RunTests(methods, type);

            if (!ExecuteAuxiliaryClassMethods(methods.AfterClassMethods, methods.TestMethods, type))
            {
                return;
            }

            foreach (var testResultInfo in testsResultInfo)
            {
                testsExecutionInfo.Add(testResultInfo);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Запуск теста.
        /// </summary>
        /// <param name="metadata">Информация о тестовом методе.</param>
        /// <param name="methods">Коллекции данных, содержащих информацию о методах типа.</param>
        /// <param name="type">Тип, содержащий тестовые методы.</param>
        /// <returns>Информация о результате выполнения теста.</returns>
        private static ITestExecutionInfo RunTest(TestMetadata metadata, MethodsClassification methods, Type type)
        {
            try
            {
                CheckMethod(metadata.MethodInfo);
            }
            catch (IncorrectMethodException exception)
            {
                return(new IndefiniteTestExecutionInfo(GetFullName(metadata.MethodInfo, type), exception));
            }

            object instanceOfType = Activator.CreateInstance(type);

            var beforeMethodException = ExecuteAuxiliaryMethods(methods.BeforeMethods, instanceOfType);

            if (beforeMethodException != null)
            {
                return(new IndefiniteTestExecutionInfo(GetFullName(metadata.MethodInfo, type), beforeMethodException));
            }

            if (metadata.Attribute.Ignore != null)
            {
                var afterMethodExceptionIgnoreTest = ExecuteAuxiliaryMethods(methods.AfterMethods, instanceOfType);

                if (afterMethodExceptionIgnoreTest != null)
                {
                    return(new IndefiniteTestExecutionInfo(GetFullName(metadata.MethodInfo, type),
                                                           afterMethodExceptionIgnoreTest));
                }

                return(new IgnoreTestExecutionInfo(GetFullName(metadata.MethodInfo, type), metadata.Attribute.Ignore));
            }

            var       stopwatch         = new Stopwatch();
            Exception testException     = null;
            bool      isCaughtException = false;

            try
            {
                stopwatch.Start();
                metadata.MethodInfo.Invoke(instanceOfType, null);
            }
            catch (Exception exception)
            {
                isCaughtException = true;

                if (metadata.Attribute.Expected != exception.InnerException.GetType())
                {
                    testException = exception.InnerException;
                }
            }
            finally
            {
                stopwatch.Stop();
            }

            if (metadata.Attribute.Expected != null && (!isCaughtException || testException != null))
            {
                testException = new ExpectedExceptionWasNotThrown();
            }

            var afterMethodException = ExecuteAuxiliaryMethods(methods.AfterMethods, instanceOfType);

            if (afterMethodException != null)
            {
                return(new IndefiniteTestExecutionInfo(GetFullName(metadata.MethodInfo, type), afterMethodException));
            }

            return(new DefaultTestExecutionInfo(GetFullName(metadata.MethodInfo, type), stopwatch.ElapsedMilliseconds,
                                                testException));
        }