protected override void RunTestCases(IEnumerable<IXunitTestCase> testCases, IMessageSink messageSink, ITestFrameworkOptions executionOptions)
 {
     var cases = testCases.Where(t =>
         t.Class.GetCustomAttributes(typeof(RunIfConfiguredAttribute)) == null
         || TestConfig.Instance.IsConfigured);
     base.RunTestCases(cases, messageSink, executionOptions);
 }
        /// <inheritdoc/>
        public void Run(IMessageSink messageSink, ITestFrameworkOptions discoveryOptions, ITestFrameworkOptions executionOptions)
        {
            var discoverySink = new TestDiscoveryVisitor();

            using (var discoverer = new XunitTestFrameworkDiscoverer(assemblyInfo, sourceInformationProvider))
            {
                discoverer.Find(false, discoverySink, discoveryOptions);
                discoverySink.Finished.WaitOne();
            }

            Run(discoverySink.TestCases, messageSink, executionOptions);
        }
        /// <inheritdoc/>
        public void Find(bool includeSourceInformation, IMessageSink messageSink, ITestFrameworkOptions options)
        {
            Guard.ArgumentNotNull("messageSink", messageSink);
            Guard.ArgumentNotNull("options", options);

            ThreadPool.QueueUserWorkItem(_ =>
            {
                using (var messageBus = new MessageBus(messageSink))
                using (new PreserveWorkingFolder(AssemblyInfo))
                {
                    foreach (var type in AssemblyInfo.GetTypes(includePrivateTypes: false).Where(IsValidTestClass))
                        if (!FindTestsForTypeAndWrapExceptions(type, includeSourceInformation, messageBus))
                            break;

                    var warnings = Aggregator.GetAndClear<EnvironmentalWarning>().Select(w => w.Message).ToList();
                    messageBus.QueueMessage(new DiscoveryCompleteMessage(warnings));
                }
            });
        }
        /// <inheritdoc/>
        public void Find(string typeName, bool includeSourceInformation, IMessageSink messageSink, ITestFrameworkOptions options)
        {
            Guard.ArgumentNotNullOrEmpty("typeName", typeName);
            Guard.ArgumentNotNull("messageSink", messageSink);
            Guard.ArgumentNotNull("options", options);

            ThreadPool.QueueUserWorkItem(_ =>
            {
                using (var messageBus = new MessageBus(messageSink))
                using (new PreserveWorkingFolder(AssemblyInfo))
                {
                    var typeInfo = AssemblyInfo.GetType(typeName);
                    if (typeInfo != null && IsValidTestClass(typeInfo))
                        FindTestsForTypeAndWrapExceptions(typeInfo, includeSourceInformation, messageBus);

                    var warnings = Aggregator.GetAndClear<EnvironmentalWarning>().Select(w => w.Message).ToList();
                    messageBus.QueueMessage(new DiscoveryCompleteMessage(warnings));
                }
            });
        }
Example #5
0
 private bool FindTestsForTypeAndWrapExceptions(ITestClass testClass, bool includeSourceInformation, IMessageBus messageBus, ITestFrameworkOptions discoveryOptions)
 {
     try
     {
         return(FindTestsForType(testClass, includeSourceInformation, messageBus, discoveryOptions));
     }
     catch (Exception ex)
     {
         Aggregator.Add(new EnvironmentalWarning {
             Message = String.Format("Exception during discovery:{0}{1}", Environment.NewLine, ex)
         });
         return(true); // Keep going on to the next type
     }
 }
Example #6
0
 /// <inheritdoc/>
 void ITestFrameworkDiscoverer.Find(string typeName, bool includeSourceInformation, IMessageSink messageSink, ITestFrameworkOptions options)
 {
     discoverer.Find(typeName, includeSourceInformation, messageSink, options);
 }
