Exemple #1
0
            /// <summary/>
            public RunAssembly(Executor executor, object _handler)
            {
                ExecutorCallback handler = ExecutorCallback.Wrap(_handler);

                executor.RunOnSTAThreadWithPreservedWorkingDirectory(() =>
                {
                    bool @continue         = true;
                    AssemblyResult results =
                        new AssemblyResult(executor.assemblyFilename,
                                           AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);

                    foreach (Type type in executor.assembly.GetExportedTypes())
                    {
                        ITestClassCommand testClassCommand = TestClassCommandFactory.Make(type);

                        if (testClassCommand != null)
                        {
                            ClassResult classResult =
                                TestClassCommandRunner.Execute(testClassCommand,
                                                               null,
                                                               command => @continue = OnTestStart(command, handler),
                                                               result => @continue  = OnTestResult(result, handler));

                            results.Add(classResult);
                        }

                        if (!@continue)
                        {
                            break;
                        }
                    }

                    OnTestResult(results, handler);
                });
            }
        /// <summary>
        /// Execute the <see cref="ITestClassCommand"/>.
        /// </summary>
        /// <param name="testClassCommand">The test class command to execute</param>
        /// <param name="methods">The methods to execute; if null or empty, all methods will be executed</param>
        /// <param name="startCallback">The start run callback</param>
        /// <param name="resultCallback">The end run result callback</param>
        /// <returns>A <see cref="ClassResult"/> with the results of the test run</returns>
        public static ClassResult Execute(ITestClassCommand testClassCommand,
                                          List<IMethodInfo> methods,
                                          Predicate<ITestCommand> startCallback,
                                          Predicate<ITestResult> resultCallback)
        {
            Guard.ArgumentNotNull("testClassCommand", testClassCommand);

            if (methods == null)
                methods = new List<IMethodInfo>();

            if (methods.Count == 0)
                foreach (IMethodInfo method in testClassCommand.EnumerateTestMethods())
                    methods.Add(method);

            ClassResult classResult = new ClassResult(testClassCommand.TypeUnderTest.Type);
            Exception fixtureException = testClassCommand.ClassStart();
            bool @continue = true;

            if (fixtureException == null)
            {
                List<IMethodInfo> runList = new List<IMethodInfo>();
                foreach (IMethodInfo method in testClassCommand.EnumerateTestMethods())
                    runList.Add(method);

                while (@continue && runList.Count > 0)
                {
                    int idx = testClassCommand.ChooseNextTest(runList.AsReadOnly());
                    IMethodInfo method = runList[idx];

                    if (methods.Contains(method))
                        foreach (ITestCommand command in TestCommandFactory.Make(testClassCommand, method))
                        {
                            if (startCallback != null)
                                @continue = startCallback(command);

                            if (!@continue)
                                break;

                            MethodResult methodResult = command.Execute(testClassCommand.ObjectUnderTest);
                            classResult.Add(methodResult);

                            if (resultCallback != null)
                                @continue = resultCallback(methodResult);

                            if (!@continue)
                                break;
                        }

                    runList.RemoveAt(idx);
                }
            }

            classResult.SetException(testClassCommand.ClassFinish() ?? fixtureException);

            if (resultCallback != null)
                resultCallback(classResult);

            return classResult;
        }
Exemple #3
0
        public void ConstructWithType()
        {
            ClassResult result = new ClassResult(typeof(object));

            Assert.Equal(typeof(object).FullName, result.FullyQualifiedName);
            Assert.Equal(typeof(object).Name, result.Name);
            Assert.Equal(typeof(object).Namespace, result.Namespace);
        }
Exemple #4
0
        public void ConstructWithStrings()
        {
            ClassResult result = new ClassResult("name", "fullname", "namespace");

            Assert.Equal("fullname", result.FullyQualifiedName);
            Assert.Equal("name", result.Name);
            Assert.Equal("namespace", result.Namespace);
        }
