Ejemplo n.º 1
0
        public void Multiple_ContainsAllExceptions_IfMultipleActionsThrow()
        {
            TestFailureException expectedException = null;

            try
            {
                NrAssert.Multiple(
                    () => { throw new IndexOutOfRangeException(); },
                    () => Assert.IsTrue(true),
                    () => { throw new NullReferenceException(); }
                    );
            }
            catch (TestFailureException ex)
            {
                expectedException = ex;
            }

            Assert.NotNull(expectedException);

            var exception1Index = expectedException.Message.IndexOf("IndexOutOfRangeException", StringComparison.InvariantCultureIgnoreCase);
            var exception2Index = expectedException.Message.IndexOf("NullReferenceException", exception1Index + 1, StringComparison.InvariantCultureIgnoreCase);

            Assert.IsTrue(exception1Index > -1);
            Assert.IsTrue(exception2Index > -1);
        }
Ejemplo n.º 2
0
        public void Multiple_ContainsAllAssertionFailures_IfMultipleAssertionsFail()
        {
            TestFailureException expectedException = null;

            try
            {
                NrAssert.Multiple(
                    () => Assert.IsTrue(false),
                    () => Assert.IsTrue(true),
                    () => Assert.IsTrue(false)
                    );
            }
            catch (TestFailureException ex)
            {
                expectedException = ex;
            }

            Assert.NotNull(expectedException);

            var failure1Index = expectedException.Message.IndexOf("assertion", StringComparison.InvariantCultureIgnoreCase);
            var failure2Index = expectedException.Message.IndexOf("assertion", failure1Index + 1, StringComparison.InvariantCultureIgnoreCase);

            Assert.IsTrue(failure1Index > -1);
            Assert.IsTrue(failure2Index > -1);
        }
Ejemplo n.º 3
0
        public void Multiple_ContainsAllExceptionsAndAssertionFailures_IfAssertionsFailureAndThrowAtTheSameTime()
        {
            TestFailureException expectedException = null;

            try
            {
                NrAssert.Multiple(
                    () => { throw new IndexOutOfRangeException(); },
                    () => Assert.IsTrue(true),
                    () => Assert.IsTrue(false)
                    );
            }
            catch (TestFailureException ex)
            {
                expectedException = ex;
            }

            Assert.NotNull(expectedException);

            var exceptionIndex = expectedException.Message.IndexOf("IndexOutOfRangeException", StringComparison.InvariantCultureIgnoreCase);
            var failureIndex   = expectedException.Message.IndexOf("assertion", exceptionIndex + 1, StringComparison.InvariantCultureIgnoreCase);

            Assert.IsTrue(exceptionIndex > -1);
            Assert.IsTrue(failureIndex > -1);
        }
Ejemplo n.º 4
0
        public void Throws_ThrowsTestFailureException_IfTargetDoesNotThrowAnException()
        {
            TestFailureException testException = null;

            try
            {
                NrAssert.Throws <NullReferenceException>(() => { });
            }
            catch (TestFailureException ex)
            {
                testException = ex;
            }

            Assert.NotNull(testException);
            Assert.AreEqual("Expected exception of type 'System.NullReferenceException' was not thrown.", testException.Message);
        }
Ejemplo n.º 5
0
        public void Throws_ThrowsTestFailureException_IfTargetThrowsIncompatibleExceptionType()
        {
            TestFailureException testFailureException = null;

            try
            {
                NrAssert.Throws <NullReferenceException>(() => { throw new InvalidOperationException(); });
            }
            catch (TestFailureException ex)
            {
                testFailureException = ex;
            }

            Assert.NotNull(testFailureException);
            Assert.IsTrue(testFailureException.Message.StartsWith(
                              "Expected exception of type 'System.NullReferenceException', but exception of type 'System.InvalidOperationException' was thrown instead",
                              StringComparison.InvariantCultureIgnoreCase));
        }
