public void DeserializedTestCaseContainsSameDataAsOriginalTestCase()
        {
            var testCollection = new XunitTestCollection();
            var type = typeof(ClassUnderTest);
            var method = type.GetMethod("FactMethod");
            var fact = CustomAttributeData.GetCustomAttributes(method).Single(cad => cad.AttributeType == typeof(FactAttribute));
            var testCase = new XunitTestCase(testCollection, Reflector.Wrap(type.Assembly), Reflector.Wrap(type), Reflector.Wrap(method), Reflector.Wrap(fact));
            var serialized = SerializationHelper.Serialize(testCase);

            var result = SerializationHelper.Deserialize<XunitTestCase>(serialized);

            Assert.Equal(testCase.Assembly.AssemblyPath, result.Assembly.AssemblyPath);
            Assert.Equal(testCase.Assembly.Name, result.Assembly.Name);
            Assert.Equal(testCase.Class.Name, result.Class.Name);
            Assert.Equal(testCase.Method.Name, result.Method.Name);
            Assert.Equal(testCase.DisplayName, result.DisplayName);
            Assert.Null(result.Arguments);
            Assert.Equal(testCase.SkipReason, result.SkipReason);
            Assert.Collection(result.Traits.Keys,
                key =>
                {
                    Assert.Equal("name", key);
                    Assert.Equal("value", Assert.Single(result.Traits[key]));
                });
        }
