Exemple #1
0
        private static ITestFrameworkExecutionOptions ConfiguExecutionOptions([NotNull] XunitProjectAssembly assembly, [NotNull] TestRunOptions options)
        {
            if (assembly == null)
            {
                throw new ArgumentNullException(nameof(assembly));
            }

            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            bool stopOnFail = options.StopOnFail;

            ITestFrameworkExecutionOptions executionOptions = TestFrameworkOptions.ForExecution(assembly.Configuration);

            executionOptions.SetStopOnTestFail(stopOnFail);

            int?maxThreadCount = options.MaxParallelThreads;

            if (maxThreadCount.HasValue)
            {
                executionOptions.SetMaxParallelThreads(maxThreadCount);
            }

            bool?parallelizeTestCollections = options.ParallelizeTestCollections;

            if (parallelizeTestCollections.HasValue)
            {
                executionOptions.SetDisableParallelization(!parallelizeTestCollections.GetValueOrDefault());
            }

            return(executionOptions);
        }
Exemple #2
0
    private async Task <XElement> Run(Assembly assembly, string assemblyPath)
    {
        using (var frontController = new XunitFrontController(AppDomainSupport, assemblyPath, null, false))
        {
            using (var discoverySink = new TestDiscoverySink())
            {
                var configuration = GetConfiguration(assembly) ?? new TestAssemblyConfiguration()
                {
                    PreEnumerateTheories = false
                };
                ITestFrameworkDiscoveryOptions discoveryOptions = GetFrameworkOptionsForDiscovery(configuration);
                discoveryOptions.SetSynchronousMessageReporting(true);
                Logger.OnDebug($"Starting test discovery in the '{assembly}' assembly");
                frontController.Find(false, discoverySink, discoveryOptions);
                Logger.OnDebug($"Test discovery in assembly '{assembly}' completed");
                discoverySink.Finished.WaitOne();

                if (discoverySink.TestCases == null || discoverySink.TestCases.Count == 0)
                {
                    Logger.Info("No test cases discovered");
                    return(null);
                }

                TotalTests += discoverySink.TestCases.Count;
                List <ITestCase> testCases;
                if (_filters != null && _filters.TestCaseFilters.Any())
                {
                    Action <string> log = LogExcludedTests ? (s) => do_log(s) : (Action <string>)null;
                    testCases = discoverySink.TestCases.Where(
                        tc => !_filters.IsExcluded(tc, log)).ToList();
                    FilteredTests += discoverySink.TestCases.Count - testCases.Count;
                }
                else
                {
                    testCases = discoverySink.TestCases;
                }

                var            assemblyElement = new XElement("assembly");
                IExecutionSink resultsSink     = new DelegatingExecutionSummarySink(_messageSink, null, null);
                resultsSink = new DelegatingXmlCreationSink(resultsSink, assemblyElement);
                ITestFrameworkExecutionOptions executionOptions = GetFrameworkOptionsForExecution(configuration);
                executionOptions.SetDisableParallelization(!RunInParallel);
                executionOptions.SetSynchronousMessageReporting(true);
                executionOptions.SetMaxParallelThreads(MaxParallelThreads);

                // set the wait for event cb first, then execute the tests
                var resultTask = WaitForEvent(resultsSink.Finished, TimeSpan.FromDays(10)).ConfigureAwait(false);
                frontController.RunTests(testCases, resultsSink, executionOptions);
                await resultTask;

                return(assemblyElement);
            }
        }
    }
