public void TheoryViaDataAcceptanceTest()
    {
        string code = @"
                using Xunit.Extensions;

                public class Stub
                {
                    [Theory]
                    [InlineData(1, ""hello"", 2.3)]
                    [InlineData(42, ""world"", 21.12)]
                    public void PassingTestData(int foo, string bar, double baz)
                    {
                    }
                }
            ";

        XmlNode assemblyNode = Execute(code, null, "xunit.extensions.dll");

        XmlNode testNode1 = ResultXmlUtility.GetResult(assemblyNode, 0, 0);

        ResultXmlUtility.AssertAttribute(testNode1, "result", "Pass");
        ResultXmlUtility.AssertAttribute(testNode1, "name", "Stub." + @"PassingTestData(foo: 1, bar: ""hello"", baz: 2.3)");

        XmlNode testNode2 = ResultXmlUtility.GetResult(assemblyNode, 0, 1);

        ResultXmlUtility.AssertAttribute(testNode2, "result", "Pass");
        ResultXmlUtility.AssertAttribute(testNode2, "name", "Stub." + @"PassingTestData(foo: 42, bar: ""world"", baz: 21.12)");
    }
Esempio n. 2
0
    public void OutputIsPresentEvenIfTestFails()
    {
        const string expected =
            @"Line 1 to Standard Output
";

        string code =
            @"
                using System;
                using Xunit;

                public class TestClass
                {
                    [Fact]
                    public void SampleTest()
                    {
                        Console.WriteLine(""Line 1 to Standard Output"");
                        Assert.False(true);
                    }
                }
            ";

        XmlNode assemblyNode = Execute(code);

        XmlNode node = ResultXmlUtility.GetResult(assemblyNode);

        Assert.Equal(expected, node.SelectSingleNode("output").InnerText);
    }
Esempio n. 3
0
        public void AcceptanceTest()
        {
            string code = @"
                    using Xunit;

                    public class TestClass
                    {
                        [Fact] public void TestMethod1() {}
                        [Fact] public void TestMethod2() {}
                        [Fact] public void TestMethod3() {}
                    }";

            using (MockAssembly assembly = new MockAssembly())
            {
                assembly.Compile(code);

                XmlNode lastNode    = null;
                XmlNode returnValue = null;

                using (ExecutorWrapper wrapper = new ExecutorWrapper(assembly.FileName, null, false))
                    returnValue = wrapper.RunTests("TestClass",
                                                   new List <string> {
                        "TestMethod1", "TestMethod2"
                    },
                                                   node => { lastNode = node; return(true); });

                Assert.Equal(returnValue, lastNode);
                Assert.Equal(2, lastNode.ChildNodes.Count); // Two test results
                XmlNode result0 = ResultXmlUtility.GetResult(lastNode, 0);
                Assert.Equal("Pass", result0.Attributes["result"].Value);
                XmlNode result1 = ResultXmlUtility.GetResult(lastNode, 1);
                Assert.Equal("Pass", result1.Attributes["result"].Value);
            }
        }
Esempio n. 4
0
        public void NonPublicTestMethod()
        {
            string code = @"
                    using Xunit;

                    public class TestClass
                    {
                        [Fact] void NonPublicTestMethod() {}
                    }";

            using (MockAssembly assembly = new MockAssembly())
            {
                assembly.Compile(code);

                XmlNode returnValue = null;

                using (ExecutorWrapper wrapper = new ExecutorWrapper(assembly.FileName, null, false))
                    returnValue = wrapper.RunTests("TestClass",
                                                   new List <string> {
                        "NonPublicTestMethod"
                    },
                                                   node => { return(true); });

                Assert.Single(returnValue.ChildNodes);
                XmlNode result = ResultXmlUtility.GetResult(returnValue, 0);
                Assert.Equal("Pass", result.Attributes["result"].Value);
            }
        }