Example #7
0
 /// <inheritdoc/>
 void ITestFrameworkDiscoverer.Find(string typeName, bool includeSourceInformation, IMessageSink messageSink, ITestFrameworkOptions options)
 {
     Find(msg => msg.TestCase.TestMethod.TestClass.Class.Name == typeName, includeSourceInformation, messageSink);
 }
Example #8
0
 /// <inheritdoc/>
 void ITestFrameworkDiscoverer.Find(bool includeSourceInformation, IMessageSink messageSink, ITestFrameworkOptions options)
 {
     Find(msg => true, includeSourceInformation, messageSink);
 }
Example #9
0
 void ITestFrameworkExecutor.RunAll(IMessageSink messageSink, ITestFrameworkOptions discoveryOptions, ITestFrameworkOptions executionOptions)
 {
     Run(messageSink);
 }
Example #10
0
 /// <inheritdoc/>
 void ITestFrameworkDiscoverer.Find(bool includeSourceInformation, IMessageSink messageSink, ITestFrameworkOptions options)
 {
     Find(msg => true, includeSourceInformation, messageSink);
 }
Example #11
0
 /// <inheritdoc/>
 public virtual void RunAll(IMessageSink messageSink, ITestFrameworkOptions discoveryOptions, ITestFrameworkOptions executionOptions)
 {
     InnerController.RunAll(messageSink, discoveryOptions, executionOptions);
 }
        public static TestableXunitTestAssemblyRunner Create(ITestAssembly assembly = null,
                                                             IXunitTestCase[] testCases = null,
                                                             ITestFrameworkOptions options = null)
        {
            if (testCases == null)
                testCases = new[] { Mocks.XunitTestCase<ClassUnderTest>("Passing") };

            return new TestableXunitTestAssemblyRunner(
                assembly ?? testCases.First().TestMethod.TestClass.TestCollection.TestAssembly,
                testCases ?? new IXunitTestCase[0],
                SpyMessageSink.Create(),
                options ?? new TestFrameworkOptions()
            );
        }
        /// <inheritdoc/>
        public void Find(string typeName, bool includeSourceInformation, IMessageSink messageSink, ITestFrameworkOptions options)
        {
            Guard.ArgumentNotNullOrEmpty("typeName", typeName);
            Guard.ArgumentNotNull("messageSink", messageSink);
            Guard.ArgumentNotNull("options", options);

            ThreadPool.QueueUserWorkItem(_ =>
            {
                using (var messageBus = new MessageBus(messageSink))
                {
                    ITypeInfo typeInfo = assemblyInfo.GetType(typeName);
                    if (typeInfo != null && (!typeInfo.IsAbstract || typeInfo.IsSealed))
                        FindImpl(typeInfo, includeSourceInformation, messageBus);

                    var warnings = messageAggregator.GetAndClear<EnvironmentalWarning>().Select(w => w.Message).ToList();
                    messageBus.QueueMessage(new DiscoveryCompleteMessage(warnings));
                }
            });
        }
        /// <inheritdoc/>
        public async void Run(IEnumerable<ITestCase> testCases, IMessageSink messageSink, ITestFrameworkOptions executionOptions)
        {
            Guard.ArgumentNotNull("testCases", testCases);
            Guard.ArgumentNotNull("messageSink", messageSink);
            Guard.ArgumentNotNull("executionOptions", executionOptions);

            var disableParallelization = false;
            var maxParallelThreads = 0;

            var collectionBehaviorAttribute = assemblyInfo.GetCustomAttributes(typeof(CollectionBehaviorAttribute)).SingleOrDefault();
            if (collectionBehaviorAttribute != null)
            {
                disableParallelization = collectionBehaviorAttribute.GetNamedArgument<bool>("DisableTestParallelization");
                maxParallelThreads = collectionBehaviorAttribute.GetNamedArgument<int>("MaxParallelThreads");
            }

            disableParallelization = executionOptions.GetValue<bool>(TestOptionsNames.Execution.DisableParallelization, disableParallelization);
            var maxParallelThreadsOption = executionOptions.GetValue<int>(TestOptionsNames.Execution.MaxParallelThreads, 0);
            if (maxParallelThreadsOption > 0)
                maxParallelThreads = maxParallelThreadsOption;

            var displayName = GetDisplayName(collectionBehaviorAttribute, disableParallelization, maxParallelThreads);
            var cancellationTokenSource = new CancellationTokenSource();
            var totalSummary = new RunSummary();
            var scheduler = maxParallelThreads > 0 ? new MaxConcurrencyTaskScheduler(maxParallelThreads) : TaskScheduler.Current;

            string currentDirectory = Directory.GetCurrentDirectory();

            var ordererAttribute = assemblyInfo.GetCustomAttributes(typeof(TestCaseOrdererAttribute)).SingleOrDefault();
            var orderer = ordererAttribute != null ? GetTestCaseOrderer(ordererAttribute) : new DefaultTestCaseOrderer();

            using (var messageBus = new MessageBus(messageSink))
            {
                try
                {
                    Directory.SetCurrentDirectory(Path.GetDirectoryName(assemblyInfo.AssemblyPath));

                    if (messageBus.QueueMessage(new TestAssemblyStarting(assemblyFileName, AppDomain.CurrentDomain.SetupInformation.ConfigurationFile, DateTime.Now,
                                                                         displayName, XunitTestFrameworkDiscoverer.DisplayName)))
                    {
                        IList<RunSummary> summaries;

                        // TODO: Contract for Run() states that null "testCases" means "run everything".

                        var masterStopwatch = Stopwatch.StartNew();

                        if (disableParallelization)
                        {
                            summaries = new List<RunSummary>();

                            foreach (var collectionGroup in testCases.Cast<XunitTestCase>().GroupBy(tc => tc.TestCollection))
                                summaries.Add(await RunTestCollectionAsync(messageBus, collectionGroup.Key, collectionGroup, orderer, cancellationTokenSource));
                        }
                        else
                        {
                            var tasks = testCases.Cast<XunitTestCase>()
                                                 .GroupBy(tc => tc.TestCollection)
                                                 .Select(collectionGroup => Task.Factory.StartNew(() => RunTestCollectionAsync(messageBus, collectionGroup.Key, collectionGroup, orderer, cancellationTokenSource),
                                                                                                  cancellationTokenSource.Token,
                                                                                                  TaskCreationOptions.None,
                                                                                                  scheduler))
                                                 .ToArray();

                            summaries = await Task.WhenAll(tasks.Select(t => t.Unwrap()));
                        }

                        totalSummary.Time = (decimal)masterStopwatch.Elapsed.TotalSeconds;
                        totalSummary.Total = summaries.Sum(s => s.Total);
                        totalSummary.Failed = summaries.Sum(s => s.Failed);
                        totalSummary.Skipped = summaries.Sum(s => s.Skipped);
                    }
                }
                finally
                {
                    messageBus.QueueMessage(new TestAssemblyFinished(assemblyInfo, totalSummary.Time, totalSummary.Total, totalSummary.Failed, totalSummary.Skipped));
                    Directory.SetCurrentDirectory(currentDirectory);
                }
            }
        }