Beispiel #2
0
        /// <inheritdoc/>
        public IEnumerable <IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo factAttribute)
        {
            var defaultMethodDisplay = discoveryOptions.MethodDisplayOrDefault();

            // Special case Skip, because we want a single Skip (not one per data item), and a skipped test may
            // not actually have any data (which is quasi-legal, since it's skipped).
            if (factAttribute.GetNamedArgument <string>("Skip") != null)
            {
                return new[] { new XunitTestCase(defaultMethodDisplay, testMethod) }
            }
            ;

            var dataAttributes = testMethod.Method.GetCustomAttributes(typeof(DataAttribute));

            if (discoveryOptions.PreEnumerateTheoriesOrDefault())
            {
                try
                {
                    var results = new List <XunitTestCase>();

                    foreach (var dataAttribute in dataAttributes)
                    {
                        var discovererAttribute = dataAttribute.GetCustomAttributes(typeof(DataDiscovererAttribute)).First();
                        var discoverer          = ExtensibilityPointFactory.GetDataDiscoverer(discovererAttribute);
                        if (!discoverer.SupportsDiscoveryEnumeration(dataAttribute, testMethod.Method))
                        {
                            return new XunitTestCase[] { new XunitTheoryTestCase(defaultMethodDisplay, testMethod) }
                        }
                        ;

                        // GetData may return null, but that's okay; we'll let the NullRef happen and then catch it
                        // down below so that we get the composite test case.
                        foreach (var dataRow in discoverer.GetData(dataAttribute, testMethod.Method))
                        {
                            // Attempt to serialize the test case, since we need a way to uniquely identify a test
                            // and serialization is the best way to do that. If it's not serializable, this will
                            // throw and we will fall back to a single theory test case that gets its data
                            // at runtime.
                            var testCase = new XunitTestCase(defaultMethodDisplay, testMethod, dataRow);

                            SerializationHelper.Serialize(testCase);
                            results.Add(testCase);
                        }
                    }

                    // REVIEW: Could we re-write LambdaTestCase to just be for exceptions?
                    if (results.Count == 0)
                    {
                        results.Add(new LambdaTestCase(defaultMethodDisplay, testMethod,
                                                       () => { throw new InvalidOperationException(String.Format("No data found for {0}.{1}", testMethod.TestClass.Class.Name, testMethod.Method.Name)); }));
                    }

                    return(results);
                }
                catch { }
            }

            return(new XunitTestCase[] { new XunitTheoryTestCase(defaultMethodDisplay, testMethod) });
        }
    }
        private KeyValuePair <string, ITestCase> Deserialize(XunitTestFrameworkDiscoverer discoverer,
                                                             ITestFrameworkExecutor executor,
                                                             string serialization)
        {
            var testCase = default(ITestCase);

            try
            {
                if (serialization.Length > 3 && serialization.StartsWith(":F:"))
                {
                    // Format from TestCaseDescriptorFactory: ":F:{typeName}:{methodName}:{defaultMethodDisplay}"
                    var parts = serialization.Split(new[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
                    if (parts.Length > 3)
                    {
                        var typeInfo             = discoverer.AssemblyInfo.GetType(parts[1]);
                        var testClass            = discoverer.CreateTestClass(typeInfo);
                        var methodInfo           = testClass.Class.GetMethod(parts[2], true);
                        var testMethod           = new TestMethod(testClass, methodInfo);
                        var defaultMethodDisplay = (TestMethodDisplay)int.Parse(parts[3]);
                        testCase = new XunitTestCase(DiagnosticMessageSink, defaultMethodDisplay, testMethod);
                    }
                }

                if (testCase == null)
                {
                    testCase = executor.Deserialize(serialization);
                }

                return(new KeyValuePair <string, ITestCase>(testCase.UniqueID, testCase));
            }
            catch (Exception ex)
            {
                return(new KeyValuePair <string, ITestCase>(ex.ToString(), null));
            }
        }
    public static void SkipReason()
    {
        var testMethod = Mocks.TestMethod("MockType", "MockMethod", skip: "Skip Reason");

        var testCase = new XunitTestCase(testMethod);

        Assert.Equal("Skip Reason", testCase.SkipReason);
    }
    public static void SkipReason()
    {
        var testMethod = Mocks.TestMethod("MockType", "MockMethod", skip: "Skip Reason");

        var testCase = new XunitTestCase(SpyMessageSink.Create(), TestMethodDisplay.ClassAndMethod, TestMethodDisplayOptions.None, testMethod);

        Assert.Equal("Skip Reason", testCase.SkipReason);
    }
        public void CanSerializeFactBasedTestCase()
        {
            var type = typeof(ClassUnderTest);
            var method = type.GetMethod("FactMethod");
            var fact = CustomAttributeData.GetCustomAttributes(method).Single(cad => cad.AttributeType == typeof(FactAttribute));
            var testCase = new XunitTestCase(Reflector.Wrap(type.Assembly), Reflector.Wrap(type), Reflector.Wrap(method), Reflector.Wrap(fact));

            Assert.DoesNotThrow(() => SerializationHelper.Serialize(testCase));
        }
        private ITestCase UpdateTestCaseWithSourceInfo(XunitTestCase testCase, bool includeSourceInformation)
        {
            if (includeSourceInformation && sourceProvider != null)
            {
                testCase.SourceInformation = sourceProvider.GetSourceInformation(testCase);
            }

            return(testCase);
        }
    public static void DefaultBehavior()
    {
        var testMethod = Mocks.TestMethod("MockType", "MockMethod");

        var testCase = new XunitTestCase(testMethod);

        Assert.Equal("MockType.MockMethod", testCase.DisplayName);
        Assert.Null(testCase.SkipReason);
        Assert.Empty(testCase.Traits);
    }
    public static void DefaultBehavior()
    {
        var testMethod = Mocks.TestMethod("MockType", "MockMethod");

        var testCase = new XunitTestCase(SpyMessageSink.Create(), TestMethodDisplay.ClassAndMethod, TestMethodDisplayOptions.None, testMethod);

        Assert.Equal("MockType.MockMethod", testCase.DisplayName);
        Assert.Null(testCase.SkipReason);
        Assert.Empty(testCase.Traits);
    }
Beispiel #10
0
    public void SkipReason()
    {
        var fact = Mocks.FactAttribute(skip: "Skip Reason");
        var method = Mocks.MethodInfo();
        var type = Mocks.TypeInfo(methods: new[] { method });
        var assmInfo = Mocks.AssemblyInfo(types: new[] { type });

        var testCase = new XunitTestCase(assmInfo, type, method, fact);

        Assert.Equal("Skip Reason", testCase.SkipReason);
    }
        public static void TraitsOnTestClass()
        {
            var trait1 = Mocks.TraitAttribute("Trait1", "Value1");
            var trait2 = Mocks.TraitAttribute("Trait2", "Value2");
            var testMethod = Mocks.TestMethod(classAttributes: new[] { trait1, trait2 });

            var testCase = new XunitTestCase(SpyMessageSink.Create(), TestMethodDisplay.ClassAndMethod, TestMethodDisplayOptions.None, testMethod);

            Assert.Equal("Value1", Assert.Single(testCase.Traits["Trait1"]));
            Assert.Equal("Value2", Assert.Single(testCase.Traits["Trait2"]));
        }
Beispiel #12
0
        public static void TraitsOnTestClass()
        {
            var trait1 = Mocks.TraitAttribute("Trait1", "Value1");
            var trait2 = Mocks.TraitAttribute("Trait2", "Value2");
            var testMethod = Mocks.TestMethod(classAttributes: new[] { trait1, trait2 });

            var testCase = new XunitTestCase(testMethod);

            Assert.Equal("Value1", Assert.Single(testCase.Traits["Trait1"]));
            Assert.Equal("Value2", Assert.Single(testCase.Traits["Trait2"]));
        }
Beispiel #13
0
        public void DeserializedTestWithNonSerializableArgumentsThrows()
        {
            var type = typeof(ClassUnderTest);
            var method = type.GetMethod("FactMethod");
            var fact = CustomAttributeData.GetCustomAttributes(method).Single(cad => cad.AttributeType == typeof(FactAttribute));
            var testCase = new XunitTestCase(Reflector.Wrap(type.Assembly), Reflector.Wrap(type), Reflector.Wrap(method), Reflector.Wrap(fact), new object[] { new ClassUnderTest() });

            var ex = Record.Exception(() => SerializationHelper.Serialize(testCase));

            Assert.IsType<SerializationException>(ex);
        }
Beispiel #14
0
    public static void DisposesArguments()
    {
        var disposable1 = Substitute.For<IDisposable>();
        var disposable2 = Substitute.For<IDisposable>();
        var testMethod = Mocks.TestMethod();
        var testCase = new XunitTestCase(testMethod, new[] { disposable1, disposable2 });

        testCase.Dispose();

        disposable1.Received(1).Dispose();
        disposable2.Received(1).Dispose();
    }
Beispiel #15
0
        /// <inheritdoc/>
        public IEnumerable<IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo factAttribute)
        {
            var methodDisplay = discoveryOptions.MethodDisplayOrDefault();

            IXunitTestCase testCase;
            if (testMethod.Method.GetParameters().Any())
                testCase = new ExecutionErrorTestCase(diagnosticMessageSink, methodDisplay, testMethod, "[Fact] methods are not allowed to have parameters. Did you mean to use [Theory]?");
            else
                testCase = new XunitTestCase(diagnosticMessageSink, methodDisplay, testMethod);

            return new[] { testCase };
        }
Beispiel #16
0
        /// <inheritdoc/>
        public IEnumerable <IXunitTestCase> Discover(ITestCollection testCollection, IAssemblyInfo assembly, ITypeInfo testClass, IMethodInfo testMethod, IAttributeInfo factAttribute)
        {
            // Special case Skip, because we want a single Skip (not one per data item), and a skipped test may
            // not actually have any data (which is quasi-legal, since it's skipped).
            if (factAttribute.GetNamedArgument <string>("Skip") != null)
            {
                return new[] { new XunitTestCase(testCollection, assembly, testClass, testMethod, factAttribute) }
            }
            ;

            try
            {
                using (var memoryStream = new MemoryStream())
                {
                    var results = new List <XunitTestCase>();

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

                        // GetData may return null, but that's okay; we'll let the NullRef happen and then catch it
                        // down below so that we get the composite test case.
                        foreach (object[] dataRow in discoverer.GetData(dataAttribute, testMethod))
                        {
                            // Attempt to serialize the test case, since we need a way to uniquely identify a test
                            // and serialization is the best way to do that. If it's not serializable, this will
                            // throw and we will fall back to a single theory test case that gets its data
                            // at runtime.
                            var testCase = new XunitTestCase(testCollection, assembly, testClass, testMethod, factAttribute, dataRow);
                            SerializationHelper.Serialize(testCase);
                            results.Add(testCase);
                        }
                    }

                    // REVIEW: Could we re-write LambdaTestCase to just be for exceptions?
                    if (results.Count == 0)
                    {
                        results.Add(new LambdaTestCase(testCollection, assembly, testClass, testMethod, factAttribute, () => { throw new InvalidOperationException("No data found for " + testClass.Name + "." + testMethod.Name); }));
                    }

                    return(results);
                }
            }
            catch
            {
                return(new XunitTestCase[] { new XunitTheoryTestCase(testCollection, assembly, testClass, testMethod, factAttribute) });
            }
        }
    }