Esempio n. 5
0
        public void TestMethodWithNonTestMethod()
        {
            string code = @"
                    using Xunit;

                    public class TestClass
                    {
                        [Fact] public void TestMethod1() {}
                        [Fact] public void TestMethod2() {}
                        [Fact] public void TestMethod3() {}
                        public void NonTestMethod() {}
                    }";

            using (MockAssembly assembly = new MockAssembly())
            {
                assembly.Compile(code);

                XmlNode lastNode = null;

                using (ExecutorWrapper wrapper = new ExecutorWrapper(assembly.FileName, null, false))
                    wrapper.RunTests("TestClass",
                                     new List <string> {
                        "TestMethod1", "NonTestMethod"
                    },
                                     node => { lastNode = node; return(true); });

                Assert.Single(lastNode.ChildNodes); // Only the test method
                XmlNode result = ResultXmlUtility.GetResult(lastNode, 0);
                Assert.Equal("Pass", result.Attributes["result"].Value);
            }
        }
    public void TheoryWithNoDataAttributes()
    {
        string code = @"
                using Xunit.Extensions;

                public class Stub
                {
                    [Theory]
                    public void TheoryMethod(int x)
                    {
                    }
                }
            ";

        XmlNode assemblyNode = Execute(code, null, "xunit.extensions.dll");

        XmlNode testNode = ResultXmlUtility.GetResult(assemblyNode, 0, 0);

        ResultXmlUtility.AssertAttribute(testNode, "result", "Fail");
        ResultXmlUtility.AssertAttribute(testNode, "name", "Stub.TheoryMethod");
        XmlNode failureNode = testNode.SelectSingleNode("failure");

        ResultXmlUtility.AssertAttribute(failureNode, "exception-type", typeof(InvalidOperationException).FullName);
        XmlNode messageNode = failureNode.SelectSingleNode("message");

        Assert.Equal("System.InvalidOperationException : No data found for Stub.TheoryMethod", messageNode.InnerText);
    }
Esempio n. 7
0
    public void StdOutput()
    {
        const string expected =
            @"Line 1 to Standard Output
Line 2 to Standard Error
Line 3 to Standard Output
";

        string code =
            @"
                using System;
                using Xunit;

                public class TestClass
                {
                    [Fact]
                    public void SampleTest()
                    {
                        Console.WriteLine(""Line 1 to Standard Output"");
                        Console.Error.WriteLine(""Line 2 to Standard Error"");
                        Console.WriteLine(""Line 3 to Standard Output"");
                    }
                }
            ";

        XmlNode assemblyNode = Execute(code);

        XmlNode node = ResultXmlUtility.GetResult(assemblyNode);

        Assert.Equal(expected, node.SelectSingleNode("output").InnerText);
    }
Esempio n. 8
0
        public void AcceptanceTest()
        {
            string code = @"
                    using Xunit;

                    public class TestClass
                    {
                        [Fact]
                        public void TestMethod()
                        {
                        }
                    }";

            using (MockAssembly assembly = new MockAssembly())
            {
                assembly.Compile(code);

                XmlNode lastNode    = null;
                XmlNode returnValue = null;

                using (ExecutorWrapper wrapper = new ExecutorWrapper(assembly.FileName, null, false))
                    returnValue = wrapper.RunTest("TestClass", "TestMethod", node => { lastNode = node; return(true); });

                XmlNode resultNode = ResultXmlUtility.GetResult(lastNode);
                Assert.Equal("Pass", resultNode.Attributes["result"].Value);
                Assert.Equal(returnValue, lastNode);
            }
        }
