Example #1
0
        private void SendTestCompletedIfNecessary(bool designTime, bool list)
        {
            if (!designTime)
            {
                return;
            }

            if (list)
            {
                testDiscoverySink.SendTestCompleted();
            }
            else
            {
                testExecutionSink.SendTestCompleted();
            }
        }
        int Execute()
        {
            DisplayRuntimeEnvironment();

            DisplayTestFiles();

            IEnumerable <string>         testList = SetupSinks();
            IDictionary <string, object> settings = GetTestSettings();

            // We display the filters at this point so  that any exception message
            // thrown by CreateTestFilter will be understandable.
            DisplayTestFilters();

            // Apply filters and merge with testList
            var filter = CreateTestFilter(testList);

            var summary = new ResultSummary();

            // Load the test framework
            foreach (var assembly in _options.InputFiles)
            {
                // TODO: Load async
                var assemblyPath  = System.IO.Path.GetFullPath(assembly);
                var testAssembly  = LoadAssembly(assemblyPath);
                var frameworkPath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(assemblyPath), "nunit.framework.dll");
                var framework     = LoadAssembly(frameworkPath);

                var driver = new NUnitPortableDriver();
                var result = driver.Load(framework, testAssembly, settings);

                // TODO: Run async
                // Explore or Run
                if (_options.List || _options.Explore)
                {
                    ITestListener listener = new TestExploreListener(_testDiscoverySink, _options, assemblyPath);
                    string        xml      = driver.Explore(filter.Text);
                    listener.OnTestEvent(xml);
                    summary.AddResult(xml);
                }
                else
                {
                    var tcListener = new TeamCityEventListener();
                    TestExecutionListener listener = new TestExecutionListener(_testExecutionSink, _options, assemblyPath);
                    SetupLabelOutput(listener);
                    string xml = driver.Run(
                        report =>
                    {
                        listener.OnTestEvent(report);
                        if (_options.TeamCity)
                        {
                            tcListener.OnTestEvent(report);
                        }
                    },
                        filter.Text);
                    summary.AddResult(xml);
                }
            }

            if (_options.List || _options.Explore)
            {
                if (_options.DesignTime)
                {
                    _testDiscoverySink.SendTestCompleted();
                }

                return(ReturnCodes.OK);
            }

            if (_options.DesignTime)
            {
                _testExecutionSink.SendTestCompleted();
                return(ReturnCodes.OK);
            }

            // Summarize and save test results
            var reporter = new ResultReporter(summary, ColorConsole, _options);

            reporter.ReportResults();

            // Save out the TestResult.xml
            SaveTestResults(reporter.TestResults);

            if (summary.UnexpectedError)
            {
                return(ReturnCodes.UNEXPECTED_ERROR);
            }

            if (summary.InvalidAssemblies > 0)
            {
                return(ReturnCodes.INVALID_ASSEMBLY);
            }

            if (summary.InvalidTestFixtures > 0)
            {
                return(ReturnCodes.INVALID_TEST_FIXTURE);
            }

            // Return the number of test failures
            return(summary.FailedCount);
        }
Example #3
0
        int Execute()
        {
            var designTimeFullyQualifiedNames = SetupSinks(args);

            var results = new List <TestResultWrapper>();

            foreach (var assembly in args.Inputs)
            {
                var assemblyPath = Path.GetFullPath(assembly);
                var testAssembly = LoadAssembly(assemblyPath);
                var libraryPath  = Path.Combine(Path.GetDirectoryName(assemblyPath), "Persimmon.dll");
                var library      = LoadAssembly(libraryPath);

                var driver = new PersimmonDriver(library, testAssembly);
                var tests  = driver.CollectTests();

                if (args.DesignTime && designTimeFullyQualifiedNames.Any())
                {
                    tests = tests.Where(t => designTimeFullyQualifiedNames.Contains(t.Test.FullyQualifiedName));
                }

                if (args.List)
                {
                    foreach (var test in tests)
                    {
                        testDiscoverySink.SendTestFound(test.Test);
                    }
                }
                else
                {
                    Action <object> before = testCase =>
                    {
                        if (args.DesignTime)
                        {
                            testExecutionSink.SendTestStarted(new TestCaseWrapper(testCase).Test);
                        }
                    };
                    Action <object> progress = result =>
                    {
                        var wrapper = new TestResultWrapper(result);
                        // TODO: send async
                        //if (args.DesignTime)
                        //{
                        //    testExecutionSink.SendTestResult(wrapper.TestResult);
                        //}
                        //else
                        if (!args.DesignTime)
                        {
                            switch (wrapper.TestResult.Outcome)
                            {
                            case TestOutcome.Passed:
                                Console.Write(".");
                                break;

                            case TestOutcome.Failed:
                                if (wrapper.Exceptions.Any())
                                {
                                    Console.Write("E");
                                }
                                else
                                {
                                    Console.Write("x");
                                }
                                break;

                            case TestOutcome.Skipped:
                                Console.Write("_");
                                break;

                            default:
                                break;
                            }
                        }
                        results.Add(wrapper);
                    };
                    driver.RunTests(tests.Select(t => t.Test.FullyQualifiedName).ToArray(), before, progress);
                }
            }

            // TODO: remove after implement `send async`
            if (args.DesignTime)
            {
                foreach (var wrapper in results)
                {
                    testExecutionSink.SendTestResult(wrapper.TestResult);
                }
            }

            if (args.List)
            {
                if (args.DesignTime)
                {
                    testDiscoverySink.SendTestCompleted();
                }
                return(0);
            }

            if (args.DesignTime)
            {
                testExecutionSink.SendTestCompleted();
                return(0);
            }

            var reporter = new Reporter(results, ColorConsole);

            reporter.Report();

            return(results.Where(r => r.TestResult.Outcome == TestOutcome.Failed).Count());
        }