Exemple #5
0
        public void SetExceptionNull()
        {
            ClassResult result = new ClassResult(typeof(object));

            result.SetException(null);

            Assert.Null(result.ExceptionType);
            Assert.Null(result.Message);
            Assert.Null(result.StackTrace);
        }
Exemple #6
0
        public void SetNonAssertException()
        {
            ClassResult result = new ClassResult(typeof(object));
            Exception thrownException;

            try
            {
                throw new Exception("message");
            }
            catch (Exception ex)
            {
                thrownException = ex;
                result.SetException(ex);
            }

            Assert.Equal(thrownException.GetType().FullName, result.ExceptionType);
            Assert.Equal(result.ExceptionType + " : " + thrownException.Message, result.Message);
            Assert.Equal(thrownException.StackTrace, result.StackTrace);
        }
Exemple #7
0
            public RunTests(Executor executor, string _type, List <string> _methods, object _handler)
            {
                Guard.ArgumentNotNull("_type", _type);
                Guard.ArgumentNotNull("_methods", _methods);

                ExecutorCallback handler  = ExecutorCallback.Wrap(_handler);
                Type             realType = executor.assembly.GetType(_type);

                Guard.ArgumentValid("_type", "Type " + _type + " could not be found", realType != null);

                ITypeInfo         type             = Reflector.Wrap(realType);
                ITestClassCommand testClassCommand = TestClassCommandFactory.Make(type);

                List <IMethodInfo> methods = new List <IMethodInfo>();

                foreach (string _method in _methods)
                {
                    try
                    {
                        IMethodInfo method = type.GetMethod(_method);
                        Guard.ArgumentValid("_methods", "Could not find method " + _method + " in type " + _type, method != null);
                        methods.Add(method);
                    }
                    catch (AmbiguousMatchException)
                    {
                        throw new ArgumentException("Ambiguous method named " + _method + " in type " + _type);
                    }
                }

                if (testClassCommand == null)
                {
                    ClassResult result = new ClassResult(type.Type);
                    OnTestResult(result, handler);
                    return;
                }

                executor.RunOnSTAThreadWithPreservedWorkingDirectory(() =>
                                                                     TestClassCommandRunner.Execute(testClassCommand,
                                                                                                    methods,
                                                                                                    command => OnTestStart(command, handler),
                                                                                                    result => OnTestResult(result, handler)));
            }
    public void ToXml_WithChildren()
    {
        XmlDocument doc = new XmlDocument();
        doc.LoadXml("<foo/>");
        XmlNode parentNode = doc.ChildNodes[0];
        PassedResult passedResult = new PassedResult("foo", "bar", null, null);
        passedResult.ExecutionTime = 1.1;
        FailedResult failedResult = new FailedResult("foo", "bar", null, null, "extype", "message", "stack");
        failedResult.ExecutionTime = 2.2;
        SkipResult skipResult = new SkipResult("foo", "bar", null, null, "reason");
        ClassResult classResult = new ClassResult(typeof(object));
        classResult.Add(passedResult);
        classResult.Add(failedResult);
        classResult.Add(skipResult);
        AssemblyResult assemblyResult = new AssemblyResult(filename);
        assemblyResult.Add(classResult);

        XmlNode resultNode = assemblyResult.ToXml(parentNode);

        Assert.Equal("3.300", resultNode.Attributes["time"].Value);
        Assert.Equal("3", resultNode.Attributes["total"].Value);
        Assert.Equal("1", resultNode.Attributes["passed"].Value);
        Assert.Equal("1", resultNode.Attributes["failed"].Value);
        Assert.Equal("1", resultNode.Attributes["skipped"].Value);
        Assert.Single(resultNode.SelectNodes("class"));
    }
