Esempio n. 1
0
    public Task <List <_MessageSinkMessage> > RunAsync(Type[] types)
    {
        var tcs = new TaskCompletionSource <List <_MessageSinkMessage> >();

        ThreadPool.QueueUserWorkItem(async _ =>
        {
            try
            {
                var diagnosticMessageSink     = new _NullMessageSink();
                await using var testFramework = new XunitTestFramework(diagnosticMessageSink, configFileName: null);

                using var discoverySink = SpyMessageSink <_DiscoveryComplete> .Create();
                var assemblyInfo        = Reflector.Wrap(Assembly.GetEntryAssembly() !);
                var discoverer          = testFramework.GetDiscoverer(assemblyInfo);
                foreach (var type in types)
                {
                    discoverer.Find(type.FullName !, discoverySink, _TestFrameworkOptions.ForDiscovery());
                    discoverySink.Finished.WaitOne();
                    discoverySink.Finished.Reset();
                }

                var testCases = discoverySink.Messages.OfType <_TestCaseDiscovered>().Select(msg => msg.Serialization).ToArray();

                using var runSink = SpyMessageSink <_TestAssemblyFinished> .Create();
                var executor      = testFramework.GetExecutor(assemblyInfo);
                executor.RunTests(testCases, runSink, _TestFrameworkOptions.ForExecution());
                runSink.Finished.WaitOne();

                tcs.TrySetResult(runSink.Messages.ToList());
            }
            catch (Exception ex)
            {
                tcs.TrySetException(ex);
            }
        });

        return(tcs.Task);
    }
Esempio n. 2
0
    public Task <List <_MessageSinkMessage> > RunAsync(
        Type[] types,
        bool preEnumerateTheories                   = true,
        _IMessageSink?diagnosticMessageSink         = null,
        _IMessageSink?internalDiagnosticMessageSink = null,
        params _IReflectionAttributeInfo[] additionalAssemblyAttributes)
    {
        var tcs = new TaskCompletionSource <List <_MessageSinkMessage> >();

        ThreadPool.QueueUserWorkItem(async _ =>
        {
            try
            {
                TestContext.SetForInitialization(diagnosticMessageSink, internalDiagnosticMessageSink);

                await using var testFramework = new XunitTestFramework();

                var assemblyInfo = Reflector.Wrap(Assembly.GetEntryAssembly() !, additionalAssemblyAttributes);
                var discoverer   = testFramework.GetDiscoverer(assemblyInfo);
                var testCases    = new List <_ITestCase>();
                await discoverer.Find(testCase => { testCases.Add(testCase); return(new(true)); }, _TestFrameworkOptions.ForDiscovery(preEnumerateTheories: preEnumerateTheories), types);

                using var runSink = SpyMessageSink <_TestAssemblyFinished> .Create();
                var executor      = testFramework.GetExecutor(assemblyInfo);
                await executor.RunTestCases(testCases, runSink, _TestFrameworkOptions.ForExecution());

                tcs.TrySetResult(runSink.Messages.ToList());
            }
            catch (Exception ex)
            {
                tcs.TrySetException(ex);
            }
        });

        return(tcs.Task);
    }
 public ITestFrameworkDiscoverer GetDiscoverer(IAssemblyInfo assembly)
 {
     return(_underlyingFramework.GetDiscoverer(assembly));
 }
