Beispiel #1
0
        internal void InternalExecute()
        {
            // We don't catch exceptions here because NAnt takes care of that job,
            // and decides whether to let them through based on the value of the
            // FailOnError
            var logger = CreateLogger();

            DisplayVersion();
            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.EchoResults       = EchoResults;
            launcher.RunTimeLimit      = runTimeLimit;

            if (RunnerType != null)
            {
                launcher.TestProject.TestRunnerFactoryName = RunnerType;
            }

            if (RunnerExtensions != null)
            {
                foreach (Argument arg in RunnerExtensions)
                {
                    launcher.TestProject.AddTestRunnerExtensionSpecification(arg.Value);
                }
            }

            launcher.RuntimeSetup = new RuntimeSetup();

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

            if (ApplicationBaseDirectory != null)
            {
                launcher.TestProject.TestPackage.ApplicationBaseDirectory = new DirectoryInfo(ApplicationBaseDirectory);
            }
            if (WorkingDirectory != null)
            {
                launcher.TestProject.TestPackage.WorkingDirectory = new DirectoryInfo(WorkingDirectory);
            }
            if (ShadowCopy.HasValue)
            {
                launcher.TestProject.TestPackage.ShadowCopy = ShadowCopy.Value;
            }
            if (Debug.HasValue && Debug.Value)
            {
                launcher.TestProject.TestPackage.DebuggerSetup = new DebuggerSetup();
            }
            if (RuntimeVersion != null)
            {
                launcher.TestProject.TestPackage.RuntimeVersion = RuntimeVersion;
            }

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

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

            AddAssemblies(launcher);
            AddHintDirectories(launcher);
            AddPluginDirectories(launcher);

            if (ReportDirectory != null)
            {
                launcher.TestProject.ReportDirectory = ReportDirectory;
            }
            if (ReportNameFormat != null)
            {
                launcher.TestProject.ReportNameFormat = ReportNameFormat;
            }
            if (ReportArchive != null)
            {
                launcher.TestProject.ReportArchive = Runner.Reports.ReportArchive.Parse(ReportArchive);
            }

            if (ReportTypes != null)
            {
                string[] typeNames = ReportTypes.Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
                GenericCollectionUtils.ForEach(typeNames, launcher.AddReportFormat);
            }

            TestLauncherResult result = RunLauncher(launcher);

            SetResultProperty(result.ResultCode);
            PopulateStatistics(result);

            if (!FailOnError)
            {
                return;
            }

            if (result.ResultCode != ResultCode.Success && result.ResultCode != ResultCode.NoTests)
            {
                // The only way to make the task fail is to throw an exception
                throw new BuildException(Resources.TestExecutionFailed);
            }
        }
Beispiel #2
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);
            }
        }
        internal TestLauncherResult Execute()
        {
            var launcher = new TestLauncher
            {
                Logger = Logger,
                ProgressMonitorProvider = ProgressMonitorProvider,
                TestExecutionOptions    = { FilterSet = GetFilterSet() },
                ShowReports             = ShowReports.IsPresent,
                DoNotRun          = DoNotRun.IsPresent,
                IgnoreAnnotations = IgnoreAnnotations.IsPresent,
                EchoResults       = !NoEchoResults.IsPresent,
                RunTimeLimit      = runTimeLimit
            };

            if (RunnerType != null)
            {
                launcher.TestProject.TestRunnerFactoryName = RunnerType;
            }
            if (RunnerExtensions != null)
            {
                GenericCollectionUtils.ForEach(RunnerExtensions, x => launcher.TestProject.AddTestRunnerExtensionSpecification(x));
            }

            launcher.RuntimeSetup = new RuntimeSetup();

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

            if (ApplicationBaseDirectory != null)
            {
                launcher.TestProject.TestPackage.ApplicationBaseDirectory = new DirectoryInfo(ApplicationBaseDirectory);
            }
            if (WorkingDirectory != null)
            {
                launcher.TestProject.TestPackage.WorkingDirectory = new DirectoryInfo(WorkingDirectory);
            }
            if (ShadowCopy.HasValue)
            {
                launcher.TestProject.TestPackage.ShadowCopy = ShadowCopy.Value.IsPresent;
            }
            if (DebugTests.HasValue && DebugTests.Value.IsPresent)
            {
                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);
            }

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

            if (ReportDirectory != null)
            {
                launcher.TestProject.ReportDirectory = ReportDirectory;
            }
            if (ReportNameFormat != null)
            {
                launcher.TestProject.ReportNameFormat = ReportNameFormat;
            }
            if (ReportTypes != null)
            {
                GenericCollectionUtils.ForEach(ReportTypes, launcher.AddReportFormat);
            }
            launcher.TestProject.ReportArchive = Runner.Reports.ReportArchive.Parse(ReportArchive);
            TestLauncherResult result = RunLauncher(launcher);

            return(result);
        }