Exemple #9
0
            public RunTests(Executor executor, string _type, List<string> _methods, object _handler)
            {
                Guard.ArgumentNotNull("_type", _type);
                Guard.ArgumentNotNull("_methods", _methods);

                ExecutorCallback handler = ExecutorCallback.Wrap(_handler);
                Type realType = executor.assembly.GetType(_type);
                Guard.ArgumentValid("_type", "Type " + _type + " could not be found", realType != null);

                ITypeInfo type = Reflector.Wrap(realType);
                ITestClassCommand testClassCommand = TestClassCommandFactory.Make(type);

                List<IMethodInfo> methods = new List<IMethodInfo>();

                foreach (string _method in _methods)
                {
                    try
                    {
                        IMethodInfo method = type.GetMethod(_method);
                        Guard.ArgumentValid("_methods", "Could not find method " + _method + " in type " + _type, method != null);
                        methods.Add(method);
                    }
                    catch (AmbiguousMatchException)
                    {
                        throw new ArgumentException("Ambiguous method named " + _method + " in type " + _type);
                    }
                }

                if (testClassCommand == null)
                {
                    ClassResult result = new ClassResult(type.Type);
                    OnTestResult(result, handler);
                    return;
                }

                executor.RunOnSTAThreadWithPreservedWorkingDirectory(() =>
                    TestClassCommandRunner.Execute(testClassCommand,
                                                   methods,
                                                   command => OnTestStart(command, handler),
                                                   result => OnTestResult(result, handler)));
            }
Exemple #10
0
        public void SetNonAssertExceptionWithInnerException()
        {
            ClassResult result = new ClassResult(typeof(object));
            Exception thrownException = null;
            Exception innerException = null;

            try
            {
                try
                {
                    throw new InvalidOperationException();
                }
                catch (Exception ex)
                {
                    innerException = ex;
                    throw new Exception("message", ex);
                }
            }
            catch (Exception ex)
            {
                thrownException = ex;
                result.SetException(ex);
            }

            string expectedMessage = string.Format("{0} : {1}{2}---- {3} : {4}",
                                                   thrownException.GetType().FullName,
                                                   thrownException.Message,
                                                   Environment.NewLine,
                                                   innerException.GetType().FullName,
                                                   innerException.Message);

            Assert.Equal(expectedMessage, result.Message);

            string expectedStackTrace =
                string.Format("{0}{1}----- Inner Stack Trace -----{1}{2}",
                              thrownException.StackTrace,
                              Environment.NewLine,
                              innerException.StackTrace);

            Assert.Equal(expectedStackTrace, result.StackTrace);
        }
Exemple #11
0
        public void ToXmlTwiceDoesNotDoubleCounts()
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml("<foo/>");
            XmlNode parentNode = doc.ChildNodes[0];
            ClassResult classResult = new ClassResult(typeof(object));
            PassedResult passedResult = new PassedResult("foo", "bar", null, null);
            passedResult.ExecutionTime = 1.1;
            FailedResult failedResult = new FailedResult("foo", "bar", null, null, "extype", "message", "stack");
            failedResult.ExecutionTime = 2.2;
            SkipResult skipResult = new SkipResult("foo", "bar", null, null, "reason");
            classResult.Add(passedResult);
            classResult.Add(failedResult);
            classResult.Add(skipResult);

            XmlNode resultNode1 = classResult.ToXml(parentNode);
            XmlNode resultNode2 = classResult.ToXml(parentNode);

            Assert.Equal(resultNode1.Attributes["time"].Value, resultNode2.Attributes["time"].Value);
            Assert.Equal(resultNode1.Attributes["total"].Value, resultNode2.Attributes["total"].Value);
            Assert.Equal(resultNode1.Attributes["passed"].Value, resultNode2.Attributes["passed"].Value);
            Assert.Equal(resultNode1.Attributes["failed"].Value, resultNode2.Attributes["failed"].Value);
            Assert.Equal(resultNode1.Attributes["skipped"].Value, resultNode2.Attributes["skipped"].Value);
            Assert.Equal(resultNode1.SelectNodes("test").Count, resultNode2.SelectNodes("test").Count);
        }