Ejemplo n.º 6
0
        public void Multiple_Throws_IfAnyActionThrows()
        {
            TestFailureException expectedException = null;

            try
            {
                NrAssert.Multiple(
                    () => Assert.IsTrue(true),
                    () => { throw new IndexOutOfRangeException(); }
                    );
            }
            catch (TestFailureException ex)
            {
                expectedException = ex;
            }

            Assert.NotNull(expectedException);
            Assert.IsTrue(expectedException.Message.IndexOf("IndexOutOfRangeException", StringComparison.InvariantCultureIgnoreCase) > -1);
        }
Ejemplo n.º 7
0
        private void Run(Type testClass)
        {
            var oldConsoleColor = Console.ForegroundColor;

            MethodInfo testInitializeMethod = null;
            MethodInfo testCleanupMethod = null;
            var methodsToRun = new List<MethodInfo>();

            foreach (var method in testClass.GetMethods())
            {
                if (method.IsDefined(typeof(TestInitializeAttribute), false))
                {
                    testInitializeMethod = method;
                }

                if (method.IsDefined(typeof(TestCleanupAttribute), false))
                {
                    testCleanupMethod = method;
                }

                if (method.IsDefined(typeof(TestMethodAttribute), false))
                {
                    methodsToRun.Add(method);
                }
            }

            foreach (var method in methodsToRun)
            {
                Exception gotException = null;

                var originalOut = Console.Out;
                var originalErr = Console.Error;

                var newOut = new StringWriter();
                var newError = new StringWriter();

                Console.SetOut(newOut);
                Console.SetError(newError);

                try
                {
                    var instance = Activator.CreateInstance(testClass);
                    if (testInitializeMethod != null)
                    {
                        testInitializeMethod.Invoke(instance, null);
                    }

                    method.Invoke(instance, null);

                    if (testCleanupMethod != null)
                    {
                        testCleanupMethod.Invoke(instance, null);
                    }
                }
                catch (TargetInvocationException ex)
                {
                    gotException = ex.InnerException;
                }
                finally
                {
                    Console.SetOut(originalOut);
                    Console.SetError(originalErr);
                }

                string stdout = newOut.ToString();
                string stderr = newError.ToString();

                var expectedExceptionAttribute = (ExpectedExceptionAttribute)Attribute.GetCustomAttribute(method, typeof(ExpectedExceptionAttribute), false);

                bool passed;
                
                if (gotException != null)
                {
                    if (expectedExceptionAttribute == null)
                    {
                        passed = false;
                    }
                    else
                    {
                        if (expectedExceptionAttribute.ExceptionType.IsAssignableFrom(gotException.GetType()))
                        {
                            passed = true;
                        }
                        else
                        {
                            passed = false;
                            gotException = new TestFailureException("Expected exception " + expectedExceptionAttribute.ExceptionType + ", but got " + gotException.GetType() + ".");
                        }
                    }
                }
                else
                {
                    if (expectedExceptionAttribute == null)
                    {
                        passed = true;
                    }
                    else
                    {
                        passed = false;
                        gotException = new TestFailureException("Expected exception " + expectedExceptionAttribute.ExceptionType + " which was not thrown.");
                    }
                }

                if (!passed)
                {
                    stderr += gotException.ToString();
                }

                if (!string.IsNullOrEmpty(stdout))
                {
                    this.consoleOutput[method] = stdout;
                }

                if (!string.IsNullOrEmpty(stderr))
                {
                    this.consoleError[method] = stderr;
                }

                if (passed)
                {
                    if (this.PrintPassedTests)
                    {
                        Console.Write("{0}.{1} ", testClass.Name, method.Name);
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.Write("PASSED");
                        Console.ForegroundColor = oldConsoleColor;
                        Console.WriteLine();
                    }

                    this.PassedTests.Add(method);
                    this.PassCount++;
                }
                else
                {
                    Console.Write("{0}.{1} ", testClass.Name, method.Name);
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.Write("FAILED {0}", gotException);
                    Console.ForegroundColor = oldConsoleColor;
                    Console.WriteLine();
                    this.FailCount++;
                    this.FailedTests.Add(method);
                }
            }
        }