Example #15
0
 /// <summary>
 /// Core implementation to discover unit tests in a given test class.
 /// </summary>
 /// <param name="testClass">The test class.</param>
 /// <param name="includeSourceInformation">Set to <c>true</c> to attempt to include source information.</param>
 /// <param name="messageBus">The message sink to send discovery messages to.</param>
 /// <param name="discoveryOptions">The options used by the test framework during discovery.</param>
 /// <returns>Returns <c>true</c> if discovery should continue; <c>false</c> otherwise.</returns>
 protected abstract bool FindTestsForType(ITestClass testClass, bool includeSourceInformation, IMessageBus messageBus, ITestFrameworkOptions discoveryOptions);
Example #16
0
        /// <inheritdoc/>
        public void Find(string typeName, bool includeSourceInformation, IMessageSink messageSink, ITestFrameworkOptions discoveryOptions)
        {
            Guard.ArgumentNotNullOrEmpty("typeName", typeName);
            Guard.ArgumentNotNull("messageSink", messageSink);
            Guard.ArgumentNotNull("discoveryOptions", discoveryOptions);

            Task.Run(() =>
            {
                using (var messageBus = new MessageBus(messageSink))
                    using (new PreserveWorkingFolder(AssemblyInfo))
                    {
                        var typeInfo = AssemblyInfo.GetType(typeName);
                        if (typeInfo != null && IsValidTestClass(typeInfo))
                        {
                            var testClass = CreateTestClass(typeInfo);
                            FindTestsForTypeAndWrapExceptions(testClass, includeSourceInformation, messageBus, discoveryOptions);
                        }

                        var warnings = Aggregator.GetAndClear <EnvironmentalWarning>().Select(w => w.Message).ToList();
                        messageBus.QueueMessage(new DiscoveryCompleteMessage(warnings));
                    }
            });
        }