Exemple #12
0
        public void ToXml_WithClassFailure()
        {
            Exception ex;

            try
            {
                throw new InvalidOperationException("message");
            }
            catch (Exception e)
            {
                ex = e;
            }

            XmlDocument doc = new XmlDocument();
            doc.LoadXml("<foo/>");
            XmlNode parentNode = doc.ChildNodes[0];
            ClassResult classResult = new ClassResult(typeof(object));
            classResult.SetException(ex);

            XmlNode resultNode = classResult.ToXml(parentNode);

            Assert.Equal("class", resultNode.Name);
            Assert.Equal(classResult.FullyQualifiedName, resultNode.Attributes["name"].Value);
            Assert.Equal("0.000", resultNode.Attributes["time"].Value);
            Assert.Equal("1", resultNode.Attributes["total"].Value);
            Assert.Equal("0", resultNode.Attributes["passed"].Value);
            Assert.Equal("1", resultNode.Attributes["failed"].Value);
            Assert.Equal("0", resultNode.Attributes["skipped"].Value);
            XmlNode failureNode = resultNode.SelectSingleNode("failure");
            Assert.Equal(ex.GetType().FullName, failureNode.Attributes["exception-type"].Value);
            Assert.Equal(ExceptionUtility.GetMessage(ex), failureNode.SelectSingleNode("message").InnerText);
            Assert.Equal(ExceptionUtility.GetStackTrace(ex), failureNode.SelectSingleNode("stack-trace").InnerText);
        }
Exemple #13
0
        public void ToXml()
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml("<foo/>");
            XmlNode parentNode = doc.ChildNodes[0];
            ClassResult classResult = new ClassResult(typeof(object));

            XmlNode resultNode = classResult.ToXml(parentNode);

            Assert.Equal("class", resultNode.Name);
            Assert.Equal(classResult.FullyQualifiedName, resultNode.Attributes["name"].Value);
            Assert.Equal("0.000", resultNode.Attributes["time"].Value);
            Assert.Equal("0", resultNode.Attributes["total"].Value);
            Assert.Equal("0", resultNode.Attributes["passed"].Value);
            Assert.Equal("0", resultNode.Attributes["failed"].Value);
            Assert.Equal("0", resultNode.Attributes["skipped"].Value);
        }
        public static ClassResult Execute(ITestClassCommand testClassCommand,
                                          List <IMethodInfo> methods,
                                          Predicate <ITestCommand> startCallback,
                                          Predicate <ITestResult> resultCallback)
        {
            if (methods == null)
            {
                methods = new List <IMethodInfo>();
            }

            if (methods.Count == 0)
            {
                foreach (IMethodInfo method in testClassCommand.EnumerateTestMethods())
                {
                    methods.Add(method);
                }
            }

            ClassResult classResult      = new ClassResult(testClassCommand.TypeUnderTest.Type);
            Exception   fixtureException = testClassCommand.ClassStart();
            bool        @continue        = true;

            if (fixtureException == null)
            {
                List <IMethodInfo> runList = new List <IMethodInfo>();
                foreach (IMethodInfo method in testClassCommand.EnumerateTestMethods())
                {
                    runList.Add(method);
                }

                while (@continue && runList.Count > 0)
                {
                    int         idx    = testClassCommand.ChooseNextTest(runList.AsReadOnly());
                    IMethodInfo method = runList[idx];

                    if (methods.Contains(method))
                    {
                        foreach (ITestCommand command in TestCommandFactory.Make(testClassCommand, method))
                        {
                            if (startCallback != null)
                            {
                                @continue = startCallback(command);
                            }

                            if (!@continue)
                            {
                                break;
                            }

                            MethodResult methodResult = command.Execute(testClassCommand.ObjectUnderTest);
                            classResult.Add(methodResult);

                            if (resultCallback != null)
                            {
                                @continue = resultCallback(methodResult);
                            }

                            if (!@continue)
                            {
                                break;
                            }
                        }
                    }

                    runList.RemoveAt(idx);
                }
            }

            classResult.SetException(testClassCommand.ClassFinish() ?? fixtureException);

            if (resultCallback != null)
            {
                resultCallback(classResult);
            }

            return(classResult);
        }