Ejemplo n.º 8
0
        private void Run(Type testClass)
        {
            var oldConsoleColor = Console.ForegroundColor;

            MethodInfo testInitializeMethod = null;
            MethodInfo testCleanupMethod    = null;
            var        methodsToRun         = new List <MethodInfo>();

            foreach (var method in testClass.GetMethods())
            {
                if (method.IsDefined(typeof(TestInitializeAttribute), false))
                {
                    testInitializeMethod = method;
                }

                if (method.IsDefined(typeof(TestCleanupAttribute), false))
                {
                    testCleanupMethod = method;
                }

                if (method.IsDefined(typeof(TestMethodAttribute), false))
                {
                    methodsToRun.Add(method);
                }
            }

            foreach (var method in methodsToRun)
            {
                Exception gotException = null;

                var originalOut = Console.Out;
                var originalErr = Console.Error;

                var newOut   = new StringWriter();
                var newError = new StringWriter();

                Console.SetOut(newOut);
                Console.SetError(newError);

                try
                {
                    var instance = Activator.CreateInstance(testClass);
                    if (testInitializeMethod != null)
                    {
                        testInitializeMethod.Invoke(instance, null);
                    }

                    method.Invoke(instance, null);

                    if (testCleanupMethod != null)
                    {
                        testCleanupMethod.Invoke(instance, null);
                    }
                }
                catch (TargetInvocationException ex)
                {
                    gotException = ex.InnerException;
                }
                finally
                {
                    Console.SetOut(originalOut);
                    Console.SetError(originalErr);
                }

                string stdout = newOut.ToString();
                string stderr = newError.ToString();

                var expectedExceptionAttribute = (ExpectedExceptionAttribute)Attribute.GetCustomAttribute(method, typeof(ExpectedExceptionAttribute), false);

                bool passed;

                if (gotException != null)
                {
                    if (expectedExceptionAttribute == null)
                    {
                        passed = false;
                    }
                    else
                    {
                        if (expectedExceptionAttribute.ExceptionType.IsAssignableFrom(gotException.GetType()))
                        {
                            passed = true;
                        }
                        else
                        {
                            passed       = false;
                            gotException = new TestFailureException("Expected exception " + expectedExceptionAttribute.ExceptionType + ", but got " + gotException.GetType() + ".");
                        }
                    }
                }
                else
                {
                    if (expectedExceptionAttribute == null)
                    {
                        passed = true;
                    }
                    else
                    {
                        passed       = false;
                        gotException = new TestFailureException("Expected exception " + expectedExceptionAttribute.ExceptionType + " which was not thrown.");
                    }
                }

                if (!passed)
                {
                    stderr += gotException.ToString();
                }

                if (!string.IsNullOrEmpty(stdout))
                {
                    this.consoleOutput[method] = stdout;
                }

                if (!string.IsNullOrEmpty(stderr))
                {
                    this.consoleError[method] = stderr;
                }

                if (passed)
                {
                    if (this.PrintPassedTests)
                    {
                        Console.Write("{0}.{1} ", testClass.Name, method.Name);
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.Write("PASSED");
                        Console.ForegroundColor = oldConsoleColor;
                        Console.WriteLine();
                    }

                    this.PassedTests.Add(method);
                    this.PassCount++;
                }
                else
                {
                    Console.Write("{0}.{1} ", testClass.Name, method.Name);
                    Console.ForegroundColor = ConsoleColor.Green;
                    Console.Write("FAILED {0}", gotException);
                    Console.ForegroundColor = oldConsoleColor;
                    Console.WriteLine();
                    this.FailCount++;
                    this.FailedTests.Add(method);
                }
            }
        }