Exemplo n.º 1
0
        public int Execute(ConsoleOptions options)
        {
            XmlTextReader transformReader = GetTransformReader(options);

            if (transformReader == null)
            {
                return(FILE_NOT_FOUND);
            }

            TextWriter outWriter      = Console.Out;
            bool       redirectOutput = options.output != null && options.output != string.Empty;

            if (redirectOutput)
            {
                StreamWriter outStreamWriter = new StreamWriter(options.output);
                outStreamWriter.AutoFlush = true;
                outWriter = outStreamWriter;
            }

            TextWriter errorWriter   = Console.Error;
            bool       redirectError = options.err != null && options.err != string.Empty;

            if (redirectError)
            {
                StreamWriter errorStreamWriter = new StreamWriter(options.err);
                errorStreamWriter.AutoFlush = true;
                errorWriter = errorStreamWriter;
            }

            TestRunner testRunner = MakeRunnerFromCommandLine(options);

            try
            {
                if (testRunner.Test == null)
                {
                    testRunner.Unload();
                    Console.Error.WriteLine("Unable to locate fixture {0}", options.fixture);
                    return(FIXTURE_NOT_FOUND);
                }

                EventCollector collector = new EventCollector(options, outWriter, errorWriter);

                TestFilter testFilter = TestFilter.Empty;
                if (options.run != null && options.run != string.Empty)
                {
                    Console.WriteLine("Selected test: " + options.run);
                    testFilter = new SimpleNameFilter(options.run);
                }

                if (options.include != null && options.include != string.Empty)
                {
                    Console.WriteLine("Included categories: " + options.include);
                    TestFilter includeFilter = new CategoryExpression(options.include).Filter;
                    if (testFilter.IsEmpty)
                    {
                        testFilter = includeFilter;
                    }
                    else
                    {
                        testFilter = new AndFilter(testFilter, includeFilter);
                    }
                }

                if (options.exclude != null && options.exclude != string.Empty)
                {
                    Console.WriteLine("Excluded categories: " + options.exclude);
                    TestFilter excludeFilter = new NotFilter(new CategoryExpression(options.exclude).Filter);
                    if (testFilter.IsEmpty)
                    {
                        testFilter = excludeFilter;
                    }
                    else if (testFilter is AndFilter)
                    {
                        ((AndFilter)testFilter).Add(excludeFilter);
                    }
                    else
                    {
                        testFilter = new AndFilter(testFilter, excludeFilter);
                    }
                }

                TestResult result         = null;
                string     savedDirectory = Environment.CurrentDirectory;
                TextWriter savedOut       = Console.Out;
                TextWriter savedError     = Console.Error;

                try
                {
                    result = testRunner.Run(collector, testFilter);
                }
                finally
                {
                    outWriter.Flush();
                    errorWriter.Flush();

                    if (redirectOutput)
                    {
                        outWriter.Close();
                    }
                    if (redirectError)
                    {
                        errorWriter.Close();
                    }

                    Environment.CurrentDirectory = savedDirectory;
                    Console.SetOut(savedOut);
                    Console.SetError(savedError);
                }

                Console.WriteLine();

                if (result != null)
                {
                    string xmlOutput = CreateXmlOutput(result);

                    if (options.xmlConsole)
                    {
                        Console.WriteLine(xmlOutput);
                    }
                    else
                    {
                        try
                        {
                            //CreateSummaryDocument(xmlOutput, transformReader );
                            XmlResultTransform xform = new XmlResultTransform(transformReader);
                            xform.Transform(new StringReader(xmlOutput), Console.Out);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Error: {0}", ex.Message);
                            return(TRANSFORM_ERROR);
                        }
                    }

                    // Write xml output here
                    string xmlResultFile = options.xml == null || options.xml == string.Empty
                                                ? "TestResult.xml" : options.xml;

                    using (StreamWriter writer = new StreamWriter(xmlResultFile))
                    {
                        writer.Write(xmlOutput);
                    }
                }

                //if ( testRunner != null )
                //    testRunner.Unload();

                if (collector.HasExceptions)
                {
                    collector.WriteExceptions();
                    return(UNEXPECTED_ERROR);
                }

                if (!result.IsFailure)
                {
                    return(OK);
                }

                ResultSummarizer summ = new ResultSummarizer(result);
                return(summ.FailureCount);
            }
            finally
            {
                testRunner.Unload();
            }
        }
Exemplo n.º 2
0
        public int Execute(ConsoleOptions options)
        {
            XmlTextReader transformReader = GetTransformReader(options);

            if (transformReader == null)
            {
                return(3);
            }

            TextWriter outWriter = Console.Out;

            if (options.isOut)
            {
                StreamWriter outStreamWriter = new StreamWriter(options.output);
                outStreamWriter.AutoFlush = true;
                outWriter = outStreamWriter;
            }

            TextWriter errorWriter = Console.Error;

            if (options.isErr)
            {
                StreamWriter errorStreamWriter = new StreamWriter(options.err);
                errorStreamWriter.AutoFlush = true;
                errorWriter = errorStreamWriter;
            }

            TestRunner testRunner = MakeRunnerFromCommandLine(options);

            try
            {
                if (testRunner.Test == null)
                {
                    testRunner.Unload();
                    Console.Error.WriteLine("Unable to locate fixture {0}", options.fixture);
                    return(2);
                }

                EventCollector collector = new EventCollector(options, outWriter, errorWriter);

                TestFilter catFilter = TestFilter.Empty;

                if (options.HasInclude)
                {
                    Console.WriteLine("Included categories: " + options.include);
                    catFilter = new CategoryFilter(options.IncludedCategories);
                }

                if (options.HasExclude)
                {
                    Console.WriteLine("Excluded categories: " + options.exclude);
                    TestFilter excludeFilter = new NotFilter(new CategoryFilter(options.ExcludedCategories));
                    if (catFilter.IsEmpty)
                    {
                        catFilter = excludeFilter;
                    }
                    else
                    {
                        catFilter = new AndFilter(catFilter, excludeFilter);
                    }
                }

                TestResult result         = null;
                string     savedDirectory = Environment.CurrentDirectory;
                TextWriter savedOut       = Console.Out;
                TextWriter savedError     = Console.Error;

                try
                {
                    result = testRunner.Run(collector, catFilter);
                }
                finally
                {
                    outWriter.Flush();
                    errorWriter.Flush();

                    if (options.isOut)
                    {
                        outWriter.Close();
                    }
                    if (options.isErr)
                    {
                        errorWriter.Close();
                    }

                    Environment.CurrentDirectory = savedDirectory;
                    Console.SetOut(savedOut);
                    Console.SetError(savedError);
                }

                Console.WriteLine();

                string xmlOutput = CreateXmlOutput(result);

                if (options.xmlConsole)
                {
                    Console.WriteLine(xmlOutput);
                }
                else
                {
                    try
                    {
                        //CreateSummaryDocument(xmlOutput, transformReader );
                        XmlResultTransform xform = new XmlResultTransform(transformReader);
                        xform.Transform(new StringReader(xmlOutput), Console.Out);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Error: {0}", ex.Message);
                        return(3);
                    }
                }

                // Write xml output here
                string xmlResultFile = options.IsXml ? options.xml : "TestResult.xml";

                using (StreamWriter writer = new StreamWriter(xmlResultFile))
                {
                    writer.Write(xmlOutput);
                }

                //if ( testRunner != null )
                //    testRunner.Unload();

                if (collector.HasExceptions)
                {
                    collector.WriteExceptions();
                    return(2);
                }

                return(result.IsFailure ? 1 : 0);
            }
            finally
            {
                testRunner.Unload();
            }
        }