Esempio n. 1
0
        public void SendTestFound()
        {
            var test = CreateMockTest();

            _testSink.SendTestFound(test);
            var message = GetMessage();

            Assert.That(message, Is.Not.Null);
            Assert.That(message.MessageType, Is.EqualTo(Messages.TestFound));
            AssertAreEqual(test, GetPayload <MsTest>(message));
        }
Esempio n. 2
0
        void OnTestCaseFound(XElement testCase)
        {
            var test = ParseTest(testCase);

            if (Options.DesignTime)
            {
                _sink.SendTestFound(test);
            }
            else
            {
                Console.WriteLine(test.FullyQualifiedName);
            }
        }
Esempio n. 3
0
        XElement ExecuteAssembly(object consoleLock,
                                 XunitProjectAssembly2 assembly,
                                 bool needsXml,
                                 bool?parallelizeTestCollections,
                                 int?maxThreadCount,
                                 bool diagnosticMessages,
                                 bool noColor,
                                 AppDomainSupport?appDomain,
                                 bool failSkips,
                                 XunitFilters filters,
                                 bool designTime,
                                 bool listTestCases,
                                 IReadOnlyList <string> designTimeFullyQualifiedNames)
        {
            if (cancel)
            {
                return(null);
            }

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

            try
            {
                // if we had a config file use it
                var config = assembly.ConfigFilename != null ? assembly.Configuration : assembly.ConfigurationStream;

                // Turn off pre-enumeration of theories when we're not running in Visual Studio
                if (!designTime)
                {
                    config.PreEnumerateTheories = false;
                }
                if (appDomain.HasValue)
                {
                    config.AppDomain = appDomain.GetValueOrDefault();
                }
                if (diagnosticMessages)
                {
                    config.DiagnosticMessages = true;
                }

                var discoveryOptions = TestFrameworkOptions.ForDiscovery(config);
                var executionOptions = TestFrameworkOptions.ForExecution(config);
                if (maxThreadCount.HasValue)
                {
                    executionOptions.SetMaxParallelThreads(maxThreadCount);
                }
                if (parallelizeTestCollections.HasValue)
                {
                    executionOptions.SetDisableParallelization(!parallelizeTestCollections.GetValueOrDefault());
                }

                var assemblyDisplayName       = Path.GetFileNameWithoutExtension(assembly.AssemblyFilename);
                var diagnosticMessageSink     = new DiagnosticMessageSink(consoleLock, assemblyDisplayName, config.DiagnosticMessagesOrDefault, noColor);
                var appDomainSupport          = config.AppDomainOrDefault;
                var shadowCopy                = config.ShadowCopyOrDefault;
                var longRunningSeconds        = config.LongRunningTestSecondsOrDefault;
                var sourceInformationProvider = GetSourceInformationProviderAdapater(assembly);

                using (var controller = new XunitFrontController(appDomainSupport, assembly.AssemblyFilename, assembly.ConfigFilename, shadowCopy, diagnosticMessageSink: diagnosticMessageSink, sourceInformationProvider: sourceInformationProvider))
                    using (var discoverySink = new TestDiscoverySink())
                    {
                        var includeSourceInformation = designTime && listTestCases;

                        // Discover & filter the tests
                        reporterMessageHandler.OnMessage(new TestAssemblyDiscoveryStarting(assembly, controller.CanUseAppDomains && appDomainSupport != AppDomainSupport.Denied, shadowCopy, discoveryOptions));

                        controller.Find(includeSourceInformation: includeSourceInformation, messageSink: discoverySink, discoveryOptions: discoveryOptions);
                        discoverySink.Finished.WaitOne();

                        IDictionary <ITestCase, VsTestCase> vsTestCases = null;
                        if (designTime)
                        {
                            vsTestCases = DesignTimeTestConverter.Convert(discoverySink.TestCases);
                        }

                        if (listTestCases)
                        {
                            lock (consoleLock)
                            {
                                if (designTime)
                                {
                                    foreach (var testcase in vsTestCases.Values)
                                    {
                                        testDiscoverySink?.SendTestFound(testcase);

                                        Console.WriteLine(testcase.FullyQualifiedName);
                                    }
                                }
                                else
                                {
                                    foreach (var testcase in discoverySink.TestCases)
                                    {
                                        Console.WriteLine(testcase.DisplayName);
                                    }
                                }
                            }

                            return(assemblyElement);
                        }

                        IExecutionSink resultsSink;

                        if (designTime)
                        {
                            resultsSink = new DesignTimeExecutionSink(testExecutionSink, vsTestCases, reporterMessageHandler);
                        }
                        else
                        {
                            resultsSink = new XmlAggregateSink(reporterMessageHandler, assemblyElement, diagnosticMessageSink, completionMessages, () => cancel, longRunningSeconds);
                        }

                        if (failSkips)
                        {
                            resultsSink = new FailSkipSink(resultsSink);
                        }

                        IList <ITestCase> filteredTestCases;
                        var testCasesDiscovered = discoverySink.TestCases.Count;
                        if (!designTime || designTimeFullyQualifiedNames.Count == 0)
                        {
                            filteredTestCases = discoverySink.TestCases.Where(filters.Filter).ToList();
                        }
                        else
                        {
                            filteredTestCases = vsTestCases.Where(t => designTimeFullyQualifiedNames.Contains(t.Value.FullyQualifiedName)).Select(t => t.Key).ToList();
                        }
                        var testCasesToRun = filteredTestCases.Count;

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

                        if (filteredTestCases.Count == 0)
                        {
                            completionMessages.TryAdd(Path.GetFileName(assembly.AssemblyFilename), new ExecutionSummary());
                        }
                        else
                        {
                            reporterMessageHandler.OnMessage(new TestAssemblyExecutionStarting(assembly, executionOptions));

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

                            reporterMessageHandler.OnMessage(new TestAssemblyExecutionFinished(assembly, executionOptions, resultsSink.ExecutionSummary));
                        }
                    }
            }
            catch (Exception ex)
            {
                failed = true;

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

            return(assemblyElement);
        }
Esempio n. 4
0
        int Execute()
        {
            var designTimeFullyQualifiedNames = SetupSinks(args);

            var results = new List <TestResultWrapper>();

            foreach (var assembly in args.Inputs)
            {
                var assemblyPath = Path.GetFullPath(assembly);
                var testAssembly = LoadAssembly(assemblyPath);
                var libraryPath  = Path.Combine(Path.GetDirectoryName(assemblyPath), "Persimmon.dll");
                var library      = LoadAssembly(libraryPath);

                var driver = new PersimmonDriver(library, testAssembly);
                var tests  = driver.CollectTests();

                if (args.DesignTime && designTimeFullyQualifiedNames.Any())
                {
                    tests = tests.Where(t => designTimeFullyQualifiedNames.Contains(t.Test.FullyQualifiedName));
                }

                if (args.List)
                {
                    foreach (var test in tests)
                    {
                        testDiscoverySink.SendTestFound(test.Test);
                    }
                }
                else
                {
                    Action <object> before = testCase =>
                    {
                        if (args.DesignTime)
                        {
                            testExecutionSink.SendTestStarted(new TestCaseWrapper(testCase).Test);
                        }
                    };
                    Action <object> progress = result =>
                    {
                        var wrapper = new TestResultWrapper(result);
                        // TODO: send async
                        //if (args.DesignTime)
                        //{
                        //    testExecutionSink.SendTestResult(wrapper.TestResult);
                        //}
                        //else
                        if (!args.DesignTime)
                        {
                            switch (wrapper.TestResult.Outcome)
                            {
                            case TestOutcome.Passed:
                                Console.Write(".");
                                break;

                            case TestOutcome.Failed:
                                if (wrapper.Exceptions.Any())
                                {
                                    Console.Write("E");
                                }
                                else
                                {
                                    Console.Write("x");
                                }
                                break;

                            case TestOutcome.Skipped:
                                Console.Write("_");
                                break;

                            default:
                                break;
                            }
                        }
                        results.Add(wrapper);
                    };
                    driver.RunTests(tests.Select(t => t.Test.FullyQualifiedName).ToArray(), before, progress);
                }
            }

            // TODO: remove after implement `send async`
            if (args.DesignTime)
            {
                foreach (var wrapper in results)
                {
                    testExecutionSink.SendTestResult(wrapper.TestResult);
                }
            }

            if (args.List)
            {
                if (args.DesignTime)
                {
                    testDiscoverySink.SendTestCompleted();
                }
                return(0);
            }

            if (args.DesignTime)
            {
                testExecutionSink.SendTestCompleted();
                return(0);
            }

            var reporter = new Reporter(results, ColorConsole);

            reporter.Report();

            return(results.Where(r => r.TestResult.Outcome == TestOutcome.Failed).Count());
        }