Example #1
0
        IEnumerable <IGrouping <string, TestCase> > GetTests(IEnumerable <string> sources, IMessageLogger logger, XunitVisualStudioSettings settings, Stopwatch stopwatch)
        {
#if WIN8_STORE
            // For store apps, the files are copied to the AppX dir, we need to load it from there
            sources = sources.Select(s => Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), Path.GetFileName(s)));
#elif WINDOWS_PHONE_APP
            sources = sources.Select(s => Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, Path.GetFileName(s)));;
#endif

            var result = new List <IGrouping <string, TestCase> >();

            DiscoverTests(
                sources,
                logger,
                settings,
                (source, discoverer) => new VsExecutionDiscoveryVisitor(),
                (source, discoverer, visitor) =>
                result.Add(
                    new Grouping <string, TestCase>(
                        source,
                        visitor.TestCases
                        .GroupBy(tc => String.Format("{0}.{1}", tc.TestMethod.TestClass.Class.Name, tc.TestMethod.Method.Name))
                        .SelectMany(group => group.Select(testCase => VsDiscoveryVisitor.CreateVsTestCase(source, discoverer, testCase, settings, forceUniqueNames: group.Count() > 1)))
                        .ToList()
                        )
                    ),
                stopwatch
                );

            return(result);
        }
Example #2
0
        List <AssemblyRunInfo> GetTests(IEnumerable <string> sources, IMessageLogger logger, Stopwatch stopwatch)
        {
            // For store apps, the files are copied to the AppX dir, we need to load it from there
#if WINDOWS_PHONE_APP || WINDOWS_APP
            sources = sources.Select(s => Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, Path.GetFileName(s)));
#endif

            var result = new List <AssemblyRunInfo>();

            DiscoverTests(
                sources,
                logger,
                (source, discoverer, discoveryOptions) => new VsExecutionDiscoveryVisitor(),
                (source, discoverer, discoveryOptions, visitor) =>
                result.Add(
                    new AssemblyRunInfo
            {
                AssemblyFileName = source,
                Configuration    = ConfigReader.Load(source),
                TestCases        = visitor.TestCases
                                   .GroupBy(tc => String.Format("{0}.{1}", tc.TestMethod.TestClass.Class.Name, tc.TestMethod.Method.Name))
                                   .SelectMany(group => group.Select(testCase => VsDiscoveryVisitor.CreateVsTestCase(source,
                                                                                                                     discoverer,
                                                                                                                     testCase,
                                                                                                                     forceUniqueNames: group.Count() > 1,
                                                                                                                     logger: logger,
                                                                                                                     logDiscovery: discoveryOptions.GetDiagnosticMessagesOrDefault()))
                                               .Where(vsTestCase => vsTestCase != null))
                                   .ToList()
            }),
                stopwatch
                );

            return(result);
        }
        List <AssemblyRunInfo> GetTests(IEnumerable <string> sources, LoggerHelper logger, IRunContext runContext)
        {
            // For store apps, the files are copied to the AppX dir, we need to load it from there
#if PLATFORM_DOTNET
            sources = sources.Select(s => Path.Combine(Windows.ApplicationModel.Package.Current.InstalledLocation.Path, Path.GetFileName(s)));
#endif

            var result          = new List <AssemblyRunInfo>();
            var knownTraitNames = new HashSet <string>(StringComparer.OrdinalIgnoreCase);

            DiscoverTests(
                sources,
                logger,
                (source, discoverer, discoveryOptions) => new VsExecutionDiscoveryVisitor(),
                (source, discoverer, discoveryOptions, visitor) =>
            {
                var vsFilteredTestCases = visitor.TestCases.Select(testCase => VsDiscoveryVisitor.CreateVsTestCase(source, discoverer, testCase, false, logger: logger, knownTraitNames: knownTraitNames)).ToList();

                // Apply any filtering
                var filterHelper    = new TestCaseFilterHelper(knownTraitNames);
                vsFilteredTestCases = filterHelper.GetFilteredTestList(vsFilteredTestCases, runContext, logger, source).ToList();

                // Re-create testcases with unique names if there is more than 1
                var testCases = visitor.TestCases.Where(tc => vsFilteredTestCases.Any(vsTc => vsTc.DisplayName == tc.DisplayName)).GroupBy(tc => $"{tc.TestMethod.TestClass.Class.Name}.{tc.TestMethod.Method.Name}")
                                .SelectMany(group => group.Select(testCase =>
                                                                  VsDiscoveryVisitor.CreateVsTestCase(
                                                                      source,
                                                                      discoverer,
                                                                      testCase,
                                                                      forceUniqueNames: group.Count() > 1,
                                                                      logger: logger,
                                                                      knownTraitNames: knownTraitNames))
                                            .Where(vsTestCase => vsTestCase != null)).ToList();             // pre-enumerate these as it populates the known trait names collection

                var runInfo = new AssemblyRunInfo
                {
                    AssemblyFileName = source,
                    Configuration    = LoadConfiguration(source),
                    TestCases        = testCases
                };
                result.Add(runInfo);
            }
                );

            return(result);
        }