Exemple #3
0
        XElement Run(Assembly assembly, string assemblyPath)
        {
            using (var frontController = new XunitFrontController(AppDomainSupport, assemblyPath, null, false))
            {
                using (var discoverySink = new TestDiscoverySink())
                {
                    var configuration = GetConfiguration(assembly) ?? new TestAssemblyConfiguration();
                    ITestFrameworkDiscoveryOptions discoveryOptions = GetFrameworkOptionsForDiscovery(configuration);
                    discoveryOptions.SetSynchronousMessageReporting(true);
                    Logger.OnInfo(LogTag, $"Starting test discovery in the '{assembly}' assembly");
                    frontController.Find(false, discoverySink, discoveryOptions);
                    Logger.OnInfo(LogTag, $"Test discovery in assembly '{assembly}' completed");
                    discoverySink.Finished.WaitOne();

                    if (discoverySink.TestCases == null || discoverySink.TestCases.Count == 0)
                    {
                        Logger.OnInfo(LogTag, "No test cases discovered");
                        return(null);
                    }

                    TotalTests += discoverySink.TestCases.Count;
                    List <ITestCase> testCases;
                    if (filters != null && filters.Count > 0)
                    {
                        testCases      = discoverySink.TestCases.Where(tc => IsIncluded(tc)).ToList();
                        FilteredTests += discoverySink.TestCases.Count - testCases.Count;
                    }
                    else
                    {
                        testCases = discoverySink.TestCases;
                    }

                    var            assemblyElement = new XElement("assembly");
                    IExecutionSink resultsSink     = new DelegatingExecutionSummarySink(messageSink, null, null);
                    resultsSink = new DelegatingXmlCreationSink(resultsSink, assemblyElement);
                    ITestFrameworkExecutionOptions executionOptions = GetFrameworkOptionsForExecution(configuration);
                    executionOptions.SetDisableParallelization(!RunInParallel);
                    executionOptions.SetSynchronousMessageReporting(true);

                    try
                    {
                        frontController.RunTests(testCases, resultsSink, executionOptions);
                        resultsSink.Finished.WaitOne();
                    }
                    finally
                    {
                        resultsSink.Dispose();
                    }

                    return(assemblyElement);
                }
            }
        }
Exemple #4
0
        /// <summary>
        ///     <see cref="StarcounterXunitRunner.Start(string, string)"/> for description.
        /// </summary>
        /// <param name="typeName"></param>
        /// <param name="testReportName"></param>
        private void ExecuteTests(string typeName = null, string testReportName = null)
        {
            ExecutionSummary executionSummary = null;

            XunitProjectAssembly assembly = new XunitProjectAssembly
            {
                AssemblyFilename = assemblyLocation,
                ConfigFilename   = null
            };

            XElement assembliesElement = new XElement("assemblies");
            XElement assemblyElement   = new XElement("assembly");

            // Logger
            var                   verboserReporter       = new XunitReporters.VerboseReporter();
            IRunnerLogger         logger                 = new ConsoleRunnerLogger(useColors: true);
            IMessageSinkWithTypes reporterMessageHandler = MessageSinkWithTypesAdapter.Wrap(verboserReporter.CreateMessageHandler(logger));

            // Option setup
            ITestFrameworkDiscoveryOptions discoveryOptions = TestFrameworkOptions.ForDiscovery(null);
            ITestFrameworkExecutionOptions executionOptions = TestFrameworkOptions.ForExecution(null);

            executionOptions.SetSynchronousMessageReporting(true);
            executionOptions.SetDisableParallelization(DeveloperMode || !RunTestsInParallel);
            executionOptions.SetDiagnosticMessages(true);

            var assemblyDisplayName = Path.GetFileNameWithoutExtension(assembly.AssemblyFilename);
            var appDomainSupport    = assembly.Configuration.AppDomainOrDefault;
            var shadowCopy          = assembly.Configuration.ShadowCopyOrDefault;

            var  clockTime = Stopwatch.StartNew();
            bool cancel    = false;

            using (var controller = new XunitFrontController(
                       appDomainSupport: AppDomainSupport.Denied,
                       assemblyFileName: assembly.AssemblyFilename,
                       configFileName: null,
                       shadowCopy: shadowCopy,
                       shadowCopyFolder: null,
                       sourceInformationProvider: null,
                       diagnosticMessageSink: null))
                using (var discoverySink = new TestDiscoverySink(() => cancel))
                {
                    // Discover & filter the tests
                    reporterMessageHandler.OnMessage(new TestAssemblyDiscoveryStarting(
                                                         assembly: assembly,
                                                         appDomain: controller.CanUseAppDomains && appDomainSupport != AppDomainSupport.Denied,
                                                         shadowCopy: shadowCopy,
                                                         discoveryOptions: discoveryOptions));

                    if (typeName != null)
                    {
                        controller.Find(typeName, false, discoverySink, discoveryOptions);
                    }
                    else
                    {
                        controller.Find(false, discoverySink, discoveryOptions);
                    }
                    discoverySink.Finished.WaitOne();

                    var testCasesDiscovered = discoverySink.TestCases.Count;
                    var filteredTestCases   = discoverySink.TestCases.Where(TestCaseFilter).ToList();
                    var testCasesToRun      = filteredTestCases.Count;

                    reporterMessageHandler.OnMessage(new TestAssemblyDiscoveryFinished(assembly, discoveryOptions, testCasesDiscovered, testCasesToRun));

                    // Run the filtered tests
                    if (testCasesToRun == 0)
                    {
                        executionSummary = new ExecutionSummary();
                    }
                    else
                    {
                        reporterMessageHandler.OnMessage(new TestAssemblyExecutionStarting(assembly, executionOptions));

                        IExecutionSink resultsSink = new DelegatingExecutionSummarySink(reporterMessageHandler, () => cancel, (path, summary) => { executionSummary = summary; });
                        if (assemblyElement != null)
                        {
                            resultsSink = new DelegatingXmlCreationSink(resultsSink, assemblyElement);
                        }

                        controller.RunTests(filteredTestCases, resultsSink, executionOptions);
                        resultsSink.Finished.WaitOne();

                        reporterMessageHandler.OnMessage(new TestAssemblyExecutionFinished(assembly, executionOptions, resultsSink.ExecutionSummary));
                        assembliesElement.Add(assemblyElement);
                    }
                }

            clockTime.Stop();

            assembliesElement.Add(new XAttribute("timestamp", DateTime.Now.ToString(CultureInfo.InvariantCulture)));

            if (executionSummary != null)
            {
                Console.WriteLine();

                KeyValuePair <string, ExecutionSummary> kvpExecutionSummary = new KeyValuePair <string, ExecutionSummary>(this.assebmlyName, executionSummary);

                reporterMessageHandler.OnMessage(new TestExecutionSummary(clockTime.Elapsed, new List <KeyValuePair <string, ExecutionSummary> > {
                    kvpExecutionSummary
                }));

                if (testReportName != null)
                {
                    // Create folder if it does not exist
                    FileInfo      fi        = new FileInfo(testReportName);
                    DirectoryInfo directory = fi.Directory;
                    if (!directory.Exists)
                    {
                        Directory.CreateDirectory(directory.FullName);
                    }

                    CreateXmlTestReport(assembliesElement, fi);
                    CreateHtmlTestReport(assembliesElement, fi);
                }
                Console.WriteLine();
                Console.WriteLine();
            }
        }
