Ejemplo n.º 1
0
        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);
        }