Exemplo n.º 1
0
        /// <summary>
        /// Метод распределяет методы из класса по спискам в зависимости от его атрибутов.
        /// </summary>
        /// <param name="type">Класс, в котором производится поиск.</param>
        /// <param name="beforeClassTestMethods">Список для методов с атрибутом BeforeClass.</param>
        /// <param name="afterClassTestMethods">Список для методов с атрибутом AfterClass.</param>
        /// <param name="beforeTestMethods">Список для методов с атрибутом Before.</param>
        /// <param name="afterTestMethods">Список для методов с атрибутом After.</param>
        /// <param name="testMethods">Список для стандартных тестовых методов.</param>
        private void GetTestMethods(Type type, List <TestInfo> beforeClassTestMethods,
                                    List <TestInfo> afterClassTestMethods, List <TestInfo> beforeTestMethods,
                                    List <TestInfo> afterTestMethods, List <TestInfo> testMethods)
        {
            foreach (var method in type.GetMethods())
            {
                foreach (var attr in Attribute.GetCustomAttributes(method))
                {
                    if (attr.GetType() == typeof(BeforeClassAttribute))
                    {
                        if (method.IsStatic)
                        {
                            var test = new TestInfo(method, type, attr);
                            beforeClassTestMethods.Add(test);
                        }
                        else
                        {
                            var result = new TestResult(type.Name, method.Name)
                            {
                                IsOk          = false,
                                RealException = new Exception("BeforeClass Test wasn't static")
                            };
                            this.isBeforeClassCompleted = false;
                            lock (lockObject)
                            {
                                this.testResults.Add(result);
                            }
                        }
                    }

                    if (attr.GetType() == typeof(AfterClassAttribute))
                    {
                        if (method.IsStatic)
                        {
                            var test = new TestInfo(method, type, attr);
                            afterClassTestMethods.Add(test);
                        }
                        else
                        {
                            var result = new TestResult(type.Name, method.Name)
                            {
                                IsOk          = false,
                                RealException = new Exception("After Class Test wasn't static")
                            };
                            lock (lockObject)
                            {
                                this.testResults.Add(result);
                            }
                        }
                    }

                    if (attr.GetType() == typeof(BeforeAttribute))
                    {
                        var test = new TestInfo(method, type, attr);
                        beforeTestMethods.Add(test);
                    }

                    if (attr.GetType() == typeof(AfterAttribute))
                    {
                        var test = new TestInfo(method, type, attr);
                        afterTestMethods.Add(test);
                    }

                    if (attr.GetType() == typeof(TestAttribute))
                    {
                        var test = new TestInfo(method, type, attr);
                        testMethods.Add(test);
                    }
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Данный метод запускает тестовый метод и выполняет необходимые проверки.
        /// </summary>
        /// <param name="test">Тестовый метод.</param>
        /// <returns>Возвращает результат выполнения теста в виде TestResult.</returns>
        private TestResult RunTest(TestInfo test)
        {
            var result = new TestResult(test.TestType.Name, test.TestMethod.Name);

            if (test.Attr.GetType() == typeof(TestAttribute))
            {
                if (((TestAttribute)test.Attr).Ignore != null)
                {
                    result.IsOk = true;

                    result.WhyIgnored = ((TestAttribute)test.Attr).Ignore;
                    return(result);
                }
            }
            Stopwatch stopwatch = Stopwatch.StartNew();

            try
            {
                if (test.Attr.GetType() == typeof(BeforeClassAttribute) ||
                    test.Attr.GetType() == typeof(AfterClassAttribute))
                {
                    test.TestMethod.Invoke(test.TestType, null);
                }
                else
                {
                    test.TestMethod.Invoke(test.Object, null);
                }

                result.IsOk = true;
            }
            catch (Exception ex)
            {
                if (test.Attr.GetType() == typeof(TestAttribute))
                {
                    if (((TestAttribute)test.Attr).Expected != null)
                    {
                        var exc = ((TestAttribute)test.Attr).Expected;
                        result.Expected = exc;
                        if (ex.InnerException.GetType() == exc)
                        {
                            result.IsOk = true;
                        }
                        else
                        {
                            result.IsOk          = false;
                            result.RealException = ex.InnerException;
                        }
                    }
                    else
                    {
                        result.IsOk          = false;
                        result.RealException = ex.InnerException;
                    }
                }
                else
                {
                    result.IsOk          = false;
                    result.RealException = ex.InnerException;
                }
            }
            finally
            {
                stopwatch.Stop();
            }

            result.Time = stopwatch.ElapsedMilliseconds;

            return(result);
        }