Example #4
0
        IEnumerable <IGrouping <string, TestCase> > GetTests(IEnumerable <string> sources, IMessageLogger logger, XunitVisualStudioSettings settings, Stopwatch stopwatch)
        {
            var result = new List <IGrouping <string, TestCase> >();

            DiscoverTests(
                sources,
                logger,
                settings,
                (source, discoverer) => new VsExecutionDiscoveryVisitor(),
                (source, discoverer, visitor) =>
                result.Add(
                    new Grouping <string, TestCase>(
                        source,
                        visitor.TestCases
                        .GroupBy(tc => String.Format("{0}.{1}", tc.Class.Name, tc.Method.Name))
                        .SelectMany(group => group.Select(testCase => VsDiscoveryVisitor.CreateVsTestCase(source, discoverer, testCase, settings, forceUniqueNames: group.Count() > 1)))
                        .ToList()
                        )
                    ),
                stopwatch
                );

            return(result);
        }
Example #5
0
        public void DiscoverTests(IEnumerable <string> sources, IDiscoveryContext discoveryContext, IMessageLogger logger, ITestCaseDiscoverySink discoverySink)
        {
            Guard.ArgumentNotNull("sources", sources);
            Guard.ArgumentNotNull("logger", logger);
            Guard.ArgumentNotNull("discoverySink", discoverySink);

            var settings  = SettingsProvider.Load();
            var stopwatch = Stopwatch.StartNew();

            try
            {
                RemotingUtility.CleanUpRegisteredChannels();

                if (settings.MessageDisplay == MessageDisplay.Diagnostic)
                {
                    logger.SendMessage(TestMessageLevel.Informational, String.Format("[xUnit.net {0}] Discovery started", stopwatch.Elapsed));
                }

                using (AssemblyHelper.SubscribeResolve())
                {
                    foreach (string assemblyFileName in sources)
                    {
                        var fileName = Path.GetFileName(assemblyFileName);

                        try
                        {
                            if (cancelled)
                            {
                                break;
                            }

                            if (!IsXunitTestAssembly(assemblyFileName))
                            {
                                if (settings.MessageDisplay == MessageDisplay.Diagnostic)
                                {
                                    logger.SendMessage(TestMessageLevel.Informational, String.Format("[xUnit.net {0}] Skipping: {1}", stopwatch.Elapsed, fileName));
                                }
                            }
                            else
                            {
                                if (settings.MessageDisplay == MessageDisplay.Diagnostic)
                                {
                                    logger.SendMessage(TestMessageLevel.Informational, String.Format("[xUnit.net {0}] Discovery starting: {1}", stopwatch.Elapsed, fileName));
                                }

                                using (var framework = new XunitFrontController(assemblyFileName, configFileName: null, shadowCopy: true))
                                    using (var sink = new VsDiscoveryVisitor(assemblyFileName, framework, logger, discoveryContext, discoverySink, () => cancelled))
                                    {
                                        framework.Find(includeSourceInformation: true, messageSink: sink, options: new TestFrameworkOptions());
                                        sink.Finished.WaitOne();

                                        if (settings.MessageDisplay == MessageDisplay.Diagnostic)
                                        {
                                            logger.SendMessage(TestMessageLevel.Informational,
                                                               String.Format("[xUnit.net {0}] Discovery finished: {1} ({2} tests)", stopwatch.Elapsed, fileName, sink.TotalTests));
                                        }
                                    }
                            }
                        }
                        catch (Exception e)
                        {
                            logger.SendMessage(TestMessageLevel.Error,
                                               String.Format("[xUnit.net {0}] Exception discovering tests from {1}: {2}", stopwatch.Elapsed, fileName, e));
                        }
                    }
                }
            }
            catch (Exception e)
            {
                logger.SendMessage(TestMessageLevel.Error,
                                   String.Format("[xUnit.net {0}] Exception discovering tests: {1}", stopwatch.Elapsed, e));
            }

            stopwatch.Stop();

            if (settings.MessageDisplay == MessageDisplay.Diagnostic)
            {
                logger.SendMessage(TestMessageLevel.Informational, String.Format("[xUnit.net {0}] Discovery complete", stopwatch.Elapsed));
            }
        }