Beispiel #17
0
    public void DefaultFactAttribute()
    {
        var fact = Mocks.FactAttribute();
        var method = Mocks.MethodInfo();
        var type = Mocks.TypeInfo(methods: new[] { method });
        var assmInfo = Mocks.AssemblyInfo(types: new[] { type });

        var testCase = new XunitTestCase(assmInfo, type, method, fact);

        Assert.Equal("MockType.MockMethod", testCase.DisplayName);
        Assert.Null(testCase.SkipReason);
        Assert.Empty(testCase.Traits);
    }
Beispiel #18
0
    public void Traits()
    {
        var fact = Mocks.FactAttribute();
        var trait1 = Mocks.TraitAttribute("Trait1", "Value1");
        var trait2 = Mocks.TraitAttribute("Trait2", "Value2");
        var method = Mocks.MethodInfo(attributes: new[] { trait1, trait2 });
        var type = Mocks.TypeInfo(methods: new[] { method });
        var assmInfo = Mocks.AssemblyInfo(types: new[] { type });

        var testCase = new XunitTestCase(assmInfo, type, method, fact);

        Assert.Equal("Value1", testCase.Traits["Trait1"]);
        Assert.Equal("Value2", testCase.Traits["Trait2"]);
    }
Beispiel #19
0
        /// <inheritdoc/>
        public IEnumerable<IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo factAttribute)
        {
            var defaultMethodDisplay = discoveryOptions.MethodDisplayOrDefault();

            // Special case Skip, because we want a single Skip (not one per data item), and a skipped test may
            // not actually have any data (which is quasi-legal, since it's skipped).
            if (factAttribute.GetNamedArgument<string>("Skip") != null)
                return new[] { new XunitTestCase(diagnosticMessageSink, defaultMethodDisplay, testMethod) };

            var dataAttributes = testMethod.Method.GetCustomAttributes(typeof(DataAttribute));

            if (discoveryOptions.PreEnumerateTheoriesOrDefault())
            {
                try
                {
                    var results = new List<XunitTestCase>();

                    foreach (var dataAttribute in dataAttributes)
                    {
                        var discovererAttribute = dataAttribute.GetCustomAttributes(typeof(DataDiscovererAttribute)).First();
                        var discoverer = ExtensibilityPointFactory.GetDataDiscoverer(diagnosticMessageSink, discovererAttribute);
                        if (!discoverer.SupportsDiscoveryEnumeration(dataAttribute, testMethod.Method))
                            return new XunitTestCase[] { new XunitTheoryTestCase(diagnosticMessageSink, defaultMethodDisplay, testMethod) };

                        // GetData may return null, but that's okay; we'll let the NullRef happen and then catch it
                        // down below so that we get the composite test case.
                        foreach (var dataRow in discoverer.GetData(dataAttribute, testMethod.Method))
                        {
                            // Attempt to serialize the test case, since we need a way to uniquely identify a test
                            // and serialization is the best way to do that. If it's not serializable, this will
                            // throw and we will fall back to a single theory test case that gets its data
                            // at runtime.
                            var testCase = new XunitTestCase(diagnosticMessageSink, defaultMethodDisplay, testMethod, dataRow);
                            SerializationHelper.Serialize(testCase);
                            results.Add(testCase);
                        }
                    }

                    if (results.Count == 0)
                        results.Add(new ExecutionErrorTestCase(diagnosticMessageSink, defaultMethodDisplay, testMethod,
                                                               String.Format("No data found for {0}.{1}", testMethod.TestClass.Class.Name, testMethod.Method.Name)));

                    return results;
                }
                catch { }  // If there are serialization issues with the theory data, fall through to return just the XunitTestCase
            }

            return new XunitTestCase[] { new XunitTheoryTestCase(diagnosticMessageSink, defaultMethodDisplay, testMethod) };
        }