Exemple #5
0
        internal void DiscoverAndRun()
        {
            var assembly = typeof(R2APITest).Assembly;
            var path     = assembly.Location;

            R2APITest.Logger.LogInfo($"Discovering tests in {path}...");
            var assemblyElement = new XElement("assembly");

            try {
                var messageSink = new MessageSink {
                    OnTest = OnTest,
                    OnExecutionComplete = OnExecutionComplete,
                };

                using (
                    var controller = new XunitFrontController(
                        AppDomainSupport.Denied,
                        path
                        )
                    ) {
                    var configuration = ConfigReader.Load(path);
                    configuration.AppDomain              = AppDomainSupport.IfAvailable;
                    configuration.DiagnosticMessages     = true;
                    configuration.StopOnFail             = true;
                    configuration.MaxParallelThreads     = 1;
                    configuration.LongRunningTestSeconds = 5;
                    ITestFrameworkDiscoveryOptions discoveryOptions =
                        TestFrameworkOptions.ForDiscovery(configuration);
                    discoveryOptions.SetSynchronousMessageReporting(true);
                    discoveryOptions.SetPreEnumerateTheories(false);
                    controller.Find(false, messageSink, discoveryOptions);
                    messageSink.DiscoveryCompletionWaitHandle.WaitOne();
                    ITestCase[] testCases = messageSink.TestCases.ToArray();
                    lock (this) {
                        R2APITest.Logger.LogInfo(
                            $"{testCases.Length} test cases were found in {path}:"
                            );
                        foreach (ITestCase testCase in testCases)
                        {
                            R2APITest.Logger.LogInfo($"- {testCase.DisplayName}");
                        }

                        Console.Error.Flush();
                    }

                    ITestFrameworkExecutionOptions executionOptions =
                        TestFrameworkOptions.ForExecution(configuration);
                    executionOptions.SetDiagnosticMessages(true);
                    executionOptions.SetSynchronousMessageReporting(true);
                    executionOptions.SetStopOnTestFail(true);
                    executionOptions.SetDisableParallelization(true);

                    controller.RunTests(
                        testCases,
                        messageSink,
                        executionOptions
                        );
                    messageSink.ExecutionCompletionWaitHandle.WaitOne();
                }
            }
            catch (Exception e) {
                R2APITest.Logger.LogError($"{e.GetType().Name}: {e.Message}");
                R2APITest.Logger.LogError(e.StackTrace);
            }
            R2APITest.Logger.LogInfo($"All tests in {path} ran.");
        }