Example #6
0
        IEnumerable <IGrouping <string, TestCase> > GetTests(IEnumerable <string> sources, IMessageLogger logger, XunitVisualStudioSettings settings, Stopwatch stopwatch)
        {
            var result = new List <IGrouping <string, TestCase> >();

            RemotingUtility.CleanUpRegisteredChannels();

            using (AssemblyHelper.SubscribeResolve())
            {
                if (settings.MessageDisplay == MessageDisplay.Diagnostic)
                {
                    logger.SendMessage(TestMessageLevel.Informational, String.Format("[xUnit.net {0}] Discovery started", stopwatch.Elapsed));
                }

                foreach (string assemblyFileName in sources)
                {
                    var fileName = Path.GetFileName(assemblyFileName);

                    try
                    {
                        if (cancelled)
                        {
                            break;
                        }

                        if (!IsXunitTestAssembly(assemblyFileName))
                        {
                            if (settings.MessageDisplay == MessageDisplay.Diagnostic)
                            {
                                logger.SendMessage(TestMessageLevel.Informational, String.Format("[xUnit.net {0}] Skipping: {1}", stopwatch.Elapsed, fileName));
                            }
                        }
                        else
                        {
                            if (settings.MessageDisplay == MessageDisplay.Diagnostic)
                            {
                                logger.SendMessage(TestMessageLevel.Informational, String.Format("[xUnit.net {0}] Discovery starting: {1}", stopwatch.Elapsed, fileName));
                            }

                            using (var framework = new XunitFrontController(assemblyFileName, configFileName: null, shadowCopy: true))
                                using (var sink = new TestDiscoveryVisitor())
                                {
                                    framework.Find(includeSourceInformation: true, messageSink: sink, options: new TestFrameworkOptions());
                                    sink.Finished.WaitOne();

                                    result.Add(
                                        new Grouping <string, TestCase>(
                                            assemblyFileName,
                                            sink.TestCases
                                            .GroupBy(tc => String.Format("{0}.{1}", tc.Class.Name, tc.Method.Name))
                                            .SelectMany(group => group.Select(testCase => VsDiscoveryVisitor.CreateVsTestCase(assemblyFileName, framework, testCase, settings, forceUniqueNames: group.Count() > 1)))
                                            .ToList()
                                            )
                                        );

                                    if (settings.MessageDisplay != MessageDisplay.None)
                                    {
                                        logger.SendMessage(TestMessageLevel.Informational,
                                                           String.Format("[xUnit.net {0}] Discovery finished: {1} ({2} tests)", stopwatch.Elapsed, Path.GetFileName(assemblyFileName), sink.TestCases.Count));
                                    }
                                }
                        }
                    }
                    catch (Exception e)
                    {
                        logger.SendMessage(TestMessageLevel.Error,
                                           String.Format("[xUnit.net {0}] Exception discovering tests from {1}: {2}", stopwatch.Elapsed, assemblyFileName, e));
                    }
                }

                if (settings.MessageDisplay == MessageDisplay.Diagnostic)
                {
                    logger.SendMessage(TestMessageLevel.Informational, String.Format("[xUnit.net {0}] Discovery complete", stopwatch.Elapsed));
                }

                return(result);
            }
        }
