Ejemplo n.º 1
0
        internal bool InternalExecute()
        {
            DisplayVersion();
            var logger = new FilteredLogger(new TaskLogger(Log), verbosity);
            var launcher = new TestLauncher();
            launcher.Logger = logger;
            launcher.ProgressMonitorProvider = new LogProgressMonitorProvider(logger);
            launcher.TestExecutionOptions.FilterSet = GetFilterSet();
            launcher.ShowReports = ShowReports;
            launcher.DoNotRun = DoNotRun;
            launcher.IgnoreAnnotations = IgnoreAnnotations;
            launcher.RunTimeLimit = runTimeLimit;
            launcher.RuntimeSetup = new RuntimeSetup();

            // Set the installation path explicitly to the path of the MSBuild task assembly
            // since otherwise we will look at the path of MSBuild.exe.
            launcher.RuntimeSetup.RuntimePath = Path.GetDirectoryName(AssemblyUtils.GetFriendlyAssemblyLocation(typeof(Gallio).Assembly));

            if (EchoResults)
                launcher.TestProject.AddTestRunnerExtension(new TaskLogExtension(Log));
            if (ApplicationBaseDirectory != null)
                launcher.TestProject.TestPackage.ApplicationBaseDirectory = new DirectoryInfo(ApplicationBaseDirectory.ItemSpec);
            if (WorkingDirectory != null)
                launcher.TestProject.TestPackage.WorkingDirectory = new DirectoryInfo(WorkingDirectory.ItemSpec);
            launcher.TestProject.TestPackage.ShadowCopy = ShadowCopy;
            if (Debug)
                launcher.TestProject.TestPackage.DebuggerSetup = new DebuggerSetup();
            if (RuntimeVersion != null)
                launcher.TestProject.TestPackage.RuntimeVersion = RuntimeVersion;

            foreach (string option in ReportFormatterProperties)
            {
                KeyValuePair<string, string> pair = StringUtils.ParseKeyValuePair(option);
                launcher.ReportFormatterOptions.AddProperty(pair.Key, pair.Value);
            }

            foreach (string option in RunnerProperties)
            {
                KeyValuePair<string, string> pair = StringUtils.ParseKeyValuePair(option);
                launcher.TestRunnerOptions.AddProperty(pair.Key, pair.Value);
            }

            ForEachItemSpec(Files, launcher.AddFilePattern);
            ForEachItemSpec(HintDirectories, x => launcher.TestProject.TestPackage.AddHintDirectory(new DirectoryInfo(x)));
            ForEachItemSpec(PluginDirectories, x => launcher.RuntimeSetup.AddPluginDirectory(x));

            if (ReportDirectory != null)
                launcher.TestProject.ReportDirectory = ReportDirectory.ItemSpec;
            if (ReportNameFormat != null)
                launcher.TestProject.ReportNameFormat = ReportNameFormat;
            if (ReportTypes != null)
                GenericCollectionUtils.ForEach(ReportTypes, launcher.AddReportFormat);
            if (ReportArchive != null)
                launcher.TestProject.ReportArchive = Runner.Reports.ReportArchive.Parse(ReportArchive);
            if (RunnerType != null)
                launcher.TestProject.TestRunnerFactoryName = RunnerType;
            if (RunnerExtensions != null)
                GenericCollectionUtils.ForEach(RunnerExtensions, x => launcher.TestProject.AddTestRunnerExtensionSpecification(x));

            TestLauncherResult result = RunLauncher(launcher);
            ExitCode = result.ResultCode;
            LogResultSummary(logger, result);
            PopulateStatistics(result);
            return ExitCode == ResultCode.Success 
                || ExitCode == ResultCode.NoTests 
                || IgnoreFailures;
        }
        private FacadeTestRunState Run(IFacadeTestListener testListener, string assemblyPath, Filter<ITestDescriptor> filter, FacadeOptions facadeOptions)
        {
            if (testListener == null)
                throw new ArgumentNullException(@"testListener");
            if (assemblyPath == null)
                throw new ArgumentNullException("assemblyPath");
            if (facadeOptions == null)
                throw new ArgumentNullException("facadeOptions");

            ILogger logger = new FilteredLogger(new TDNetLogger(testListener), LogSeverity.Info);

            try
            {
                RuntimeAccessor.Instance.AddLogListener(logger);
                var filterRules = new List<FilterRule<ITestDescriptor>>();

                switch (facadeOptions.FilterCategoryMode)
                {
                    case FacadeFilterCategoryMode.Disabled:
                        filterRules.Add(new FilterRule<ITestDescriptor>(FilterRuleType.Inclusion, filter));
                        break;

                    case FacadeFilterCategoryMode.Include:
                        filterRules.Add(new FilterRule<ITestDescriptor>(FilterRuleType.Inclusion,
                            new AndFilter<ITestDescriptor>(new[] { filter, ToCategoryFilter(facadeOptions.FilterCategoryNames) })));
                        break;

                    case FacadeFilterCategoryMode.Exclude:
                        filterRules.Add(new FilterRule<ITestDescriptor>(FilterRuleType.Exclusion, ToCategoryFilter(facadeOptions.FilterCategoryNames)));
                        filterRules.Add(new FilterRule<ITestDescriptor>(FilterRuleType.Inclusion, filter));
                        break;
                }

                var filterSet = new FilterSet<ITestDescriptor>(filterRules);
                launcher.Logger = logger;
                launcher.ProgressMonitorProvider = new LogProgressMonitorProvider(logger);
                launcher.TestExecutionOptions.FilterSet = filterSet;
                launcher.TestProject.TestRunnerFactoryName = StandardTestRunnerFactoryNames.IsolatedAppDomain;
                launcher.TestProject.AddTestRunnerExtension(new TDNetExtension(testListener)); // This monitor will inform the user in real-time what's going on
                launcher.TestProject.TestPackage.AddFile(new FileInfo(assemblyPath));
                string assemblyDirectory = Path.GetDirectoryName(assemblyPath);
                launcher.TestProject.TestPackage.ApplicationBaseDirectory = new DirectoryInfo(assemblyDirectory);
                launcher.TestProject.TestPackage.WorkingDirectory = new DirectoryInfo(assemblyDirectory);
                TestLauncherResult result = RunLauncher(launcher);
                string reportDirectory = GetReportDirectory(logger);

                if (reportDirectory != null)
                {
                    var reportFormatterOptions = new ReportFormatterOptions();
                    var preferenceManager = (TDNetPreferenceManager)RuntimeAccessor.ServiceLocator.ResolveByComponentId("TDNetRunner.PreferenceManager");
                    var reportFormat = preferenceManager.ReportSettings.DetermineReportFormat(result.Report);
                    result.GenerateReports(reportDirectory, Path.GetFileName(assemblyPath), ReportArchive.Normal,
                        new[] { reportFormat }, reportFormatterOptions,
                        RuntimeAccessor.ServiceLocator.Resolve<IReportManager>(), NullProgressMonitor.CreateInstance());

                    // This will generate a link to the generated report
                    if (result.ReportDocumentPaths.Count != 0)
                    {
                        Uri rawUrl = new Uri(result.ReportDocumentPaths[0]);
                        string displayUrl = "file:///" + rawUrl.LocalPath.Replace(" ", "%20").Replace(@"\", "/");

                        // TDNet just prints the link on its own but it's not always clear to users what it represents.
                        // testListener.TestResultsUrl(displayUrl);
                        testListener.WriteLine("\nTest Report: " + displayUrl, FacadeCategory.Info);
                    }
                }

                // Inform no tests run, if necessary.
                if (result.ResultCode == ResultCode.NoTests)
                {
                    InformNoTestsWereRun(testListener, Resources.MbUnitTestRunner_NoTestsFound);
                }
                else if (result.Statistics.TestCount == 0)
                {
                    InformNoTestsWereRun(testListener, null);
                }

                return GetTestRunState(result);
            }
            finally
            {
                RuntimeAccessor.Instance.RemoveLogListener(logger);
            }
        }