Example #17
0
 /// <inheritdoc/>
 public virtual void RunTests(IEnumerable <ITestCase> testMethods, IMessageSink messageSink, ITestFrameworkOptions options)
 {
     InnerController.RunTests(testMethods, messageSink, options);
 }
Example #18
0
 /// <inheritdoc/>
 public virtual void RunAll(IMessageSink messageSink, ITestFrameworkOptions discoveryOptions, ITestFrameworkOptions executionOptions)
 {
     InnerController.RunAll(messageSink, discoveryOptions, executionOptions);
 }
Example #19
0
 /// <inheritdoc/>
 public virtual void Find(string typeName, bool includeSourceInformation, IMessageSink messageSink, ITestFrameworkOptions options)
 {
     InnerController.Find(typeName, includeSourceInformation, messageSink, options);
 }
        /// <inheritdoc/>
        public void Find(bool includeSourceInformation, IMessageSink messageSink, ITestFrameworkOptions options)
        {
            Guard.ArgumentNotNull("messageSink", messageSink);
            Guard.ArgumentNotNull("options", options);

            ThreadPool.QueueUserWorkItem(_ =>
            {
                using (var messageBus = new MessageBus(messageSink))
                {
                    foreach (var type in assemblyInfo.GetTypes(includePrivateTypes: false).Where(type => !type.IsAbstract || type.IsSealed))
                        if (!FindImpl(type, includeSourceInformation, messageBus))
                            break;

                    var warnings = messageAggregator.GetAndClear<EnvironmentalWarning>().Select(w => w.Message).ToList();
                    messageBus.QueueMessage(new DiscoveryCompleteMessage(warnings));
                }
            });
        }
Example #21
0
 /// <inheritdoc/>
 protected override async void RunTestCases(IEnumerable <IXunitTestCase> testCases, IMessageSink messageSink, ITestFrameworkOptions executionOptions)
 {
     using (var assemblyRunner = new XunitTestAssemblyRunner(TestAssembly, testCases, messageSink, executionOptions))
         await assemblyRunner.RunAsync();
 }
 TestableXunitTestAssemblyRunner(ITestAssembly testAssembly,
                                 IEnumerable<IXunitTestCase> testCases,
                                 IMessageSink messageSink,
                                 ITestFrameworkOptions executionOptions)
     : base(testAssembly, testCases, messageSink, executionOptions) { }
Example #23
0
 void ITestFrameworkExecutor.RunAll(IMessageSink messageSink, ITestFrameworkOptions discoveryOptions, ITestFrameworkOptions executionOptions)
 {
     executor.RunAll(messageSink, discoveryOptions, executionOptions);
 }
Example #24
0
 /// <inheritdoc/>
 public virtual void Find(string typeName, bool includeSourceInformation, IMessageSink messageSink, ITestFrameworkOptions options)
 {
     InnerController.Find(typeName, includeSourceInformation, messageSink, options);
 }
