Exemplo n.º 1
0
        private void ExecuteTest(MethodInfo method, object instance)
        {
            var attribute = (TestAttribute)method.GetCustomAttribute(typeof(TestAttribute));

            if (attribute.Ignore != null)
            {
                var testInfo = new IgnoredTestInfo(method, attribute.Ignore);
                testsInfo.Enqueue(testInfo);
                return;
            }

            var       isPassed   = false;
            Exception unexpected = null;
            var       stopwatch  = new Stopwatch();

            try
            {
                stopwatch.Start();
                method.Invoke(instance, null);
                isPassed = attribute.Expected == null;
            }
            catch (TargetInvocationException e)
            {
                isPassed   = e.InnerException.GetType() == attribute.Expected;
                unexpected = e.InnerException.GetType() == attribute.Expected ? null : e.InnerException;
            }
            finally
            {
                stopwatch.Stop();
                var time     = stopwatch.Elapsed;
                var testInfo = new TestResultInfo(method, isPassed, attribute.Expected, unexpected, time);
                testsInfo.Enqueue(testInfo);
            }
        }
Exemplo n.º 2
0
        private static void RunTest(MethodInfo method)
        {
            var instanceOfType          = Activator.CreateInstance(method.DeclaringType);
            var attrTemp                = Attribute.GetCustomAttribute(method, typeof(TestAttribute));
            var attr                    = (TestAttribute)Attribute.GetCustomAttributes(method).Where(t => Equals(t.GetType(), typeof(TestAttribute))).First();
            var ignore                  = attr.Ignore;
            var expectedException       = attr.Expected;
            var methodsBeforeTest       = MethodsWithAttributes <BeforeTestAttribute>(method.DeclaringType);
            var passedMethodsBeforeTest = RunMethodsWithAnnotation(methodsBeforeTest, new List <MethodInfo> {
                method
            }, "BeforeTest", instanceOfType);

            if (!passedMethodsBeforeTest)
            {
                return;
            }
            if (ignore != null)
            {
                var resultTest = new TestResultInfo(method.DeclaringType + " " + method.Name, true, ignore);
                testsResult.Add(resultTest);
                return;
            }
            Stopwatch watch = new Stopwatch();

            watch.Start();
            var result = RunMethod(method, instanceOfType);

            watch.Stop();
            var elapsedTime             = watch.ElapsedMilliseconds;
            var condTrueResultException = expectedException != null && (result.Exception) == expectedException;

            if (!condTrueResultException && expectedException != null)
            {
                var resultTest = new TestResultInfo(method.DeclaringType + " " + method.Name, false, $"Expected exception {expectedException}");
                testsResult.Add(resultTest);
                return;
            }
            if (result.Exception != null && expectedException == null)
            {
                var resultTest = new TestResultInfo(method.DeclaringType + " " + method.Name, false, $"Exception {result.Exception}");
                testsResult.Add(resultTest);
            }
            var methodsAfterTest = MethodsWithAttributes <AfterTestAttribute>(method.DeclaringType);
            var passedAfterTest  = RunMethodsWithAnnotation(methodsAfterTest, new List <MethodInfo> {
                method
            }, "AfterTest", instanceOfType);

            if (!passedAfterTest)
            {
                return;
            }
            if (condTrueResultException || result.Result)
            {
                var resultTest = new TestResultInfo(method.DeclaringType + " " + method.Name, elapsedTime);
                testsResult.Add(resultTest);
                return;
            }
        }