Beispiel #20
0
        /// <inheritdoc/>
        public IEnumerable<IXunitTestCase> Discover(ITestMethod testMethod, IAttributeInfo factAttribute)
        {
            // Special case Skip, because we want a single Skip (not one per data item), and a skipped test may
            // not actually have any data (which is quasi-legal, since it's skipped).
            if (factAttribute.GetNamedArgument<string>("Skip") != null)
                return new[] { new XunitTestCase(testMethod) };

            var dataAttributes = testMethod.Method.GetCustomAttributes(typeof(DataAttribute));

            try
            {
                using (var memoryStream = new MemoryStream())
                {
                    var results = new List<XunitTestCase>();

                    foreach (var dataAttribute in dataAttributes)
                    {
                        var discovererAttribute = dataAttribute.GetCustomAttributes(typeof(DataDiscovererAttribute)).First();
                        var discoverer = ExtensibilityPointFactory.GetDataDiscoverer(discovererAttribute);
                        if (!discoverer.SupportsDiscoveryEnumeration(dataAttribute, testMethod.Method))
                            return new XunitTestCase[] { new XunitTheoryTestCase(testMethod) };

                        // GetData may return null, but that's okay; we'll let the NullRef happen and then catch it
                        // down below so that we get the composite test case.
                        foreach (var dataRow in discoverer.GetData(dataAttribute, testMethod.Method))
                        {
                            // Attempt to serialize the test case, since we need a way to uniquely identify a test
                            // and serialization is the best way to do that. If it's not serializable, this will
                            // throw and we will fall back to a single theory test case that gets its data
                            // at runtime.
                            var testCase = new XunitTestCase(testMethod, dataRow);
                            SerializationHelper.Serialize(testCase);
                            results.Add(testCase);
                        }
                    }

                    // REVIEW: Could we re-write LambdaTestCase to just be for exceptions?
                    if (results.Count == 0)
                        results.Add(new LambdaTestCase(testMethod,
                                                       () => { throw new InvalidOperationException(String.Format("No data found for {0}.{1}", testMethod.TestClass.Class.Name, testMethod.Method.Name)); }));

                    return results;
                }
            }
            catch
            {
                return new XunitTestCase[] { new XunitTheoryTestCase(testMethod) };
            }
        }