Esempio n. 9
0
        public void ConfigurationExceptionShouldBeThrown()
        {
            string code =
                @"
                    using System;
                    using System.Configuration;
                    using Xunit;

                    public class MockTestClass
                    {
                        [Fact]
                        public void TestMethod()
                        {
                        }
                    }
                ";

            XmlNode assemblyNode;

            using (MockAssembly mockAssembly = new MockAssembly(assemblyFileName))
            {
                mockAssembly.Compile(code, null);
                assemblyNode = mockAssembly.Run(configFile);
            }

            var resultNode  = ResultXmlUtility.GetResult(assemblyNode);
            var failureNode = resultNode.SelectSingleNode("failure");

            Assert.NotNull(failureNode);
            Assert.Equal("System.Configuration.ConfigurationErrorsException", failureNode.Attributes["exception-type"].Value);
        }
    public void NamedFailingTest()
    {
        string code = codeHeader + @"
                public class ExampleSpecUsage
                {
                    [Spec(""Failing specification"")]
                    public void Failing()
                    {
                        Assert.True(false);
                    }
                }
            ";

        XmlNode assemblyNode = Execute(code);

        XmlNode resultNode = ResultXmlUtility.GetResult(assemblyNode);

        ResultXmlUtility.AssertAttribute(resultNode, "result", "Fail");
        ResultXmlUtility.AssertAttribute(resultNode, "name", "Failing specification");
    }
    public void NamedSkippedTest()
    {
        string code = codeHeader + @"
                public class ExampleSpecUsage
                {
                    [Spec(""Skipped specification"", Skip=""Failing, not sure why..."")]
                    public void Skippy()
                    {
                        Assert.False(true);
                    }
                }
            ";

        XmlNode assemblyNode = Execute(code);

        XmlNode resultNode = ResultXmlUtility.GetResult(assemblyNode);

        ResultXmlUtility.AssertAttribute(resultNode, "result", "Skip");
        ResultXmlUtility.AssertAttribute(resultNode, "name", "Skipped specification");
    }
    public void ThrowingDataAttributeAcceptanceTest()
    {
        string code = @"
            using System;
            using System.Collections.Generic;
            using System.Reflection;
            using Xunit;
            using Xunit.Extensions;

            public class MisbehavingTestClass
            {
                [Theory, MisbehavedData]
                public void TheoryWithMisbehavingData(string a)
                {
                    Assert.True(true);
                }
            }

            public class MisbehavedDataAttribute : DataAttribute
            {
                public override IEnumerable<object[]> GetData(MethodInfo method, Type[] paramTypes)
                {
                    throw new Exception();
                }
            }
        ";

        XmlNode assemblyNode = Execute(code, null, "xunit.extensions.dll");

        XmlNode testNode = ResultXmlUtility.GetResult(assemblyNode, 0, 0);

        ResultXmlUtility.AssertAttribute(testNode, "result", "Fail");
        ResultXmlUtility.AssertAttribute(testNode, "name", "MisbehavingTestClass.TheoryWithMisbehavingData");
        XmlNode failureNode = testNode.SelectSingleNode("failure");

        ResultXmlUtility.AssertAttribute(failureNode, "exception-type", typeof(InvalidOperationException).FullName);
        XmlNode messageNode = failureNode.SelectSingleNode("message");

        Assert.Contains("System.InvalidOperationException : An exception was thrown while getting data for theory MisbehavingTestClass.TheoryWithMisbehavingData:", messageNode.InnerText);
    }
