Exemplo n.º 1
0
        private async Task <int> ExecuteTest(UnitTest test, int testFailures, TestExpectations methodDesc)
        {
            test.Setup();

            try
            {
                if (methodDesc.Method.ReturnType == typeof(Task))
                {
                    Task  result = methodDesc.Method.Invoke(test, new object[0]) as Task;
                    await result;
                }
                else
                {
                    methodDesc.Method.Invoke(test, new object[0]);
                }

                if (methodDesc.ExpectedExceptionType != null)
                {
                    vm_.AddMessage(string.Format("Expected exception of type '{0}' but no exception was thrown.", methodDesc.ExpectedExceptionType.FullName));
                    testFailures++;
                }

                // success
            }
            catch (TargetInvocationException tie)
            {
                var inner     = tie.InnerException;
                var innerType = inner.GetType();
                if (innerType == typeof(AssertionFailedException))
                {
                    vm_.AddMessage(inner.ToString());
                    testFailures++;
                }
                else if (innerType != methodDesc.ExpectedExceptionType)
                {
                    // failure
                    vm_.AddMessage(new AssertionFailedException(test.Assert.TestName, "Threw unexpected exception", inner).ToString());
                    testFailures++;
                }
                else
                {
                    // success
                    if (methodDesc.ExpectedExceptionType != null)
                    {
                        test.Assert.Succeeded();
                    }
                }
            }
            catch (AssertionFailedException ex)
            {
                vm_.AddMessage(ex.ToString());
                testFailures++;
            }
            catch (Exception ex)
            {
                if (ex.GetType() != methodDesc.ExpectedExceptionType)
                {
                    // failure
                    vm_.AddMessage(ex.ToString());
                    testFailures++;
                }
                else
                {
                    // success
                    if (methodDesc.ExpectedExceptionType != null)
                    {
                        test.Assert.Succeeded();
                    }
                }
            }

            test.Cleanup();
            return(testFailures);
        }
Exemplo n.º 2
0
        private async Task<int> ExecuteTest(UnitTest test, int testFailures, TestExpectations methodDesc)
        {
            test.Setup();

            try
            {
                if (methodDesc.Method.ReturnType == typeof(Task))
                {
                    Task result = methodDesc.Method.Invoke(test, new object[0]) as Task;
                    await result;
                }
                else
                {
                    methodDesc.Method.Invoke(test, new object[0]);
                }

                if (methodDesc.ExpectedExceptionType != null)
                {
                    vm_.AddMessage(string.Format("Expected exception of type '{0}' but no exception was thrown.", methodDesc.ExpectedExceptionType.FullName));
                    testFailures++;
                }

                // success
            }
            catch (TargetInvocationException tie)
            {
                var inner = tie.InnerException;
                var innerType = inner.GetType();
                if (innerType == typeof(AssertionFailedException))
                {
                    vm_.AddMessage(inner.ToString());
                    testFailures++;
                }
                else if (innerType != methodDesc.ExpectedExceptionType)
                {
                    // failure
                    vm_.AddMessage(new AssertionFailedException(test.Assert.TestName, "Threw unexpected exception", inner).ToString());
                    testFailures++;
                }
                else
                {
                    // success
                    if (methodDesc.ExpectedExceptionType != null)
                        test.Assert.Succeeded();
                }
            }
            catch (AssertionFailedException ex)
            {
                vm_.AddMessage(ex.ToString());
                testFailures++;
            }
            catch (Exception ex)
            {
                if (ex.GetType() != methodDesc.ExpectedExceptionType)
                {
                    // failure
                    vm_.AddMessage(ex.ToString());
                    testFailures++;
                }
                else
                {
                    // success
                    if (methodDesc.ExpectedExceptionType != null)
                        test.Assert.Succeeded();
                }
            }

            test.Cleanup();
            return testFailures;
        }