Beispiel #21
0
        public void DeserializedTestWithSerializableArgumentsPreservesArguments()
        {
            var type = typeof(ClassUnderTest);
            var method = type.GetMethod("FactMethod");
            var fact = CustomAttributeData.GetCustomAttributes(method).Single(cad => cad.AttributeType == typeof(FactAttribute));
            var testCase = new XunitTestCase(Reflector.Wrap(type.Assembly), Reflector.Wrap(type), Reflector.Wrap(method), Reflector.Wrap(fact), new object[] { 42, 21.12, "Hello world" });
            var serialized = SerializationHelper.Serialize(testCase);

            var result = SerializationHelper.Deserialize<XunitTestCase>(serialized);

            Assert.Collection(result.Arguments,
                arg => Assert.Equal(42, arg),
                arg => Assert.Equal(21.12, arg),
                arg => Assert.Equal("Hello world", arg));
        }
Beispiel #22
0
        /// <summary>
        /// Creates test cases for a skipped theory. By default, returns a single instance of <see cref="XunitTestCase"/>
        /// (which inherently discovers the skip reason via the fact attribute).
        /// </summary>
        /// <param name="discoveryOptions">The discovery options to be used.</param>
        /// <param name="testMethod">The test method the test cases belong to.</param>
        /// <param name="theoryAttribute">The theory attribute attached to the test method.</param>
        /// <param name="skipReason">The skip reason that decorates <paramref name="theoryAttribute"/>.</param>
        /// <returns>The test cases</returns>
        protected virtual ValueTask <IReadOnlyCollection <IXunitTestCase> > CreateTestCasesForSkippedTheory(
            _ITestFrameworkDiscoveryOptions discoveryOptions,
            _ITestMethod testMethod,
            _IAttributeInfo theoryAttribute,
            string skipReason)
        {
            // TODO: Skip reason should be passed down into the test case
            var testCase = new XunitTestCase(
                discoveryOptions.MethodDisplayOrDefault(),
                discoveryOptions.MethodDisplayOptionsOrDefault(),
                testMethod
                );

            return(new(new[] { testCase }));
        }
    public void DisposesArguments()
    {
        var disposable1 = Substitute.For<IDisposable>();
        var disposable2 = Substitute.For<IDisposable>();
        var testCollection = new XunitTestCollection();
        var fact = Mocks.FactAttribute();
        var method = Mocks.MethodInfo();
        var type = Mocks.TypeInfo(methods: new[] { method });
        var assmInfo = Mocks.AssemblyInfo(types: new[] { type });
        var testCase = new XunitTestCase(testCollection, assmInfo, type, method, fact, new[] { disposable1, disposable2 });

        testCase.Dispose();

        disposable1.Received(1).Dispose();
        disposable2.Received(1).Dispose();
    }
Beispiel #24
0
        /// <summary>
        /// Creates test cases for a skipped theory. By default, returns a single instance of <see cref="XunitTestCase"/>
        /// (which inherently discovers the skip reason via the fact attribute).
        /// </summary>
        /// <param name="discoveryOptions">The discovery options to be used.</param>
        /// <param name="testMethod">The test method the test cases belong to.</param>
        /// <param name="theoryAttribute">The theory attribute attached to the test method.</param>
        /// <param name="skipReason">The skip reason that decorates <paramref name="theoryAttribute"/>.</param>
        /// <returns>The test cases</returns>
        protected virtual IEnumerable <IXunitTestCase> CreateTestCasesForSkip(
            _ITestFrameworkDiscoveryOptions discoveryOptions,
            _ITestMethod testMethod,
            _IAttributeInfo theoryAttribute,
            string skipReason)
        {
            // TODO: Skip reason should be passed down into the test case
            var testCase = new XunitTestCase(
                DiagnosticMessageSink,
                discoveryOptions.MethodDisplayOrDefault(),
                discoveryOptions.MethodDisplayOptionsOrDefault(),
                testMethod
                );

            return(new[] { testCase });
        }
