Exemple #1
0
        public int Execute(ConsoleOptions options)
        {
            var outWriter = System.Console.Out;
            var redirectOutput = options.output != null && options.output != string.Empty;
            if (redirectOutput)
            {
                var outStreamWriter = new StreamWriter(options.output);
                outStreamWriter.AutoFlush = true;
                outWriter = outStreamWriter;
            }

            var errorWriter = System.Console.Error;
            var redirectError = options.err != null && options.err != string.Empty;
            if (redirectError)
            {
                var errorStreamWriter = new StreamWriter(options.err);
                errorStreamWriter.AutoFlush = true;
                errorWriter = errorStreamWriter;
            }

            var package = MakeTestPackage(options);

            System.Console.WriteLine("ProcessModel: {0}    DomainUsage: {1}",
                package.Settings.Contains("ProcessModel")
                    ? package.Settings["ProcessModel"]
                    : "Default",
                package.Settings.Contains("DomainUsage")
                    ? package.Settings["DomainUsage"]
                    : "Default");

            System.Console.WriteLine("Execution Runtime: {0}",
                package.Settings.Contains("RuntimeFramework")
                    ? package.Settings["RuntimeFramework"]
                    : "Default");

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

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

            if (options.include != null && options.include != string.Empty)
            {
                System.Console.WriteLine("Included categories: " + options.include);
                var 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)
            {
                System.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);
            }

            if (testFilter is NotFilter)
                ((NotFilter)testFilter).TopLevel = true;
            TestResult result;

            var returnCode = DoRun(options, redirectOutput, redirectError, package, outWriter, errorWriter, testFilter, out result, collector);
            if (returnCode != 0)
                return returnCode;

            returnCode = UNEXPECTED_ERROR;

            if (result != null)
            {
                var xmlOutput = CreateXmlOutput(result);
                var summary = new ResultSummarizer(result);

                if (options.xmlConsole)
                {
                    System.Console.WriteLine(xmlOutput);
                }
                else
                {
                    WriteSummaryReport(summary);
                    if (summary.ErrorsAndFailures > 0)
                        WriteErrorsAndFailuresReport(result);
                    if (summary.TestsNotRun > 0)
                        WriteNotRunReport(result);
                }

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

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

                returnCode = summary.ErrorsAndFailures;
            }

            if (collector.HasExceptions)
            {
                collector.WriteExceptions();
                returnCode = UNEXPECTED_ERROR;
            }

            return returnCode;
        }
Exemple #2
0
        protected override int DoRun(Console.ConsoleOptions options, bool redirectOutput, bool redirectError, TestPackage package, TextWriter outWriter, TextWriter errorWriter, TestFilter testFilter, out TestResult result, EventCollector collector)
        {
            result = null;
            var testRunner1 = new DefaultTestRunnerFactory().MakeTestRunner(package);
            try
            {
                testRunner1.Load(package);

                if (testRunner1.Test == null)
                {
                    testRunner1.Unload();
                    System.Console.Error.WriteLine("Unable to locate fixture {0}", options.fixture);
                    return FIXTURE_NOT_FOUND;
                }
            }
            finally
            {
                var disp = testRunner1 as IDisposable;
                if (disp != null)
                    disp.Dispose();
            }

            result = new TestResult(new TestName { Name = "Global" });
            var timer = new Stopwatch();
            timer.Start();

            var testRunnerId = 0;
            {
                var syncTestRunner = new DefaultTestRunnerFactory().MakeTestRunner(package);
                var syncFilter = new AndFilter(testFilter, new SynchronousFilter());
                var logger = new ConsoleLoggingEventListener(collector);
                result.AddResult(RunPartition(redirectOutput, redirectError, package, outWriter, errorWriter, syncFilter, syncTestRunner, logger));
                testRunnerId = syncTestRunner.ID;
            }

            var dep = 0;
            var consoleOptions = options as ConsoleOptions;
            if (options != null)
                dep = consoleOptions.degreeofparallelism;
            if (dep == 0)
                dep = 4;
            System.Console.WriteLine("Degree of Parallelism: {0}", dep);

            var state = new AsynchronousFilterState(dep);
            var asyncTestRunner = new ParallelTestRunner(testRunnerId + 1, dep, state);
            result.AddResult(RunPartition(redirectOutput, redirectError, package, outWriter, errorWriter, testFilter, asyncTestRunner, collector));

            if (consoleOptions != null && consoleOptions.retestfailures)
            {
                var failedTests = (from test in Flatten(result)
                                   where test.Result.IsFailure
                                   select test).ToList();
                var failedTestFilter = new SimpleNameFilter(failedTests.Select(t => t.Result.FullName));

                var retestTestRunner = new DefaultTestRunnerFactory().MakeTestRunner(package);
                var retestFilter = new AndFilter(testFilter, failedTestFilter);
                var logger = new ConsoleLoggingEventListener(collector);
                var retestResults = RunPartition(redirectOutput, redirectError, package, outWriter, errorWriter, retestFilter, retestTestRunner, logger);

                var newTests = Flatten(retestResults).ToDictionary(test => test.Result.FullName);

                foreach (var failedTest in failedTests)
                {
                    var newTest = newTests[failedTest.Result.FullName];
                    if (newTest.Result.IsSuccess)
                    {
                        failedTest.Parent.Results.Remove(failedTest.Result);
                        failedTest.Parent.Results.Add(newTest.Result);
                    }
                }
            }

            result = MergeResults(result.Test, (TestResult)result.Results[0], (TestResult)result.Results[1]);

            timer.Stop();
            result.Time = timer.ElapsedTicks / (double)TimeSpan.TicksPerSecond;
            return 0;
        }
Exemple #3
0
 protected abstract int DoRun(ConsoleOptions options, bool redirectOutput, bool redirectError, TestPackage package, TextWriter outWriter, TextWriter errorWriter, TestFilter testFilter, out TestResult result, EventCollector collector);