Example #25
0
 void ITestFrameworkExecutor.RunTests(IEnumerable <ITestCase> testCases, IMessageSink messageSink, ITestFrameworkOptions options)
 {
     executor.RunTests(testCases, messageSink, options);
 }
Example #26
0
 /// <inheritdoc/>
 public virtual void RunTests(IEnumerable<ITestCase> testMethods, IMessageSink messageSink, ITestFrameworkOptions options)
 {
     InnerController.RunTests(testMethods, messageSink, options);
 }
        /// <inheritdoc/>
        protected override bool FindTestsForType(ITestClass testClass, bool includeSourceInformation, IMessageBus messageBus, ITestFrameworkOptions discoveryOptions)
        {
            foreach (var method in testClass.Class.GetMethods(includePrivateMethods: true))
            {
                var testMethod = new TestMethod(testClass, method);
                if (!FindTestsForMethod(testMethod, includeSourceInformation, messageBus, discoveryOptions))
                {
                    return(false);
                }
            }

            return(true);
        }
Example #28
0
 /// <inheritdoc/>
 void ITestFrameworkDiscoverer.Find(string typeName, bool includeSourceInformation, IMessageSink messageSink, ITestFrameworkOptions options)
 {
     Find(msg => msg.TestCase.Class.Name == typeName, includeSourceInformation, messageSink);
 }
        /// <summary>
        /// Finds the tests on a test method.
        /// </summary>
        /// <param name="testMethod">The test method.</param>
        /// <param name="includeSourceInformation">Set to <c>true</c> to indicate that source information should be included.</param>
        /// <param name="messageBus">The message bus to report discovery messages to.</param>
        /// <param name="discoveryOptions">The options used by the test framework during discovery.</param>
        /// <returns>Return <c>true</c> to continue test discovery, <c>false</c>, otherwise.</returns>
        protected virtual bool FindTestsForMethod(ITestMethod testMethod, bool includeSourceInformation, IMessageBus messageBus, ITestFrameworkOptions discoveryOptions)
        {
            var factAttribute = testMethod.Method.GetCustomAttributes(typeof(FactAttribute)).FirstOrDefault();

            if (factAttribute == null)
            {
                return(true);
            }

            var testCaseDiscovererAttribute = factAttribute.GetCustomAttributes(typeof(XunitTestCaseDiscovererAttribute)).FirstOrDefault();

            if (testCaseDiscovererAttribute == null)
            {
                return(true);
            }

            var args           = testCaseDiscovererAttribute.GetConstructorArguments().Cast <string>().ToList();
            var discovererType = Reflector.GetType(args[1], args[0]);

            if (discovererType == null)
            {
                return(true);
            }

            var discoverer = GetDiscoverer(discovererType);

            if (discoverer == null)
            {
                return(true);
            }

            var methodDisplayString = discoveryOptions.GetValue <string>(TestOptionsNames.Discovery.MethodDisplay, null);
            var methodDisplay       = methodDisplayString == null ? TestMethodDisplay.ClassAndMethod : (TestMethodDisplay)Enum.Parse(typeof(TestMethodDisplay), methodDisplayString);

            foreach (var testCase in discoverer.Discover(methodDisplay, testMethod, factAttribute))
            {
                if (!ReportDiscoveredTestCase(testCase, includeSourceInformation, messageBus))
                {
                    return(false);
                }
            }

            return(true);
        }
Example #30
0
 void ITestFrameworkExecutor.RunTests(IEnumerable<ITestCase> testCases, IMessageSink messageSink, ITestFrameworkOptions options)
 {
     Run(testCases, messageSink);
 }
Example #31
0
 /// <inheritdoc/>
 public virtual void Find(bool includeSourceInformation, IMessageSink messageSink, ITestFrameworkOptions discoveryOptions)
 {
     InnerController.Find(includeSourceInformation, messageSink, discoveryOptions);
 }