Пример #1
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();
            }
        }
Пример #2
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.");
        }