Beispiel #1
0
        void RunTests(IRunContext runContext, IFrameworkHandle frameworkHandle, LoggerHelper logger, Func <List <AssemblyRunInfo> > testCaseAccessor)
        {
            Guard.ArgumentNotNull("runContext", runContext);
            Guard.ArgumentNotNull("frameworkHandle", frameworkHandle);

            try
            {
                RemotingUtility.CleanUpRegisteredChannels();

                cancelled = false;

                var assemblies             = testCaseAccessor();
                var parallelizeAssemblies  = assemblies.All(runInfo => runInfo.Configuration.ParallelizeAssemblyOrDefault);
                var reporterMessageHandler = new DefaultRunnerReporterWithTypes().CreateMessageHandler(new VisualStudioRunnerLogger(logger));

                using (AssemblyHelper.SubscribeResolve())
                    if (parallelizeAssemblies)
                    {
                        assemblies
                        .Select(runInfo => RunTestsInAssemblyAsync(runContext, frameworkHandle, logger, reporterMessageHandler, runInfo))
                        .ToList()
                        .ForEach(@event => @event.WaitOne());
                    }
                    else
                    {
                        assemblies
                        .ForEach(runInfo => RunTestsInAssembly(runContext, frameworkHandle, logger, reporterMessageHandler, runInfo));
                    }
            }
            catch (Exception ex)
            {
                logger.LogError("Catastrophic failure: {0}", ex);
            }
        }
Beispiel #2
0
        void RunTests(IRunContext runContext, IFrameworkHandle frameworkHandle, LoggerHelper logger, Func <List <AssemblyRunInfo> > testCaseAccessor)
        {
            Guard.ArgumentNotNull("runContext", runContext);
            Guard.ArgumentNotNull("frameworkHandle", frameworkHandle);

            try
            {
                RemotingUtility.CleanUpRegisteredChannels();

#if !PLATFORM_DOTNET
                // Reads settings like disabling appdomains, parallel etc.
                // Do this first before invoking any thing else to ensure correct settings for the run
                RunSettingsHelper.ReadRunSettings(runContext?.RunSettings?.SettingsXml);
#endif

                cancelled = false;

                var assemblies             = testCaseAccessor();
                var parallelizeAssemblies  = !RunSettingsHelper.DisableParallelization && assemblies.All(runInfo => runInfo.Configuration.ParallelizeAssemblyOrDefault);
                var reporterMessageHandler = new DefaultRunnerReporterWithTypes().CreateMessageHandler(new VisualStudioRunnerLogger(logger));

                using (AssemblyHelper.SubscribeResolve())
                    if (parallelizeAssemblies)
                    {
                        assemblies
                        .Select(runInfo => RunTestsInAssemblyAsync(runContext, frameworkHandle, logger, reporterMessageHandler, runInfo))
                        .ToList()
                        .ForEach(@event => @event.WaitOne());
                    }
                    else
                    {
                        assemblies
                        .ForEach(runInfo => RunTestsInAssembly(runContext, frameworkHandle, logger, reporterMessageHandler, runInfo));
                    }
            }
            catch (Exception ex)
            {
                logger.LogError("Catastrophic failure: {0}", ex);
            }
        }
