Esempio n. 1
0
        /// <inheritdoc/>
        protected void SetTestCaseOrderer(IReflectionTypeInfo Class)
        {
            var ordererAttribute = Class.GetCustomAttributes(typeof(TestCaseOrdererAttribute)).SingleOrDefault();

            if (ordererAttribute != null)
            {
                try
                {
                    var testCaseOrderer = ExtensibilityPointFactory.GetTestCaseOrderer(diagnosticMessageSink, ordererAttribute);
                    if (testCaseOrderer != null)
                    {
                        TestCaseOrderer = testCaseOrderer;
                    }
                    else
                    {
                        var args = ordererAttribute.GetConstructorArguments().Cast <string>().ToList();
                        diagnosticMessageSink.OnMessage(new DiagnosticMessage($"Could not find type '{args[0]}' in {args[1]} for class-level test case orderer on test class '{Class.Name}'"));
                    }
                }
                catch (Exception ex)
                {
                    var innerEx = ex.InnerException ?? ex; //.Unwrap();
                    var args    = ordererAttribute.GetConstructorArguments().Cast <string>().ToList();
                    diagnosticMessageSink.OnMessage(new DiagnosticMessage($"Class-level test case orderer '{args[0]}' for test class '{Class.Name}' threw '{innerEx.GetType().FullName}' " +
                                                                          $"during construction: {innerEx.Message}{Environment.NewLine}{innerEx.StackTrace}"));
                }
            }
        }
        static object[] GetTestMethodArguments(ITestMethod testMethod, int attributeNumber, string rowName, IMessageSink diagnosticMessageSink)
        {
            try
            {
                IAttributeInfo dataAttribute = testMethod.Method.GetCustomAttributes(typeof(DataAttribute)).Where((x, i) => i == attributeNumber).FirstOrDefault();
                if (dataAttribute == null)
                {
                    return(null);
                }

                IAttributeInfo  discovererAttribute = dataAttribute.GetCustomAttributes(typeof(DataDiscovererAttribute)).First();
                IDataDiscoverer discoverer          = ExtensibilityPointFactory.GetDataDiscoverer(diagnosticMessageSink, discovererAttribute);

                IEnumerable <object[]> data = discoverer.GetData(dataAttribute, testMethod.Method);

                if (data is IDictionary <string, object[]> )
                {
                    return(((IDictionary <string, object[]>)data)[rowName]);
                }

                return(data.Where(x => x[0].ToString() == rowName).FirstOrDefault());
            }
            catch
            {
                return(null);
            }
        }