Esempio n. 4
0
        XElement ExecuteAssembly(object consoleLock,
                                 XunitProjectAssembly assembly,
                                 bool needsXml,
                                 bool?parallelizeTestCollections,
                                 int?maxThreadCount,
                                 bool diagnosticMessages,
                                 bool noColor,
                                 bool failSkips,
                                 bool stopOnFail,
                                 XunitFilters filters,
                                 bool internalDiagnosticMessages)
        {
            if (cancel)
            {
                return(null);
            }

            var assemblyElement = needsXml ? new XElement("assembly") : null;

            try
            {
                if (!ValidateFileExists(consoleLock, assembly.ConfigFilename))
                {
                    return(null);
                }

                // Turn off pre-enumeration of theories, since there is no theory selection UI in this runner
                assembly.Configuration.PreEnumerateTheories        = false;
                assembly.Configuration.DiagnosticMessages         |= diagnosticMessages;
                assembly.Configuration.InternalDiagnosticMessages |= internalDiagnosticMessages;

                // Setup discovery and execution options with command-line overrides
                var discoveryOptions = TestFrameworkOptions.ForDiscovery(assembly.Configuration);
                var executionOptions = TestFrameworkOptions.ForExecution(assembly.Configuration);
                executionOptions.SetStopOnTestFail(stopOnFail);
                if (maxThreadCount.HasValue)
                {
                    executionOptions.SetMaxParallelThreads(maxThreadCount);
                }
                if (parallelizeTestCollections.HasValue)
                {
                    executionOptions.SetDisableParallelization(!parallelizeTestCollections.GetValueOrDefault());
                }

                var assemblyDisplayName            = Path.GetFileNameWithoutExtension(assembly.AssemblyFilename);
                var diagnosticMessageSink          = DiagnosticMessageSink.ForDiagnostics(consoleLock, assemblyDisplayName, diagnosticMessages, noColor);
                var internalDiagnosticsMessageSink = DiagnosticMessageSink.ForInternalDiagnostics(consoleLock, assemblyDisplayName, internalDiagnosticMessages, noColor);
                var longRunningSeconds             = assembly.Configuration.LongRunningTestSecondsOrDefault;

                using var testFramework = new XunitTestFramework(diagnosticMessageSink, assembly.ConfigFilename);
                var entryAssembly = Assembly.GetEntryAssembly();
                var assemblyInfo  = new ReflectionAssemblyInfo(entryAssembly);
                var discoverySink = new TestDiscoverySink(() => cancel);

                using (var testDiscoverer = testFramework.GetDiscoverer(assemblyInfo))
                {
                    // Discover & filter the tests
                    reporterMessageHandler.OnMessage(new TestAssemblyDiscoveryStarting(assembly, appDomain: false, shadowCopy: false, discoveryOptions));

                    testDiscoverer.Find(includeSourceInformation: false, discoverySink, discoveryOptions);
                    discoverySink.Finished.WaitOne();
                }

                var testCasesDiscovered = discoverySink.TestCases.Count;
                var filteredTestCases   = discoverySink.TestCases.Where(filters.Filter).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);
                    if (assemblyElement != null)
                    {
                        resultsSink = new DelegatingXmlCreationSink(resultsSink, assemblyElement);
                    }
                    if (longRunningSeconds > 0)
                    {
                        resultsSink = new DelegatingLongRunningTestDetectionSink(resultsSink, TimeSpan.FromSeconds(longRunningSeconds), MessageSinkWithTypesAdapter.Wrap(diagnosticMessageSink));
                    }
                    if (failSkips)
                    {
                        resultsSink = new DelegatingFailSkipSink(resultsSink);
                    }

                    using var executor = testFramework.GetExecutor(entryAssembly.GetName());
                    executor.RunTests(filteredTestCases, resultsSink, executionOptions);
                    resultsSink.Finished.WaitOne();

                    executionSummary = resultsSink.ExecutionSummary;

                    reporterMessageHandler.OnMessage(new TestAssemblyExecutionFinished(assembly, executionOptions, resultsSink.ExecutionSummary));
                    if (stopOnFail && resultsSink.ExecutionSummary.Failed != 0)
                    {
                        Console.WriteLine("Canceling due to test failure...");
                        cancel = true;
                    }
                }
            }
            catch (Exception ex)
            {
                failed = true;

                var e = ex;
                while (e != null)
                {
                    Console.WriteLine($"{e.GetType().FullName}: {e.Message}");

                    if (internalDiagnosticMessages)
                    {
                        Console.WriteLine(e.StackTrace);
                    }

                    e = e.InnerException;
                }
            }

            return(assemblyElement);
        }