Esempio n. 13
0
    public void UsesSingleInstanceOfFixtureDataForAllTests()
    {
        string code =
            @"
                using System;
                using System.Diagnostics;
                using Xunit;

                public class TestFixtureTest : IUseFixture<object>
                {
                    public static object fixtureData = null;

                    public void SetFixture(object data)
                    {
                        if (fixtureData == null)
                            fixtureData = data;
                        else
                            Assert.Same(fixtureData, data);
                    }

                    [Fact]
                    public void Test1()
                    {
                    }

                    [Fact]
                    public void Test2()
                    {
                    }
                }";

        XmlNode assemblyNode = Execute(code);

        XmlNode result0 = ResultXmlUtility.GetResult(assemblyNode, 0);

        ResultXmlUtility.AssertAttribute(result0, "result", "Pass");
        XmlNode result1 = ResultXmlUtility.GetResult(assemblyNode, 1);

        ResultXmlUtility.AssertAttribute(result1, "result", "Pass");
    }
    public void TheoryWithDataAttributesWithNoData()
    {
        string code = @"
            using System;
            using System.Collections.Generic;
            using System.Reflection;
            using Xunit.Extensions;

            public class EmptyTheoryData : DataAttribute
            {
                public override IEnumerable<object[]> GetData(MethodInfo methodUnderTest, Type[] parameterTypes)
                {
                    return new object[0][];
                }
            }

            public class Stub
            {
                [Theory]
                [EmptyTheoryData]
                public void TheoryMethod(int x)
                {
                }
            }
        ";

        XmlNode assemblyNode = Execute(code, null, "xunit.extensions.dll");

        XmlNode testNode = ResultXmlUtility.GetResult(assemblyNode, 0, 0);

        ResultXmlUtility.AssertAttribute(testNode, "result", "Fail");
        ResultXmlUtility.AssertAttribute(testNode, "name", "Stub.TheoryMethod");
        XmlNode failureNode = testNode.SelectSingleNode("failure");

        ResultXmlUtility.AssertAttribute(failureNode, "exception-type", typeof(InvalidOperationException).FullName);
        XmlNode messageNode = failureNode.SelectSingleNode("message");

        Assert.Equal("System.InvalidOperationException : No data found for Stub.TheoryMethod", messageNode.InnerText);
    }
    public void TheoryViaXlsAcceptanceTest()
    {
        if (IntPtr.Size == 8)  // Test always fails in 64-bit; no JET engine
        {
            return;
        }

        string code = @"
                using System.Data;
                using Xunit.Extensions;

                public class Stub
                {
                    [Theory, ExcelData(@""DataTheories\AcceptanceTestData.xls"", ""select * from Data"")]
                    public void PassingTestData(int? foo, string bar, string baz)
                    {
                    }
                }
            ";


        XmlNode assemblyNode = Execute(code, null, "xunit.extensions.dll");

        XmlNode testNode1 = ResultXmlUtility.GetResult(assemblyNode, 0, 0);

        ResultXmlUtility.AssertAttribute(testNode1, "result", "Pass");
        ResultXmlUtility.AssertAttribute(testNode1, "name", "Stub." + @"PassingTestData(foo: 1, bar: ""Foo"", baz: ""Bar"")");

        XmlNode testNode2 = ResultXmlUtility.GetResult(assemblyNode, 0, 1);

        ResultXmlUtility.AssertAttribute(testNode2, "result", "Pass");
        ResultXmlUtility.AssertAttribute(testNode2, "name", "Stub." + @"PassingTestData(foo: null, bar: null, baz: null)");

        XmlNode testNode3 = ResultXmlUtility.GetResult(assemblyNode, 0, 2);

        ResultXmlUtility.AssertAttribute(testNode3, "result", "Pass");
        ResultXmlUtility.AssertAttribute(testNode3, "name", "Stub." + @"PassingTestData(foo: 14, bar: ""Biff"", baz: ""Baz"")");
    }