Esempio n. 3
0
        protected override async Task AfterTestCaseStartingAsync()
        {
            await base.AfterTestCaseStartingAsync();

            try
            {
                var dataAttributes = this.TestCase.TestMethod.Method.GetCustomAttributes(typeof(DataAttribute)).ToList();
                foreach (var dataAttribute in dataAttributes)
                {
                    var discovererAttribute = dataAttribute.GetCustomAttributes(typeof(DataDiscovererAttribute)).First();
                    var discoverer          =
                        ExtensibilityPointFactory.GetDataDiscoverer(this.diagnosticMessageSink, discovererAttribute);

                    foreach (var dataRow in discoverer.GetData(dataAttribute, this.TestCase.TestMethod.Method))
                    {
                        this.disposables.AddRange(dataRow.OfType <IDisposable>());
                        this.scenarioRunners.Add(this.scenarioRunnerFactory.Create(dataRow));
                    }
                }

                if (!this.scenarioRunners.Any())
                {
                    this.scenarioRunners.Add(this.scenarioRunnerFactory.Create(noArguments));
                }
            }
            catch (Exception ex)
            {
                this.dataDiscoveryException = ex;
            }
        }
        /// <summary>
        /// Gives an opportunity to override test case orderer. By default, this method gets the
        /// orderer from the collection definition. If this function returns <c>null</c>, the
        /// test case orderer passed into the constructor will be used.
        /// </summary>
        protected virtual ITestCaseOrderer GetTestCaseOrderer()
        {
            if (TestCollection.CollectionDefinition != null)
            {
                var ordererAttribute = TestCollection.CollectionDefinition.GetCustomAttributes(typeof(TestCaseOrdererAttribute)).SingleOrDefault();
                if (ordererAttribute != null)
                {
                    try
                    {
                        var testCaseOrderer = ExtensibilityPointFactory.GetTestCaseOrderer(DiagnosticMessageSink, ordererAttribute);
                        if (testCaseOrderer != null)
                        {
                            return(testCaseOrderer);
                        }

                        var args = ordererAttribute.GetConstructorArguments().Cast <string>().ToList();
                        DiagnosticMessageSink.OnMessage(new DiagnosticMessage($"Could not find type '{args[0]}' in {args[1]} for collection-level test case orderer on test collection '{TestCollection.DisplayName}'"));
                    }
                    catch (Exception ex)
                    {
                        var innerEx = ex.Unwrap();
                        var args    = ordererAttribute.GetConstructorArguments().Cast <string>().ToList();
                        DiagnosticMessageSink.OnMessage(new DiagnosticMessage($"Collection-level test case orderer '{args[0]}' for test collection '{TestCollection.DisplayName}' threw '{innerEx.GetType().FullName}' during construction: {innerEx.Message}{Environment.NewLine}{innerEx.StackTrace}"));
                    }
                }
            }

            return(null);
        }
    public async void CanRunTests()
    {
        Assert.SkipWhen(EnvironmentHelper.IsMono, "Mono does not fully support dynamic assemblies");

        var assemblyInfo = new ReflectionAssemblyInfo(fixture.Assembly);

        await using var disposalTracker = new DisposalTracker();
        var testFramework = ExtensibilityPointFactory.GetTestFramework(assemblyInfo);

        disposalTracker.Add(testFramework);

        var messages     = new List <_MessageSinkMessage>();
        var testExecutor = testFramework.GetExecutor(assemblyInfo);
        await testExecutor.RunAll(SpyMessageSink.Create(messages: messages), _TestFrameworkOptions.ForDiscovery(), _TestFrameworkOptions.ForExecution());

        var assemblyStarting = Assert.Single(messages.OfType <_TestAssemblyStarting>());

        Assert.Equal("DynamicAssembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null", assemblyStarting.AssemblyName);
        Assert.Null(assemblyStarting.AssemblyPath);
        Assert.Null(assemblyStarting.ConfigFilePath);

        Assert.Single(messages.OfType <_TestFailed>());
        Assert.Single(messages.OfType <_TestPassed>());
        Assert.Empty(messages.OfType <_TestSkipped>());
    }
Esempio n. 6
0
        /// <inheritdoc/>
        protected override void Initialize()
        {
            base.Initialize();

            var factAttribute   = TestMethod.Method.GetCustomAttributes(typeof(FactAttribute)).First();
            var baseDisplayName = factAttribute.GetNamedArgument <string>("DisplayName") ?? BaseDisplayName;

            DisplayName = GetDisplayName(factAttribute, baseDisplayName);
            SkipReason  = GetSkipReason(factAttribute);

            foreach (var traitAttribute in GetTraitAttributesData(TestMethod))
            {
                var discovererAttribute = traitAttribute
                                          .GetCustomAttributes(typeof(TraitDiscovererAttribute)).FirstOrDefault();

                if (discovererAttribute != null)
                {
                    var discoverer = ExtensibilityPointFactory.GetTraitDiscoverer(_MessageSink, discovererAttribute);
                    if (discoverer != null)
                    {
                        foreach (var keyValuePair in discoverer.GetTraits(traitAttribute))
                        {
                            Add(Traits, keyValuePair.Key, keyValuePair.Value);
                        }
                    }
                }
                else
                {
                    _MessageSink.OnMessage(
                        new DiagnosticMessage(
                            $"Trait attribute on '{DisplayName}' did not have [TraitDiscoverer]"));
                }
            }
        }
