Esempio n. 1
0
        /// <summary>
        /// Runs a given test.
        /// </summary>
        /// <param name="testName">name of test to run.</param>
        /// <param name="test">test action</param>
        /// <returns>true if the test passes and false otherwise.</returns>
        private static bool RunTestMethod(TestInfo t)
        {
            if (s_listOnly)
            {
                Logger.LogInformation("{0}", t.Name);
                return(true);
            }

            s_numTests++;
            bool passed = true;

            try
            {
                Logger.LogInformation("--------------------------------");
                if (t.InstanceClass != null && t.InstanceClass.TestInitializer != null)
                {
                    Logger.LogInformation("Running TestInitializer for {0}", t.Name);
                    t.InstanceClass.TestInitializer();
                }

                try
                {
                    Logger.LogInformation("Running Test: {0}", t.Name);
                    if (t.IsTask)
                    {
                        var rv = Task.Run(async() => await asyncRunner(t.AsFunc())).Result;
                    }
                    else
                    {
                        t.AsAction();
                    }

                    if (t.ExpectsException != null)
                    {
                        Assert.Fail(
                            "Test did not throw expected exception: {0}. {1}",
                            t.ExpectsException.ExceptionType.ToString(),
                            t.ExpectsException.Description != null ? t.ExpectsException.Description : "");
                    }
                }
                catch (Exception ex)
                {
                    if (t.ExpectsException == null)
                    {
                        throw;
                    }
                    else
                    {
                        if (!t.ExpectsException.ExceptionType.GetTypeInfo().IsAssignableFrom(ex.GetType().GetTypeInfo()))
                        {
                            throw;
                        }
                        else
                        {
                            passed = true;
                        }
                    }
                }

                s_numPassedTests++;
                if (t.InstanceClass != null && t.InstanceClass.TestCleanup != null)
                {
                    Logger.LogInformation("Running TestCleanup for {0}", t.Name);
                    t.InstanceClass.TestCleanup();
                }
            }
            catch (AssertTestException ex)
            {
                Logger.LogInformation(ex.ToString());
                passed = false;
                s_numFailedTests++;
            }
            catch (AggregateException ae)
            {
                // Only handles AssertTestExceptions and lets any other exceptions stop the program.
                ae.Handle(exception =>
                {
                    if (exception is AssertTestException)
                    {
                        Logger.LogInformation(exception.ToString());
                        passed = false;
                        s_numFailedTests++;
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                });
            }


            // Does not catch Exception because we want to fail fast and generate a dump
            // file when an unexpected exception is encountered. (Except for ExpectedException cases.)

            Logger.LogInformation("---- Test {0} ---------------", passed ? "PASSED" : "FAILED");
            Logger.LogInformation(string.Empty);
            return(passed);
        }