Esempio n. 16
0
        public void TestsFromAbstractBaseClassesShouldBeExecuted()
        {
            string code = @"
                using Xunit;

                public abstract class TestBase
                {
                    [Fact]
                    public void BaseTestMethod() {}
                }

                public class DerivedTestClass1 : TestBase
                {
                    [Fact]
                    public void TestMethod1() {}
                }

                public class DerivedTestClass2 : TestBase
                {
                    [Fact]
                    public void TestMethod2() {}
                }";

            XmlNode assemblyNode = Execute(code);

            XmlNode result00 = ResultXmlUtility.GetResult(assemblyNode, 0, 0);

            ResultXmlUtility.AssertAttribute(result00, "result", "Pass");
            XmlNode result01 = ResultXmlUtility.GetResult(assemblyNode, 0, 1);

            ResultXmlUtility.AssertAttribute(result01, "result", "Pass");
            XmlNode result10 = ResultXmlUtility.GetResult(assemblyNode, 1, 0);

            ResultXmlUtility.AssertAttribute(result10, "result", "Pass");
            XmlNode result11 = ResultXmlUtility.GetResult(assemblyNode, 1, 1);

            ResultXmlUtility.AssertAttribute(result11, "result", "Pass");
        }
    public void ClassWithFixtureAndSkippedFactDoesNotSetFixtureData()
    {
        string code = @"
                using Xunit;

                public class MyFacts : IUseFixture<object>
                {
                    public void SetFixture(object data)
                    {
                        Assert.True(false);
                    }

                    [Fact(Skip=""Skip Me!"")]
                    public void SkippedTest() {}
                }
            ";

        XmlNode assemblyNode = Execute(code);

        XmlNode result = ResultXmlUtility.GetResult(assemblyNode);

        ResultXmlUtility.AssertAttribute(result, "result", "Skip");
    }
    public void IncorrectParameterCountThrows()
    {
        string code = @"
                using Xunit.Extensions;

                public class Stub
                {
                    [Theory]
                    [InlineData(1)]
                    [InlineData(2, 3)]
                    [InlineData(4)]
                    public void PassingTestData(int x)
                    {
                    }
                }
            ";

        XmlNode assemblyNode = Execute(code, null, "xunit.extensions.dll");

        XmlNode testNode1 = ResultXmlUtility.GetResult(assemblyNode, 0, 0);

        ResultXmlUtility.AssertAttribute(testNode1, "result", "Pass");
        ResultXmlUtility.AssertAttribute(testNode1, "name", "Stub." + @"PassingTestData(x: 1)");

        XmlNode testNode2 = ResultXmlUtility.GetResult(assemblyNode, 0, 1);

        ResultXmlUtility.AssertAttribute(testNode2, "result", "Fail");
        ResultXmlUtility.AssertAttribute(testNode2, "name", "Stub." + @"PassingTestData(x: 2, ???: 3)");
        XmlNode failureNode = testNode2.SelectSingleNode("failure");

        ResultXmlUtility.AssertAttribute(failureNode, "exception-type", typeof(InvalidOperationException).FullName);

        XmlNode testNode3 = ResultXmlUtility.GetResult(assemblyNode, 0, 2);

        ResultXmlUtility.AssertAttribute(testNode3, "result", "Pass");
        ResultXmlUtility.AssertAttribute(testNode3, "name", "Stub." + @"PassingTestData(x: 4)");
    }
    public void TheoryWithInlineDataOfSingleNullPassesNullToTestMethod()
    {
        string code = @"
                using Xunit;
                using Xunit.Extensions;

                public class Stub
                {
                    [Theory]
                    [InlineData(null)]
                    public void PassingTestData(string foo)
                    {
                        Assert.Null(foo);
                    }
                }
            ";

        XmlNode assemblyNode = Execute(code, null, "xunit.extensions.dll");

        XmlNode testNode = ResultXmlUtility.GetResult(assemblyNode, 0, 0);

        ResultXmlUtility.AssertAttribute(testNode, "result", "Pass");
        ResultXmlUtility.AssertAttribute(testNode, "name", "Stub.PassingTestData(foo: null)");
    }