Beispiel #25
0
        /// <inheritdoc/>
        public IEnumerable<XunitTestCase> Discover(ITestCollection testCollection, IAssemblyInfo assembly, ITypeInfo testClass, IMethodInfo testMethod, IAttributeInfo factAttribute)
        {
            // Special case Skip, because we want a single Skip (not one per data item), and a skipped test may
            // not actually have any data (which is quasi-legal, since it's skipped).
            if (factAttribute.GetNamedArgument<string>("Skip") != null)
                return new[] { new XunitTestCase(testCollection, assembly, testClass, testMethod, factAttribute) };

            try
            {
                using (var memoryStream = new MemoryStream())
                {
                    List<XunitTestCase> results = new List<XunitTestCase>();

                    var dataAttributes = testMethod.GetCustomAttributes(typeof(DataAttribute));
                    foreach (var dataAttribute in dataAttributes)
                    {
                        var discovererAttribute = dataAttribute.GetCustomAttributes(typeof(DataDiscovererAttribute)).First();
                        var args = discovererAttribute.GetConstructorArguments().Cast<string>().ToList();
                        var discovererType = Reflector.GetType(args[1], args[0]);
                        IDataDiscoverer discoverer = (IDataDiscoverer)Activator.CreateInstance(discovererType);

                        // GetData may return null, but that's okay; we'll let the NullRef happen and then catch it
                        // down below so that we get the composite test case.
                        foreach (object[] dataRow in discoverer.GetData(dataAttribute, testMethod))
                        {
                            // Attempt to serialize the test case, since we need a way to uniquely identify a test
                            // and serialization is the best way to do that. If it's not serializable, this will
                            // throw and we will fall back to a single theory test case that gets its data
                            // at runtime.
                            var testCase = new XunitTestCase(testCollection, assembly, testClass, testMethod, factAttribute, dataRow);
                            SerializationHelper.Serialize(testCase);
                            results.Add(testCase);
                        }
                    }

                    // REVIEW: Could we re-write LambdaTestCase to just be for exceptions?
                    if (results.Count == 0)
                        results.Add(new LambdaTestCase(testCollection, assembly, testClass, testMethod, factAttribute, () => { throw new InvalidOperationException("No data found for " + testClass.Name + "." + testMethod.Name); }));

                    return results;
                }
            }
            catch
            {
                return new XunitTestCase[] { new XunitTheoryTestCase(testCollection, assembly, testClass, testMethod, factAttribute) };
            }
        }
Beispiel #26
0
        /// <inheritdoc/>
        public IEnumerable <IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo factAttribute)
        {
            var methodDisplay = discoveryOptions.MethodDisplayOrDefault();

            IXunitTestCase testCase;

            if (testMethod.Method.GetParameters().Any())
            {
                testCase = new LambdaTestCase(methodDisplay, testMethod, () => { throw new InvalidOperationException("[Fact] methods are not allowed to have parameters. Did you mean to use [Theory]?"); });
            }
            else
            {
                testCase = new XunitTestCase(methodDisplay, testMethod);
            }

            return(new[] { testCase });
        }
Beispiel #27
0
        /// <inheritdoc/>
        public IEnumerable <IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo factAttribute)
        {
            var methodDisplay = discoveryOptions.MethodDisplayOrDefault();

            IXunitTestCase testCase;

            if (testMethod.Method.GetParameters().Any())
            {
                testCase = new ExecutionErrorTestCase(diagnosticMessageSink, methodDisplay, testMethod, "[Fact] methods are not allowed to have parameters. Did you mean to use [Theory]?");
            }
            else
            {
                testCase = new XunitTestCase(diagnosticMessageSink, methodDisplay, testMethod);
            }

            return(new[] { testCase });
        }
        private IEnumerable<IXunitTestCase> discoverTestCases(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod)
        {
            if (!discoveryOptions.PreEnumerateTheoriesOrDefault())
                return new XunitTestCase[] { new XunitTheoryTestCase(_messageSink, TestMethodDisplay.Method, testMethod) };

            var withValuesAttributes = testMethod.Method.GetCustomAttributes(typeof(WithValuesAttribute).AssemblyQualifiedName);
            var testCase = new List<XunitTestCase>();

            foreach (var withValuesAttribute in withValuesAttributes)
            {
                var discovererAttribute = withValuesAttribute.GetCustomAttributes(typeof(DataDiscovererAttribute).AssemblyQualifiedName).Single();
                var discoverer = ExtensibilityPointFactory.GetDataDiscoverer(_messageSink, discovererAttribute);
                if (!discoverer.SupportsDiscoveryEnumeration(withValuesAttribute, testMethod.Method))
                    return new XunitTestCase[] { new XunitTheoryTestCase(_messageSink, TestMethodDisplay.Method, testMethod) };

                foreach (var data in discoverer.GetData(withValuesAttribute, testMethod.Method))
                {
                    var test = new XunitTestCase(_messageSink, TestMethodDisplay.Method, testMethod, data);
                    testCase.Add(test);
                }
            }

            return testCase;
        }
        public static void CustomDisplayName()
        {
            var testMethod = Mocks.TestMethod(displayName: "Custom Display Name");

            var testCase = new XunitTestCase(SpyMessageSink.Create(), TestMethodDisplay.ClassAndMethod, TestMethodDisplayOptions.None, testMethod);

            Assert.Equal("Custom Display Name", testCase.DisplayName);
        }
