Exemplo n.º 1
0
        /**
         * Virtual method to output an individual test failure.
         * NOTE: Can be overridden to customize how errors are reported.
         *       Especially useful for displaying failed tests within the
         *       Unity3D console, etc.
         *
         * @param Exception error, the failed test exception to output.
         */
        public virtual void LogFailure(Exception error)
        {
            // If error valid
            if (null != error)
            {
                // If error is a test exception
                if (typeof(TestException) == error.GetType())
                {
                    // Write the summary
                    TestException te = error as TestException;
                    System.Console.WriteLine(te.Description);
                }

                // Log failure
                System.Console.WriteLine(error + "\n");
            }
        }
Exemplo n.º 2
0
        /**
         * Run the test, catching all exceptions.
         *
         * @param result, the result of the test.
         *
         * @return TestResult, the result of the test.
         */
        public IEnumerator Run(TestResult result)
        {
            // If test method invalid
            if (null == m_testMethod)
            {
                // Error
                throw new Exception("Invalid test method encountered, be sure to call TestCase::SetTestMethod()");
            }

            // If the test method does not exist
            Type       type   = GetType();
            MethodInfo method = type.GetMethod(m_testMethod);

            if (null == method)
            {
                // Error
                throw new Exception("Test method: " + m_testMethod + " does not exist in class: " + type.ToString());
            }

            // If result invalid
            if (null == result)
            {
                // Create result
                result = new TestResult();
            }

            // Pre-test setup
            SetUp();
            result.TestStarted();

            try
            {
                try
                {
                    // Try to call the test method
                    method.Invoke(this, null);
                }
                catch (TargetInvocationException e)
                {
                    // If we are expecting an exception
                    m_caughtEx = e;
                    if (null != Assert.Exception)
                    {
                        // Compare the exceptions
                        Assert.Equal(Assert.Exception, m_caughtEx.InnerException);
                    }
                    else
                    {
                        // If this is a test exception
                        if (null != e.InnerException &&
                            typeof(TestException) == e.InnerException.GetType())
                        {
                            // Set the description
                            TestException te = e.InnerException as TestException;
                            te.Description = "Failed: " + GetType() + "." + m_testMethod + "()";
                            if (null != te.StackFrame)
                            {
                                // Add stack frame info
                                te.Description += " in File: " + System.IO.Path.GetFileName(te.StackFrame.GetFileName());
                                te.Description += " on Line: " + te.StackFrame.GetFileLineNumber();
                            }
                        }

                        // Re-throw the exception to be tracked
                        throw m_caughtEx.InnerException;
                    }
                }

                // If we are expecting an exception but did not catch one
                if (null != Assert.Exception &&
                    null == m_caughtEx)
                {
                    // Error
                    Assert.NotNull(m_caughtEx, "Did not catch expected exception: " + Assert.Exception);
                }

                UnityEngine.Debug.Log("[SharpUnit] " + type.Name + "." + method.Name + " runs ok");
            }
            catch (Exception ex)
            {
                // Track test failure
                result.TestFailed(ex);
                UnityEngine.Debug.LogWarning("[SharpUnit] " + type.Name + "." + method.Name + " failed");
            }



            // Clear expected exception
            Assert.Exception = null;

            // Post-test cleanup
            TearDown();

            _TestResult = result;
            yield break;
        }
 protected void MarkAsFailure(TestException e)
 {
     _Failed    = true;
     _Exception = e;
 }
Exemplo n.º 4
0
        /**
         * Run the test, catching all exceptions.
         *
         * @param TestResult result, the result of the test.
         *
         * @return TestResult, the result of the test.
         */
        public TestResult Run(TestResult result)
        {
            // If test method invalid
            if (null == m_testMethod)
            {
                // Error
                throw new Exception("Invalid test method encountered, be sure to call TestCase::SetTestMethod()");
            }

            // If the test method does not exist
            Type       type   = GetType();
            MethodInfo method = type.GetMethod(m_testMethod);

            if (null == method)
            {
                // Error
                throw new Exception("Test method: " + m_testMethod + " does not exist in class: " + type.ToString());
            }

            // If result invalid
            if (null == result)
            {
                // Create result
                result = new TestResult();
            }

            // Pre-test setup
            SetUp();
            result.TestStarted();

            try
            {
                try
                {
                    // Try to call the test method
                    method.Invoke(this, null);
                }
                catch (TargetInvocationException e)
                {
                    // If we are expecting an exception
                    m_caughtEx = e;
                    if (null != Assert.ExpectedException)
                    {
                        // Compare the exceptions
                        Assert.Equal(Assert.ExpectedException, m_caughtEx.InnerException);
                    }
                    else
                    {
                        // If this is a test exception
                        if (null != e.InnerException &&
                            typeof(TestException) == e.InnerException.GetType())
                        {
                            // Set the description
                            TestException te = e.InnerException as TestException;
                            te.Description = "Failed: " + GetType() + "." + m_testMethod + "()";
                        }

                        // Re-throw the exception to be tracked
                        throw m_caughtEx.InnerException;
                    }
                }

                // If we are expecting an exception but did not catch one
                if (null != Assert.ExpectedException &&
                    null == m_caughtEx)
                {
                    // Error
                    Assert.NotNull(m_caughtEx, "Did not catch expected exception: " + Assert.ExpectedException);
                }
            }
            catch (Exception ex)
            {
                // Track test failure
                result.TestFailed(ex);
            }

            // Clear expected exception
            Assert.ExpectedException = null;

            // Post-test cleanup
            TearDown();

            return(result);
        }