Esempio n. 20
0
    public void SpecificationSampleAcceptanceTest()
    {
        string code = @"
            using System;
            using System.Collections.Generic;
            using System.Linq;
            using Xunit;
            using Xunit.Sdk;

            public class SpecificationBaseRunner : ITestClassCommand
            {
                SpecificationBase objectUnderTest;

                public SpecificationBase ObjectUnderTest
                {
                    get
                    {
                        if (objectUnderTest == null)
                        {
                            GuardTypeUnderTest();
                            objectUnderTest = (SpecificationBase)Activator.CreateInstance(TypeUnderTest.Type);
                        }

                        return objectUnderTest;
                    }
                }

                object ITestClassCommand.ObjectUnderTest
                {
                    get { return ObjectUnderTest; }
                }

                public ITypeInfo TypeUnderTest { get; set; }

                public int ChooseNextTest(ICollection<IMethodInfo> testsLeftToRun)
                {
                    return 0;
                }

                public Exception ClassFinish()
                {
                    try
                    {
                        ObjectUnderTest.OnFinish();
                        return null;
                    }
                    catch (Exception ex)
                    {
                        return ex;
                    }
                }

                public Exception ClassStart()
                {
                    try
                    {
                        ObjectUnderTest.OnStart();
                        return null;
                    }
                    catch (Exception ex)
                    {
                        return ex;
                    }
                }

                public IEnumerable<ITestCommand> EnumerateTestCommands(IMethodInfo testMethod)
                {
                    string displayName = (TypeUnderTest.Type.Name + "", it "" + testMethod.Name).Replace('_', ' ');
                    return new[] { new SpecificationBaseCommand(testMethod, displayName) };
                }

                public IEnumerable<IMethodInfo> EnumerateTestMethods()
                {
                    GuardTypeUnderTest();

                    return TypeUnderTest.GetMethods().Where(IsTestMethod);
                }

                void GuardTypeUnderTest()
                {
                    if (TypeUnderTest == null)
                        throw new InvalidOperationException(""Forgot to set TypeUnderTest before calling ObjectUnderTest"");

                    if (!typeof(SpecificationBase).IsAssignableFrom(TypeUnderTest.Type))
                        throw new InvalidOperationException(""SpecificationBaseRunner can only be used with types that derive from SpecificationBase"");
                }

                public bool IsTestMethod(IMethodInfo testMethod)
                {
                    return testMethod.HasAttribute(typeof(ObservationAttribute));
                }
            }

            [RunWith(typeof(SpecificationBaseRunner))]
            public abstract class SpecificationBase
            {
                protected virtual void Because() { }

                protected virtual void DestroyContext() { }

                protected virtual void EstablishContext() { }

                internal void OnFinish()
                {
                    DestroyContext();
                }

                internal void OnStart()
                {
                    EstablishContext();
                    Because();
                }
            }

            public class SpecificationBaseCommand : TestCommand
            {
                public SpecificationBaseCommand(IMethodInfo method, string displayName)
                    : base(method, displayName, 0) { }

                public override MethodResult Execute(object testClass)
                {
                    testMethod.Invoke(testClass, null);
                    return new PassedResult(testMethod, DisplayName);
                }
            }

            [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
            public class ObservationAttribute : Attribute { }

            public class When_the_specification_runs : SpecificationBase
            {
                static int establishRunCount = 0;
                static int becauseRunCount = 0;
                static int destroyRunCount = 0;

                protected override void EstablishContext()
                {
                    ++establishRunCount;
                    Assert.Equal(1, establishRunCount);
                }

                protected override void Because()
                {
                    ++becauseRunCount;
                    Assert.Equal(1, establishRunCount);
                    Assert.Equal(1, becauseRunCount);
                }

                protected override void DestroyContext()
                {
                    ++destroyRunCount;
                    Assert.Equal(1, establishRunCount);
                    Assert.Equal(1, becauseRunCount);
                    Assert.Equal(1, destroyRunCount);
                }

                [Observation]
                public void should_ensure_run_counts_are_correct()
                {
                    Assert.Equal(1, establishRunCount);
                    Assert.Equal(1, becauseRunCount);
                    Assert.Equal(0, destroyRunCount);
                }

                [Observation]
                public void should_fail()
                {
                    Assert.True(false);
                }
            }";

        XmlNode assemblyNode = Execute(code);

        XmlNode result0 = ResultXmlUtility.GetResult(assemblyNode, 0);

        Assert.Equal("Pass", result0.Attributes["result"].Value);
        Assert.Equal("When the specification runs, it should ensure run counts are correct", result0.Attributes["name"].Value);
        XmlNode result1 = ResultXmlUtility.GetResult(assemblyNode, 1);

        Assert.Equal("Fail", result1.Attributes["result"].Value);
        Assert.Equal("When the specification runs, it should fail", result1.Attributes["name"].Value);
    }