Beispiel #30
0
        public static void TooManyTestArguments()
        {
            var param = Mocks.ParameterInfo("p1");
            var testMethod = Mocks.TestMethod(parameters: new[] { param });
            var arguments = new object[] { 42, 21.12M };

            var testCase = new XunitTestCase(testMethod, arguments);

            Assert.Equal(String.Format("MockType.MockMethod(p1: 42, ???: {0})", 21.12), testCase.DisplayName);
        }
Beispiel #31
0
        /// <inheritdoc/>
        public IEnumerable <IXunitTestCase> Discover(ITestFrameworkDiscoveryOptions discoveryOptions, ITestMethod testMethod, IAttributeInfo factAttribute)
        {
            var defaultMethodDisplay = discoveryOptions.MethodDisplayOrDefault();

            // Special case Skip, because we want a single Skip (not one per data item), and a skipped test may
            // not actually have any data (which is quasi-legal, since it's skipped).
            if (factAttribute.GetNamedArgument <string>("Skip") != null)
            {
                return new[] { new XunitTestCase(diagnosticMessageSink, defaultMethodDisplay, testMethod) }
            }
            ;

            var dataAttributes = testMethod.Method.GetCustomAttributes(typeof(DataAttribute));

            if (discoveryOptions.PreEnumerateTheoriesOrDefault())
            {
                try
                {
                    var results = new List <XunitTestCase>();

                    foreach (var dataAttribute in dataAttributes)
                    {
                        var discovererAttribute = dataAttribute.GetCustomAttributes(typeof(DataDiscovererAttribute)).First();
                        var discoverer          = ExtensibilityPointFactory.GetDataDiscoverer(diagnosticMessageSink, discovererAttribute);
                        if (!discoverer.SupportsDiscoveryEnumeration(dataAttribute, testMethod.Method))
                        {
                            return new XunitTestCase[] { new XunitTheoryTestCase(diagnosticMessageSink, defaultMethodDisplay, testMethod) }
                        }
                        ;

                        // GetData may return null, but that's okay; we'll let the NullRef happen and then catch it
                        // down below so that we get the composite test case.
                        foreach (var dataRow in discoverer.GetData(dataAttribute, testMethod.Method))
                        {
                            // Determine whether we can serialize the test case, since we need a way to uniquely
                            // identify a test and serialization is the best way to do that. If it's not serializable,
                            // this will throw and we will fall back to a single theory test case that gets its data at runtime.
                            var testCase = new XunitTestCase(diagnosticMessageSink, defaultMethodDisplay, testMethod, dataRow);

                            if (!SerializationHelper.IsSerializable(dataRow))
                            {
                                return new XunitTestCase[] { new XunitTheoryTestCase(diagnosticMessageSink, defaultMethodDisplay, testMethod) }
                            }
                            ;
                            results.Add(testCase);
                        }
                    }

                    if (results.Count == 0)
                    {
                        results.Add(new ExecutionErrorTestCase(diagnosticMessageSink, defaultMethodDisplay, testMethod,
                                                               string.Format("No data found for {0}.{1}", testMethod.TestClass.Class.Name, testMethod.Method.Name)));
                    }

                    return(results);
                }
                catch { }  // If something goes wrong, fall through to return just the XunitTestCase
            }

            return(new XunitTestCase[] { new XunitTheoryTestCase(diagnosticMessageSink, defaultMethodDisplay, testMethod) });
        }
    }
 /// <summary>
 /// Converts a <see cref="XunitTestCase"/> into an equivalent test case for the discoverer.
 /// </summary>
 /// <param name="testCase">The test case to convert.</param>
 /// <param name="defaultMethodDisplay">The default test method display.</param>
 /// <param name="diagnosticMessageSink">The message sink used to send diagnostic messages.</param>
 /// <param name="timeout">The amount of time the test case is allowed to run in milliseconds, or a value less than 1 to indicate no timeout.</param>
 /// <param name="useStaThread">Whether to use an STA thread to execute tests.</param>
 /// <returns>The converted test case (the default implementation returns a <see cref="TimeoutXunitTestCase"/>).</returns>
 protected virtual XunitTestCase ConvertXunitTestCase(XunitTestCase testCase, TestMethodDisplay defaultMethodDisplay, IMessageSink diagnosticMessageSink, int timeout, bool useStaThread)
 {
     return(new TimeoutXunitTestCase(diagnosticMessageSink, defaultMethodDisplay, testCase.TestMethod, testCase.TestMethodArguments, timeout, useStaThread));
 }