Example #7
0
        IEnumerable <IGrouping <string, TestCase> > GetTests(IEnumerable <string> sources, IMessageLogger logger, XunitVisualStudioSettings settings, Stopwatch stopwatch)
        {
            var sourceSinks = new List <SourceSink <TestDiscoveryVisitor> >();
            var result      = new List <IGrouping <string, TestCase> >();

            try
            {
                if (settings.MessageDisplay == MessageDisplay.Diagnostic)
                {
                    lock (stopwatch)
                        logger.SendMessage(TestMessageLevel.Informational, String.Format("[xUnit.net {0}] Discovery started", stopwatch.Elapsed));
                }

                foreach (string assemblyFileName in sources)
                {
                    try
                    {
                        if (cancelled)
                        {
                            break;
                        }

                        if (!IsXunitTestAssembly(assemblyFileName))
                        {
                            if (settings.MessageDisplay == MessageDisplay.Diagnostic)
                            {
                                lock (stopwatch)
                                    logger.SendMessage(TestMessageLevel.Informational, String.Format("[xUnit.net {0}] Skipping: {1}", stopwatch.Elapsed, Path.GetFileName(assemblyFileName)));
                            }
                        }
                        else
                        {
                            if (settings.MessageDisplay == MessageDisplay.Diagnostic)
                            {
                                lock (stopwatch)
                                    logger.SendMessage(TestMessageLevel.Informational, String.Format("[xUnit.net {0}] Discovery starting: {1}", stopwatch.Elapsed, Path.GetFileName(assemblyFileName)));
                            }

                            var framework = new XunitFrontController(assemblyFileName, configFileName: null, shadowCopy: true);
                            var sink      = new TestDiscoveryVisitor();
                            sourceSinks.Add(new SourceSink <TestDiscoveryVisitor> {
                                Framework = framework, Sink = sink, AssemblyFileName = assemblyFileName
                            });
                            framework.Find(includeSourceInformation: true, messageSink: sink);
                        }
                    }
                    catch (Exception e)
                    {
                        lock (stopwatch)
                            logger.SendMessage(TestMessageLevel.Error,
                                               String.Format("[xUnit.net {0}] Exception discovering tests from {1}: {2}", stopwatch.Elapsed, assemblyFileName, e));
                    }
                }

                var toFinish = new List <SourceSink <TestDiscoveryVisitor> >(sourceSinks);

                while (toFinish.Count > 0)
                {
                    var finishedIdx = WaitHandle.WaitAny(sourceSinks.Select(sink => sink.Sink.Finished).ToArray());
                    var sourceSink  = toFinish[finishedIdx];

                    result.Add(
                        new Grouping <string, TestCase>(
                            sourceSink.AssemblyFileName,
                            sourceSink.Sink.TestCases
                            .GroupBy(tc => String.Format("{0}.{1}", tc.Class.Name, tc.Method.Name))
                            .SelectMany(group => group.Select(testCase => VsDiscoveryVisitor.CreateVsTestCase(sourceSink.AssemblyFileName, sourceSink.Framework, testCase, settings, forceUniqueNames: group.Count() > 1)))
                            .ToList()
                            )
                        );

                    if (settings.MessageDisplay != MessageDisplay.None)
                    {
                        lock (stopwatch)
                            logger.SendMessage(TestMessageLevel.Informational,
                                               String.Format("[xUnit.net {0}] Discovery finished: {1} ({2} tests)", stopwatch.Elapsed, Path.GetFileName(sourceSink.AssemblyFileName), sourceSink.Sink.TestCases.Count));
                    }

                    toFinish.RemoveAt(finishedIdx);
                }
            }
            finally
            {
                foreach (var sourceSink in sourceSinks)
                {
                    sourceSink.Sink.Dispose();
                    sourceSink.Framework.Dispose();
                }
            }

            if (settings.MessageDisplay == MessageDisplay.Diagnostic)
            {
                lock (stopwatch)
                    logger.SendMessage(TestMessageLevel.Informational, String.Format("[xUnit.net {0}] Discovery complete", stopwatch.Elapsed));
            }

            return(result);
        }