Esempio n. 7
0
    public WasmRunner(XunitProject project)
    {
        this.project = project;

        var assemblyFileName = "/" + project.Assemblies.First().AssemblyFilename;

        testCases = new List <ITestCase> ();

        var assembly     = Assembly.LoadFrom(assemblyFileName);
        var assemblyInfo = new Xunit.Sdk.ReflectionAssemblyInfo(assembly);

        var collectionBehaviorAttribute = assemblyInfo.GetCustomAttributes(typeof(CollectionBehaviorAttribute)).SingleOrDefault();
        var testAssembly = new TestAssembly(assemblyInfo, null);

        var collectionFactory = ExtensibilityPointFactory.GetXunitTestCollectionFactory(this, collectionBehaviorAttribute, testAssembly);

        /*
         * object res = null;
         * res = Activator.CreateInstance (typeof (Xunit.Sdk.MemberDataDiscoverer), true, true);
         * Console.WriteLine ("DISC2: " + res);
         */

        DiscoveryOptions = TestFrameworkOptions.ForDiscovery(null);
        executionOptions = TestFrameworkOptions.ForExecution(null);

        discoverer = new Discoverer(assemblyInfo, new NullSourceInformationProvider(), this, DiscoveryOptions, collectionFactory);

        executor = new XunitTestFrameworkExecutor(assembly.GetName(), new NullSourceInformationProvider(), this);
    }