Beispiel #33
0
        public void TooManyTestArguments()
        {
            var fact = Mocks.FactAttribute();
            var param = Mocks.ParameterInfo("p1");
            var method = Mocks.MethodInfo(parameters: new[] { param });
            var type = Mocks.TypeInfo(methods: new[] { method });
            var assmInfo = Mocks.AssemblyInfo(types: new[] { type });
            var arguments = new object[] { 42, 21.12 };

            var testCase = new XunitTestCase(assmInfo, type, method, fact, arguments);

            Assert.Equal("MockType.MockMethod(p1: 42, ???: 21.12)", testCase.DisplayName);
        }
        public static void CustomDisplayNameWithArguments()
        {
            var param1 = Mocks.ParameterInfo("p1");
            var param2 = Mocks.ParameterInfo("p2");
            var param3 = Mocks.ParameterInfo("p3");
            var testMethod = Mocks.TestMethod(displayName: "Custom Display Name", parameters: new[] { param1, param2, param3 });
            var arguments = new object[] { 42, "Hello, world!", 'A' };

            var testCase = new XunitTestCase(SpyMessageSink.Create(), TestMethodDisplay.ClassAndMethod, TestMethodDisplayOptions.None, testMethod, arguments);

            Assert.Equal("Custom Display Name(p1: 42, p2: \"Hello, world!\", p3: 'A')", testCase.DisplayName);
        }
        private ITestCase UpdateTestCaseWithSourceInfo(XunitTestCase testCase, bool includeSourceInformation)
        {
            if (includeSourceInformation && sourceProvider != null)
                testCase.SourceInformation = sourceProvider.GetSourceInformation(testCase);

            return testCase;
        }
        public void NotEnoughTestArguments()
        {
            var testCollection = new XunitTestCollection();
            var fact = Mocks.FactAttribute();
            var param = Mocks.ParameterInfo("p1");
            var method = Mocks.MethodInfo(parameters: new[] { param });
            var type = Mocks.TypeInfo(methods: new[] { method });
            var assmInfo = Mocks.AssemblyInfo(types: new[] { type });

            var testCase = new XunitTestCase(testCollection, assmInfo, type, method, fact, arguments: new object[0]);

            Assert.Equal("MockType.MockMethod(p1: ???)", testCase.DisplayName);
        }
Beispiel #37
0
        public static void CustomDisplayName()
        {
            var testMethod = Mocks.TestMethod(displayName: "Custom Display Name");

            var testCase = new XunitTestCase(testMethod);

            Assert.Equal("Custom Display Name", testCase.DisplayName);
        }
Beispiel #38
0
        public void CustomDisplayName()
        {
            var fact = Mocks.FactAttribute(displayName: "Custom Display Name");
            var method = Mocks.MethodInfo();
            var type = Mocks.TypeInfo(methods: new[] { method });
            var assmInfo = Mocks.AssemblyInfo(types: new[] { type });

            var testCase = new XunitTestCase(assmInfo, type, method, fact);

            Assert.Equal("Custom Display Name", testCase.DisplayName);
        }
        public static void CustomTraitWithoutDiscoverer()
        {
            var trait = Mocks.TraitAttribute<BadTraitAttribute>();
            var testMethod = Mocks.TestMethod(classAttributes: new[] { trait });
            var messages = new List<IMessageSinkMessage>();
            var spy = SpyMessageSink.Create(messages: messages);

            var testCase = new XunitTestCase(spy, TestMethodDisplay.ClassAndMethod, TestMethodDisplayOptions.None, testMethod);

            Assert.Empty(testCase.Traits);
            var diagnosticMessages = messages.OfType<IDiagnosticMessage>();
            var diagnosticMessage = Assert.Single(diagnosticMessages);
            Assert.Equal("Trait attribute on 'MockType.MockMethod' did not have [TraitDiscoverer]", diagnosticMessage.Message);
        }
Beispiel #40
0
        public void CorrectNumberOfTestArguments()
        {
            var fact = Mocks.FactAttribute();
            var param1 = Mocks.ParameterInfo("p1");
            var param2 = Mocks.ParameterInfo("p2");
            var param3 = Mocks.ParameterInfo("p3");
            var method = Mocks.MethodInfo(parameters: new[] { param1, param2, param3 });
            var type = Mocks.TypeInfo(methods: new[] { method });
            var assmInfo = Mocks.AssemblyInfo(types: new[] { type });
            var arguments = new object[] { 42, "Hello, world!", 'A' };

            var testCase = new XunitTestCase(assmInfo, type, method, fact, arguments);

            Assert.Equal("MockType.MockMethod(p1: 42, p2: \"Hello, world!\", p3: 'A')", testCase.DisplayName);
        }