Beispiel #3
0
        void DiscoverTests <TVisitor>(IEnumerable <string> sources,
                                      LoggerHelper logger,
                                      Func <string, ITestFrameworkDiscoverer, ITestFrameworkDiscoveryOptions, TVisitor> visitorFactory,
                                      Action <string, ITestFrameworkDiscoverer, ITestFrameworkDiscoveryOptions, TVisitor> visitComplete = null)
            where TVisitor : IVsDiscoverySink, IDisposable
        {
            try
            {
                RemotingUtility.CleanUpRegisteredChannels();

                using (AssemblyHelper.SubscribeResolve())
                {
                    var reporterMessageHandler = new DefaultRunnerReporterWithTypes().CreateMessageHandler(new VisualStudioRunnerLogger(logger));

                    foreach (var assemblyFileNameCanBeWithoutAbsolutePath in sources)
                    {
                        var assemblyFileName = assemblyFileNameCanBeWithoutAbsolutePath;
#if !PLATFORM_DOTNET
                        assemblyFileName = Path.GetFullPath(assemblyFileNameCanBeWithoutAbsolutePath);
#endif
                        var assembly = new XunitProjectAssembly {
                            AssemblyFilename = assemblyFileName
                        };
                        var configuration = LoadConfiguration(assemblyFileName);
                        var fileName      = Path.GetFileNameWithoutExtension(assemblyFileName);
                        var shadowCopy    = configuration.ShadowCopyOrDefault;

                        try
                        {
                            if (cancelled)
                            {
                                break;
                            }

                            if (!IsXunitTestAssembly(assemblyFileName))
                            {
                                if (configuration.DiagnosticMessagesOrDefault)
                                {
                                    logger.Log("Skipping: {0} (no reference to xUnit.net)", fileName);
                                }
                            }
                            else
                            {
                                var diagnosticSink = new DiagnosticMessageSink(logger, fileName, configuration.DiagnosticMessagesOrDefault);

                                using (var framework = new XunitFrontController(AppDomainDefaultBehavior, assemblyFileName: assemblyFileName, configFileName: null, shadowCopy: shadowCopy, diagnosticMessageSink: MessageSinkAdapter.Wrap(diagnosticSink)))
                                {
                                    var targetFramework = framework.TargetFramework;
                                    if (targetFramework.StartsWith("MonoTouch", StringComparison.OrdinalIgnoreCase) ||
                                        targetFramework.StartsWith("MonoAndroid", StringComparison.OrdinalIgnoreCase) ||
                                        targetFramework.StartsWith("Xamarin.iOS", StringComparison.OrdinalIgnoreCase))
                                    {
                                        if (configuration.DiagnosticMessagesOrDefault)
                                        {
                                            logger.Log("Skipping: {0} (unsupported target framework '{1}')", fileName, targetFramework);
                                        }
                                    }
                                    else
                                    {
                                        var discoveryOptions = TestFrameworkOptions.ForDiscovery(configuration);

                                        using (var visitor = visitorFactory(assemblyFileName, framework, discoveryOptions))
                                        {
                                            reporterMessageHandler.OnMessage(new TestAssemblyDiscoveryStarting(assembly, framework.CanUseAppDomains && AppDomainDefaultBehavior != AppDomainSupport.Denied, shadowCopy, discoveryOptions));

                                            framework.Find(includeSourceInformation: true, discoveryMessageSink: visitor, discoveryOptions: discoveryOptions);
                                            var totalTests = visitor.Finish();

                                            visitComplete?.Invoke(assemblyFileName, framework, discoveryOptions, visitor);

                                            reporterMessageHandler.OnMessage(new TestAssemblyDiscoveryFinished(assembly, discoveryOptions, totalTests, totalTests));
                                        }
                                    }
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            var ex           = e.Unwrap();
                            var fileNotFound = ex as FileNotFoundException;
#if !PLATFORM_DOTNET
                            var fileLoad = ex as FileLoadException;
#endif
                            if (ex is InvalidOperationException)
                            {
                                logger.LogWarning("Skipping: {0} ({1})", fileName, ex.Message);
                            }
                            else if (fileNotFound != null)
                            {
                                logger.LogWarning("Skipping: {0} (could not find dependent assembly '{1}')", fileName, Path.GetFileNameWithoutExtension(fileNotFound.FileName));
                            }
#if !PLATFORM_DOTNET
                            else if (fileLoad != null)
                            {
                                logger.LogWarning("Skipping: {0} (could not find dependent assembly '{1}')", fileName, Path.GetFileNameWithoutExtension(fileLoad.FileName));
                            }
#endif
                            else
                            {
                                logger.LogWarning("Exception discovering tests from {0}: {1}", fileName, ex);
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                logger.LogWarning("Exception discovering tests: {0}", e.Unwrap());
            }
        }
Beispiel #4
0
        public override bool Execute()
        {
            RemotingUtility.CleanUpRegisteredChannels();

            if (TeamCity)
            {
                Log.LogError("The 'TeamCity' property is deprecated. Please set the 'Reporter' property to 'teamcity' instead.");
                return(false);
            }
            if (Verbose)
            {
                Log.LogError("The 'Verbose' property is deprecated. Please set the 'Reporter' property to 'verbose' instead.");
                return(false);
            }

            XElement assembliesElement = null;
            var      environment       = $"{IntPtr.Size * 8}-bit .NET {Environment.Version}";

            if (NeedsXml)
            {
                assembliesElement = new XElement("assemblies");
            }

            switch (MaxParallelThreads)
            {
            case null:
            case "default":
                break;

            case "unlimited":
                maxThreadCount = -1;
                break;

            default:
                int threadValue;
                if (!int.TryParse(MaxParallelThreads, out threadValue) || threadValue < 1)
                {
                    Log.LogError("MaxParallelThreads value '{0}' is invalid: must be 'default', 'unlimited', or a positive number", MaxParallelThreads);
                    return(false);
                }

                maxThreadCount = threadValue;
                break;
            }

            var originalWorkingFolder = Directory.GetCurrentDirectory();

            using (AssemblyHelper.SubscribeResolve())
            {
                var reporters = GetAvailableRunnerReporters();
                var reporter  = reporters.FirstOrDefault(r => r.IsEnvironmentallyEnabled);

                if (reporter == null && !string.IsNullOrWhiteSpace(Reporter))
                {
                    reporter = reporters.FirstOrDefault(r => string.Equals(r.RunnerSwitch, Reporter, StringComparison.OrdinalIgnoreCase));
                    if (reporter == null)
                    {
                        var switchableReporters = reporters.Where(r => !string.IsNullOrWhiteSpace(r.RunnerSwitch)).Select(r => r.RunnerSwitch.ToLowerInvariant()).OrderBy(x => x).ToList();
                        if (switchableReporters.Count == 0)
                        {
                            Log.LogError("Reporter value '{0}' is invalid. There are no available reporters.", Reporter);
                        }
                        else
                        {
                            Log.LogError("Reporter value '{0}' is invalid. Available reporters: {1}", Reporter, string.Join(", ", switchableReporters));
                        }

                        return(false);
                    }
                }

                if (reporter == null)
                {
                    reporter = new DefaultRunnerReporterWithTypes();
                }

                logger = new MSBuildLogger(Log);
                reporterMessageHandler = MessageSinkWithTypesAdapter.Wrap(reporter.CreateMessageHandler(logger));

                if (!NoLogo)
                {
                    Log.LogMessage(MessageImportance.High, "xUnit.net MSBuild Runner ({0})", environment);
                }

                var project = new XunitProject();
                foreach (var assembly in Assemblies)
                {
                    var assemblyFileName = assembly.GetMetadata("FullPath");
                    var configFileName   = assembly.GetMetadata("ConfigFile");
                    if (configFileName != null && configFileName.Length == 0)
                    {
                        configFileName = null;
                    }

                    var projectAssembly = new XunitProjectAssembly {
                        AssemblyFilename = assemblyFileName, ConfigFilename = configFileName
                    };
                    if (shadowCopy.HasValue)
                    {
                        projectAssembly.Configuration.ShadowCopy = shadowCopy;
                    }

                    project.Add(projectAssembly);
                }

                if (WorkingFolder != null)
                {
                    Directory.SetCurrentDirectory(WorkingFolder);
                }

                var clockTime = Stopwatch.StartNew();

                if (!parallelizeAssemblies.HasValue)
                {
                    parallelizeAssemblies = project.All(assembly => assembly.Configuration.ParallelizeAssemblyOrDefault);
                }

                if (parallelizeAssemblies.GetValueOrDefault())
                {
                    var tasks   = project.Assemblies.Select(assembly => Task.Run(() => ExecuteAssembly(assembly)));
                    var results = Task.WhenAll(tasks).GetAwaiter().GetResult();
                    foreach (var assemblyElement in results.Where(result => result != null))
                    {
                        assembliesElement.Add(assemblyElement);
                    }
                }
                else
                {
                    foreach (var assembly in project.Assemblies)
                    {
                        var assemblyElement = ExecuteAssembly(assembly);
                        if (assemblyElement != null)
                        {
                            assembliesElement.Add(assemblyElement);
                        }
                    }
                }

                clockTime.Stop();

                if (completionMessages.Count > 0)
                {
                    reporterMessageHandler.OnMessage(new TestExecutionSummary(clockTime.Elapsed, completionMessages.OrderBy(kvp => kvp.Key).ToList()));
                }
            }

            Directory.SetCurrentDirectory(WorkingFolder ?? originalWorkingFolder);

            if (NeedsXml)
            {
                if (Xml != null)
                {
                    assembliesElement.Save(Xml.GetMetadata("FullPath"));
                }

                if (XmlV1 != null)
                {
                    Transform("xUnit1.xslt", assembliesElement, XmlV1);
                }

                if (Html != null)
                {
                    Transform("HTML.xslt", assembliesElement, Html);
                }

                if (NUnit != null)
                {
                    Transform("NUnitXml.xslt", assembliesElement, NUnit);
                }
            }

            // ExitCode is set to 1 for test failures and -1 for Exceptions.
            return(ExitCode == 0 || (ExitCode == 1 && IgnoreFailures));
        }
Beispiel #5
0
        /// <inheritdoc />
        public Result Execute([NotNull] ExternalCommandData commandData, [CanBeNull] ref string message, [CanBeNull][ItemNotNull] ElementSet elements)
        {
            try
            {
                CommandData = commandData ?? throw new ArgumentNullException(nameof(commandData));

                var testRunnerControlViewModel = new TestRunnerControlViewModel(DispatcherScheduler.Current);

                var mainWindowHandlePtr = Process.GetCurrentProcess().MainWindowHandle;

                var testRunnerWindow = new TestRunnerWindow
                {
                    DataContext           = testRunnerControlViewModel,
                    WindowStartupLocation = WindowStartupLocation.CenterOwner
                };

                // ReSharper disable once UnusedVariable
                var wih = new WindowInteropHelper(testRunnerWindow)
                {
                    Owner = mainWindowHandlePtr
                };

                if (testRunnerWindow.ShowDialog() != true)
                {
                    return(Result.Cancelled);
                }

                var selectedAssemblyList = testRunnerControlViewModel.SelectedAssemblies.ToList();

                if (!selectedAssemblyList.Any())
                {
                    TaskDialog.Show("Error", "No assemblies selected");
                    return(Result.Failed);
                }

                var nonExistentAssemblies = selectedAssemblyList.Where(a => !File.Exists(a)).ToList();

                if (nonExistentAssemblies.Any())
                {
                    TaskDialog.Show("Error", $"One or more assemblies does not exist: {nonExistentAssemblies.JoinList(", ")}");
                    return(Result.Failed);
                }

                List <string> assemblyList = BuildAssemblyList(testRunnerControlViewModel);

                var stopWatch = Stopwatch.StartNew();

                var logger     = new RvtRunnerLogger(stopWatch);
                var testRunner = new TestRunner(logger);

                var reporter = new DefaultRunnerReporterWithTypes();

                var options = new TestRunOptions(assemblyList, reporter);

                SetEnableCommandDataOption(options, testRunnerControlViewModel.AllowCommandDataAccess);

                var result = testRunner.Run(options);

                stopWatch.Stop();

                var allMessages = logger.AllMessages.JoinList();

                new TaskDialog("Result")
                {
                    MainInstruction = $"Failing tests: {result}. Total time elapsed: {stopWatch.Elapsed}",
                    ExpandedContent = allMessages
                }.Show();

                Debug.WriteLine(result);
            }
            catch (Exception ex)
            {
                TaskDialog.Show("Error", $"{ex.GetType()} - {ex.Message}");
                return(Result.Failed);
            }
            finally
            {
                CommandData = null;
            }

            return(Result.Succeeded);
        }