Esempio n. 8
0
        static object[] GetTestMethodArguments(ITestMethod testMethod, int attributeNumber, int rowNumber, IMessageSink diagnosticMessageSink)
        {
            try
            {
                IAttributeInfo dataAttribute =
                    testMethod.Method.GetCustomAttributes(typeof(DataAttribute))
                    .Where((x, i) => i == attributeNumber)
                    .FirstOrDefault();

                if (dataAttribute == null)
                {
                    return(null);
                }

                IAttributeInfo discovererAttribute =
                    dataAttribute.GetCustomAttributes(typeof(DataDiscovererAttribute)).First();

                IDataDiscoverer discoverer =
                    ExtensibilityPointFactory.GetDataDiscoverer(diagnosticMessageSink, discovererAttribute);

                return
                    (discoverer.GetData(dataAttribute, testMethod.Method)
                     .Where((x, i) => i == rowNumber)
                     .FirstOrDefault());
            }
            catch
            {
                return(null);
            }
        }
        /// <inheritdoc/>
        protected override string GetTestFrameworkEnvironment()
        {
            this.Initialize();

            var testCollectionFactory = ExtensibilityPointFactory.GetXunitTestCollectionFactory(DiagnosticMessageSink, collectionBehaviorAttribute, TestAssembly);
            var threadCountText       = maxParallelThreads < 0 ? "unlimited" : maxParallelThreads.ToString();

            return($"{base.GetTestFrameworkEnvironment()} [{testCollectionFactory.DisplayName}, {(disableParallelization ? "non-parallel" : $"parallel ({threadCountText} threads)")}]");
Esempio n. 10
0
        public void DefaultTestCollectionFactoryIsCollectionPerClass()
        {
            var assembly = Mocks.TestAssembly();

            var result = ExtensibilityPointFactory.GetXunitTestCollectionFactory(spy, (IAttributeInfo)null, assembly);

            Assert.IsType <CollectionPerClassTestCollectionFactory>(result);
        }
Esempio n. 11
0
 /// <summary>
 /// Initializes a new instance of the <see cref="T:Xunit.Sdk.XunitTestAssemblyRunner"/> class.
 /// </summary>
 /// <param name="testAssembly">The assembly that contains the tests to be run.</param><param name="testCases">The test cases to be run.</param><param name="diagnosticMessageSink">The message sink to report diagnostic messages to.</param><param name="executionMessageSink">The message sink to report run status to.</param><param name="executionOptions">The user's requested execution options.</param>
 public BrowserTestAssemblyRunner(ITestAssembly testAssembly, IEnumerable <IXunitTestCase> testCases, IMessageSink diagnosticMessageSink, IMessageSink executionMessageSink, ITestFrameworkExecutionOptions executionOptions)
     : base(testAssembly, testCases.Cast <BrowserTestCase>().ToList(), diagnosticMessageSink, executionMessageSink, executionOptions)
 {
     BeforeAfterHandlers =
         testAssembly.Assembly.GetCustomAttributes(typeof(BeforeAfterAssemblyTestsAttribute))
         .Select(attributeInfo => attributeInfo.GetNamedArgument <Type>("HandlerType"))
         .Select(handlerType => ExtensibilityPointFactory.Get <IBeforeAfterAssemblyTests>(diagnosticMessageSink, handlerType))
         .ToList();
 }
Esempio n. 12
0
        public void NoAttribute()
        {
            var assembly = Mocks.AssemblyInfo();

            var framework = ExtensibilityPointFactory.GetTestFramework(spy, assembly);

            Assert.IsType <XunitTestFramework>(framework);
            Assert.Empty(messages);
        }
Esempio n. 13
0
        public void UserCanChooseFromBuiltInCollectionFactories_NonParallel(CollectionBehavior behavior, Type expectedType)
        {
            var attr     = Mocks.CollectionBehaviorAttribute(behavior);
            var assembly = Mocks.TestAssembly();

            var result = ExtensibilityPointFactory.GetXunitTestCollectionFactory(spy, attr, assembly);

            Assert.IsType(expectedType, result);
        }
Esempio n. 14
0
        public static void IncompatibleOrInvalidTypesGetDefaultBehavior(string factoryTypeName)
        {
            var attr     = Mocks.CollectionBehaviorAttribute(factoryTypeName, "test.xunit.execution");
            var assembly = Mocks.TestAssembly();

            var result = ExtensibilityPointFactory.GetXunitTestCollectionFactory(attr, assembly);

            Assert.IsType <CollectionPerClassTestCollectionFactory>(result);
        }
Esempio n. 15
0
        public void Attribute_NoDiscoverer()
        {
            var attribute = Mocks.TestFrameworkAttribute <AttributeWithoutDiscoverer>();
            var assembly  = Mocks.AssemblyInfo(attributes: new[] { attribute });

            var framework = ExtensibilityPointFactory.GetTestFramework(spy, assembly);

            Assert.IsType <XunitTestFramework>(framework);
            AssertSingleDiagnosticMessage("Assembly-level test framework attribute was not decorated with [TestFrameworkDiscoverer]");
        }
Esempio n. 16
0
        public void UserCanChooseCustomCollectionFactoryWithType()
        {
            var attr     = Mocks.CollectionBehaviorAttribute <MyTestCollectionFactory>();
            var assembly = Mocks.TestAssembly();

            var result = ExtensibilityPointFactory.GetXunitTestCollectionFactory(spy, attr, assembly);

            var myFactory = Assert.IsType <MyTestCollectionFactory>(result);

            Assert.Same(assembly, myFactory.Assembly);
        }
        public void Attribute_WithDiscoverer_NoMessageSink()
        {
            var spy = SpyMessageSink.Capture();

            TestContext.Current !.DiagnosticMessageSink = spy;
            var attribute = Mocks.TestFrameworkAttribute <AttributeWithDiscoverer>();
            var assembly  = Mocks.AssemblyInfo(attributes: new[] { attribute });

            ExtensibilityPointFactory.GetTestFramework(assembly);

            Assert.Empty(spy.Messages);
        }
Esempio n. 18
0
 /// <summary>
 /// Gets the test case discover instance for the given discoverer type. The instances are cached
 /// and reused, since they should not be stateful.
 /// </summary>
 /// <param name="discovererType">The discoverer type.</param>
 /// <returns>Returns the test case discoverer instance.</returns>
 protected IXunitTestCaseDiscoverer GetDiscoverer(Type discovererType)
 {
     try
     {
         return(ExtensibilityPointFactory.GetXunitTestCaseDiscoverer(DiagnosticMessageSink, discovererType));
     }
     catch (Exception ex)
     {
         DiagnosticMessageSink.OnMessage(new DiagnosticMessage($"Discoverer type '{discovererType.FullName}' could not be created or does not implement IXunitTestCaseDiscoverer: {ex.Unwrap()}"));
         return(null);
     }
 }
Esempio n. 19
0
        public static void UserCanChooseCustomCollectionFactory()
        {
            var factoryType = typeof(MyTestCollectionFactory);
            var attr        = Mocks.CollectionBehaviorAttribute(factoryType.FullName, factoryType.Assembly.FullName);
            var assembly    = Mocks.TestAssembly();

            var result = ExtensibilityPointFactory.GetXunitTestCollectionFactory(attr, assembly);

            var myFactory = Assert.IsType <MyTestCollectionFactory>(result);

            Assert.Same(assembly, myFactory.Assembly);
        }
Esempio n. 20
0
        public void Attribute_ThrowingDiscovererMethod()
        {
            CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;

            var attribute = Mocks.TestFrameworkAttribute <AttributeWithThrowingDiscovererMethod>();
            var assembly  = Mocks.AssemblyInfo(attributes: new[] { attribute });

            var framework = ExtensibilityPointFactory.GetTestFramework(spy, assembly);

            Assert.IsType <XunitTestFramework>(framework);
            AssertSingleDiagnosticMessage("Exception thrown during test framework discoverer construction: System.DivideByZeroException: Attempted to divide by zero.");
        }
Esempio n. 21
0
        protected override async Task AfterTestCaseStartingAsync()
        {
            await base.AfterTestCaseStartingAsync();

            try
            {
                var dataAttributes = TestCase.TestMethod.Method.GetCustomAttributes(typeof(DataAttribute));

                foreach (var dataAttribute in dataAttributes)
                {
                    var discovererAttribute = dataAttribute.GetCustomAttributes(typeof(DataDiscovererAttribute)).First();
                    var args           = discovererAttribute.GetConstructorArguments().Cast <string>().ToList();
                    var discovererType = SerializationHelper.GetType(args[1], args[0]);
                    var discoverer     = ExtensibilityPointFactory.GetDataDiscoverer(_diagnosticMessageSink, discovererType);

                    foreach (var dataRow in discoverer.GetData(dataAttribute, TestCase.TestMethod.Method))
                    {
                        _toDispose.AddRange(dataRow.OfType <IDisposable>());

                        ITypeInfo[] resolvedTypes = null;
                        var         methodToRun   = TestMethod;

                        if (methodToRun.IsGenericMethodDefinition)
                        {
                            resolvedTypes = TestCase.TestMethod.Method.ResolveGenericTypes(dataRow);
                            methodToRun   = methodToRun.MakeGenericMethod(resolvedTypes.Select(t => ((IReflectionTypeInfo)t).Type).ToArray());
                        }

                        var parameterTypes   = methodToRun.GetParameters().Select(p => p.ParameterType).ToArray();
                        var convertedDataRow = Reflector.ConvertArguments(dataRow, parameterTypes);

                        string theoryDisplayName;
                        if (convertedDataRow.Length == 1 && convertedDataRow[0] is Test.IReadableTestCase)
                        {
                            theoryDisplayName = ((Test.IReadableTestCase)convertedDataRow[0]).TestCase();
                        }
                        else
                        {
                            theoryDisplayName = TestCase.TestMethod.Method.GetDisplayNameWithArguments(DisplayName, convertedDataRow, resolvedTypes);
                        }

                        var test       = new XunitTest(TestCase, theoryDisplayName);
                        var skipReason = SkipReason ?? dataAttribute.GetNamedArgument <string>("Skip");
                        _testRunners.Add(new XunitTestRunner(test, MessageBus, TestClass, ConstructorArguments, methodToRun, convertedDataRow, skipReason, BeforeAfterAttributes, Aggregator, CancellationTokenSource));
                    }
                }
            }
            catch (Exception ex)
            {
                // Stash the exception so we can surface it during RunTestAsync
                _dataDiscoveryException = ex;
            }
        }
        public void NoAttribute()
        {
            var spy = SpyMessageSink.Capture();

            TestContext.Current !.DiagnosticMessageSink = spy;
            var assembly = Mocks.AssemblyInfo();

            var framework = ExtensibilityPointFactory.GetTestFramework(assembly);

            Assert.IsType <XunitTestFramework>(framework);
            Assert.Empty(spy.Messages);
        }
Esempio n. 23
0
        public void IncompatibleOrInvalidTypesGetDefaultBehavior(string factoryTypeName, string expectedMessage)
        {
            var attr     = Mocks.CollectionBehaviorAttribute(factoryTypeName, "xunit.v2.tests");
            var assembly = Mocks.TestAssembly();

            var result = ExtensibilityPointFactory.GetXunitTestCollectionFactory(spy, attr, assembly);

            Assert.IsType <CollectionPerClassTestCollectionFactory>(result);
            Assert.Collection(DiagnosticMessages,
                              msg => Assert.Equal(expectedMessage, msg)
                              );
        }
Esempio n. 24
0
        /// <inheritdoc/>
        protected override async Task AfterTestClassStartingAsync()
        {
            var ordererAttribute = Class.GetCustomAttributes(typeof(TestCaseOrdererAttribute)).SingleOrDefault();

            if (ordererAttribute != null)
            {
                try
                {
                    var testCaseOrderer = ExtensibilityPointFactory.GetTestCaseOrderer(DiagnosticMessageSink, ordererAttribute);
                    if (testCaseOrderer != null)
                    {
                        TestCaseOrderer = testCaseOrderer;
                    }
                    else
                    {
                        var args = ordererAttribute.GetConstructorArguments().Cast <string>().ToList();
                        DiagnosticMessageSink.OnMessage(new DiagnosticMessage($"Could not find type '{args[0]}' in {args[1]} for class-level test case orderer on test class '{TestClass.Class.Name}'"));
                    }
                }
                catch (Exception ex)
                {
                    var innerEx = ex.Unwrap();
                    var args    = ordererAttribute.GetConstructorArguments().Cast <string>().ToList();
                    DiagnosticMessageSink.OnMessage(new DiagnosticMessage($"Class-level test case orderer '{args[0]}' for test class '{TestClass.Class.Name}' threw '{innerEx.GetType().FullName}' during construction: {innerEx.Message}{Environment.NewLine}{innerEx.StackTrace}"));
                }
            }

            var testClassTypeInfo = Class.Type.GetTypeInfo();

            if (testClassTypeInfo.ImplementedInterfaces.Any(i => i.GetTypeInfo().IsGenericType&& i.GetGenericTypeDefinition() == typeof(ICollectionFixture <>)))
            {
                Aggregator.Add(new TestClassException("A test class may not be decorated with ICollectionFixture<> (decorate the test collection class instead)."));
            }

            var createClassFixtureAsyncTasks = new List <Task>();

            foreach (var interfaceType in testClassTypeInfo.ImplementedInterfaces.Where(i => i.GetTypeInfo().IsGenericType&& i.GetGenericTypeDefinition() == typeof(IClassFixture <>)))
            {
                createClassFixtureAsyncTasks.Add(CreateClassFixtureAsync(interfaceType.GetTypeInfo().GenericTypeArguments.Single()));
            }

            if (TestClass.TestCollection.CollectionDefinition != null)
            {
                var declarationType = ((IReflectionTypeInfo)TestClass.TestCollection.CollectionDefinition).Type;
                foreach (var interfaceType in declarationType.GetTypeInfo().ImplementedInterfaces.Where(i => i.GetTypeInfo().IsGenericType&& i.GetGenericTypeDefinition() == typeof(IClassFixture <>)))
                {
                    createClassFixtureAsyncTasks.Add(CreateClassFixtureAsync(interfaceType.GetTypeInfo().GenericTypeArguments.Single()));
                }
            }

            await Task.WhenAll(createClassFixtureAsyncTasks).ConfigureAwait(false);
        }
Esempio n. 25
0
        /// <inheritdoc/>
        protected override async Task AfterTestCaseStartingAsync()
        {
            await base.AfterTestCaseStartingAsync();

            try
            {
                var dataAttributes = this.TestCase.TestMethod.Method.GetCustomAttributes(typeof(DataAttribute)).ToList();
                foreach (var dataAttribute in dataAttributes)
                {
                    var discovererAttribute = dataAttribute.GetCustomAttributes(typeof(DataDiscovererAttribute)).First();
                    // #4 MWP 2020-07-09 05:48:16 PM / also clarifying actualArgs versus dataRows
                    var discoverer = ExtensibilityPointFactory.GetDataDiscoverer(this._diagnosticMessageSink, discovererAttribute);

                    foreach (var actualArgs in discoverer.GetData(dataAttribute, this.TestCase.TestMethod.Method))
                    {
                        this._disposables.AddRange(actualArgs.OfType <IDisposable>());

                        var info                = new ScenarioInfo(this.TestCase.TestMethod.Method, actualArgs, this.DisplayName);
                        var methodToRun         = info.MethodToRun;
                        var convertedActualArgs = info.ConvertedActualArgs.ToArray();

                        var theoryDisplayName = info.ScenarioDisplayName;
                        var test       = new Scenario(this.TestCase, theoryDisplayName);
                        var skipReason = this.SkipReason ?? dataAttribute.GetNamedArgument <string>("Skip");
                        var runner     = new ScenarioRunner(test, this.MessageBus, this.TestClass, this.ConstructorArguments
                                                            , methodToRun, convertedActualArgs, skipReason, this.BeforeAfterAttributes, this.Aggregator
                                                            , this.CancellationTokenSource);
                        this._scenarioRunners.Add(runner);
                    }
                }

                if (!this._scenarioRunners.Any())
                {
                    var info                = new ScenarioInfo(this.TestCase.TestMethod.Method, _noArguments, this.DisplayName);
                    var methodToRun         = info.MethodToRun;
                    var convertedActualArgs = info.ConvertedActualArgs.ToArray();

                    var theoryDisplayName = info.ScenarioDisplayName;
                    var test   = new Scenario(this.TestCase, theoryDisplayName);
                    var runner = new ScenarioRunner(test, this.MessageBus, this.TestClass, this.ConstructorArguments
                                                    , methodToRun, convertedActualArgs, this.SkipReason, this.BeforeAfterAttributes, this.Aggregator
                                                    , this.CancellationTokenSource);
                    this._scenarioRunners.Add(runner);
                }
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
            {
                this._dataDiscoveryException = ex;
            }
        }
Esempio n. 26
0
        public void Attribute_WithDiscoverer_NoMessageSink()
        {
            var attribute      = Mocks.TestFrameworkAttribute <AttributeWithDiscoverer>();
            var assembly       = Mocks.AssemblyInfo(attributes: new[] { attribute });
            var sourceProvider = Substitute.For <_ISourceInformationProvider>();

            var framework = ExtensibilityPointFactory.GetTestFramework(spy, assembly, sourceProvider);

            var testFramework = Assert.IsType <MyTestFramework>(framework);

            Assert.Same(sourceProvider, testFramework.SourceInformationProvider);
            Assert.Empty(messages);
        }
Esempio n. 27
0
        static void DiscoverAllTraits(this ITestCase testCase)
        {
            if (testCase.Traits == null)
            {
                return;
            }

            // the following code is a work around an issue in the extensions that
            // was fixed in https://github.com/dotnet/arcade/pull/5425 but until we get the
            // arcade update, we do the following, check for the discoverer and then add
            // the expected category for the ActiveIssue
            var nullMessageSink = new NullMessageSink();
            var traitAttributes = testCase.TestMethod?.Method?.GetCustomAttributes(typeof(ITraitAttribute));

            if (traitAttributes == null)
            {
                return;
            }
            foreach (var traitAttribute in traitAttributes)
            {
                var discovererAttribute = traitAttribute.GetCustomAttributes(typeof(TraitDiscovererAttribute)).FirstOrDefault();
                if (discovererAttribute == null)
                {
                    continue;
                }

                var discoverer = ExtensibilityPointFactory.GetTraitDiscoverer(nullMessageSink, discovererAttribute);

                if (discoverer == null || !(discoverer is ActiveIssueDiscoverer))
                {
                    continue;
                }

                // add the missing trait
                if (testCase.Traits.ContainsKey(XunitConstants.Category))
                {
                    if (!testCase.Traits[XunitConstants.Category].Contains("failing"))
                    {
                        testCase.Traits[XunitConstants.Category].Add("failing");
                    }
                }
                else
                {
                    testCase.Traits.Add(XunitConstants.Category, new List <string> {
                        "failing"
                    });
                }
            }
        }
        public void Attribute_ThrowingTestFrameworkCtor()
        {
            CultureInfo.CurrentUICulture = CultureInfo.InvariantCulture;

            var spy = SpyMessageSink.Capture();

            TestContext.Current !.DiagnosticMessageSink = spy;
            var attribute = Mocks.TestFrameworkAttribute <AttributeWithThrowingTestFrameworkCtor>();
            var assembly  = Mocks.AssemblyInfo(attributes: new[] { attribute });

            var framework = ExtensibilityPointFactory.GetTestFramework(assembly);

            Assert.IsType <XunitTestFramework>(framework);
            AssertSingleDiagnosticMessage(spy, "Exception thrown during test framework construction; falling back to default test framework: System.DivideByZeroException: Attempted to divide by zero.");
        }
Esempio n. 29
0
        private static IScenarioDiscoverer GetDiscoverer(IMessageSink diagnosticsMessageSink, Type discovererType)
        {
            if (discovererType == null)
            {
                throw new ArgumentNullException(nameof(discovererType));
            }

            var discoverer = ExtensibilityPointFactory.Get <IScenarioDiscoverer>(diagnosticsMessageSink, discovererType);

            if (discoverer == null)
            {
                throw new InvalidOperationException($"Could not create instance for discoverer type {discovererType}");
            }

            return(discoverer);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="XunitTestFrameworkDiscoverer"/> class.
        /// </summary>
        /// <param name="assemblyInfo">The test assembly.</param>
        /// <param name="configFileName">The test configuration file.</param>
        /// <param name="collectionFactory">The test collection factory used to look up test collections.</param>
        public XunitTestFrameworkDiscoverer(
            _IAssemblyInfo assemblyInfo,
            string?configFileName,
            IXunitTestCollectionFactory?collectionFactory = null)
            : base(assemblyInfo)
        {
            var collectionBehaviorAttribute = assemblyInfo.GetCustomAttributes(typeof(CollectionBehaviorAttribute)).SingleOrDefault();
            var disableParallelization      = collectionBehaviorAttribute != null && collectionBehaviorAttribute.GetNamedArgument <bool>("DisableTestParallelization");

            testAssembly          = new TestAssembly(assemblyInfo, configFileName);
            TestCollectionFactory =
                collectionFactory
                ?? ExtensibilityPointFactory.GetXunitTestCollectionFactory(collectionBehaviorAttribute, testAssembly)
                ?? new CollectionPerClassTestCollectionFactory(testAssembly);

            TestFrameworkDisplayName = $"{DisplayName} [{TestCollectionFactory.DisplayName}, {(disableParallelization ? "non-parallel" : "parallel")}]";
        }