Esempio n. 1
0
        public void ValueFromUserSpecifiedConfigFile()
        {
            string code =
                @"
                    using System;
                    using System.Configuration;
                    using Xunit;

                    public class MockTestClass
                    {
                        [Fact]
                        public void CheckConfigurationFileEntry()
                        {
                            Assert.Equal(ConfigurationSettings.AppSettings[""ConfigurationValue""], ""42"");
                        }
                    }
                ";

            XmlNode assemblyNode;

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

            ResultXmlUtility.AssertResult(assemblyNode, "Pass", "MockTestClass.CheckConfigurationFileEntry");
            Assert.Equal(Path.GetFullPath(configFile), assemblyNode.Attributes["configFile"].Value);
        }
Esempio n. 2
0
    public void TestClassIsNotInstantiatedForSkippedTests()
    {
        string code =
            @"
                using System;
                using Xunit;

                public class MockTestClass
                {
                    public MockTestClass()
                    {
                        throw new Exception(""Should not reach me!"");
                    }

                    [Fact(Skip = ""the reason"")]
                    public void TestThatShouldBeSkipped()
                    {
                    }
                }
            ";

        XmlNode assemblyNode = Execute(code);

        XmlNode testNode    = ResultXmlUtility.AssertResult(assemblyNode, "Skip", "MockTestClass.TestThatShouldBeSkipped");
        XmlNode messageNode = testNode.SelectSingleNode("reason/message");

        Assert.Equal("the reason", messageNode.InnerText);
    }
    public void TheoryViaClassAcceptanceTest()
    {
        string code = @"
                using System.Collections;
                using System.Collections.Generic;
                using Xunit.Extensions;

                public class StubData : IEnumerable<object[]>
                {
                    public IEnumerator<object[]> GetEnumerator()
                    {
                        yield return new object[] { 1, ""hello world"", 2.3 };
                    }

                    IEnumerator IEnumerable.GetEnumerator()
                    {
                        return GetEnumerator();
                    }
                }

                public class Stub
                {
                    [Theory, ClassData(typeof(StubData))]
                    public void PassingTestData(int foo, string bar, double baz)
                    {
                    }
                }
            ";

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

        ResultXmlUtility.AssertResult(assemblyNode, "Pass", "Stub." + @"PassingTestData(foo: 1, bar: ""hello world"", baz: 2.3)");
    }
Esempio n. 4
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);
        }
Esempio n. 5
0
        public void CallbackIncludesStartMessages()
        {
            const 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);

                List <XmlNode> nodes = new List <XmlNode>();

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

                Assert.Equal(3, nodes.Count);
                Assert.Equal("start", nodes[0].Name);  // <start>
                ResultXmlUtility.AssertAttribute(nodes[0], "name", "TestClass.TestMethod1");
                ResultXmlUtility.AssertAttribute(nodes[0], "type", "TestClass");
                ResultXmlUtility.AssertAttribute(nodes[0], "method", "TestMethod1");
                Assert.Equal("test", nodes[1].Name);
                Assert.Equal("class", nodes[2].Name);
            }
        }
Esempio n. 6
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. 7
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);
            }
        }
Esempio n. 8
0
    public void VerifiesConstructorIsCalled()
    {
        string code =
            @"
                using System;
                using Xunit;

                public class MockTestClass
                {
                    int counter; 

                    public MockTestClass()
                    {
                        counter++;
                    }

                    [Fact]
                    public void CounterShouldBeIncrementedInConstructor()
                    {
                        Assert.Equal(counter, 1);
                    }
                }
            ";

        XmlNode assemblyNode = Execute(code);

        ResultXmlUtility.AssertResult(assemblyNode, "Pass", "MockTestClass.CounterShouldBeIncrementedInConstructor");
    }
Esempio n. 9
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);
    }
    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)");
    }
    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. 12
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. 13
0
    public void TestHasTimeoutAndExceeds()
    {
        string code =
            @"
                using System;
                using System.Threading;
                using Xunit;

                public class Stub
                {
                    [Fact(Timeout = 50)]
                    public void TestShouldTimeout()
                    {
                        Thread.Sleep(120); 
                        Assert.Equal(2, 2);
                    }
                }
            ";

        XmlNode assemblyNode = Execute(code);

        XmlNode testNode    = ResultXmlUtility.AssertResult(assemblyNode, "Fail", "Stub.TestShouldTimeout");
        XmlNode messageNode = testNode.SelectSingleNode("failure/message");

        Assert.Equal("Test execution time exceeded: 50ms", messageNode.InnerText);
    }
    public void Async40AcceptanceTest()
    {
        string code = @"
            using System;
            using System.Threading;
            using System.Threading.Tasks;
            using Xunit;

            public class TestClass
            {
                [Fact]
                public Task TestMethod()
                {
                    return Task.Factory.StartNew(() =>
                    {
                        Thread.Sleep(1);
                    })
                    .ContinueWith(_ =>
                    {
                        Assert.True(false);
                    });
                }
            }
        ";

        XmlNode assemblyNode = Execute(code);

        XmlNode testNode    = ResultXmlUtility.AssertResult(assemblyNode, "Fail", "TestClass.TestMethod");
        XmlNode failureNode = testNode.SelectSingleNode("failure");

        ResultXmlUtility.AssertAttribute(failureNode, "exception-type", typeof(TrueException).FullName);
    }
Esempio n. 15
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. 16
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. 17
0
    public void TraceAssertDoesNotCreateAnyOutput()
    {
        string code =
            @"
                #define TRACE

                using System;
                using System.Diagnostics;
                using Xunit;

                public class TestClass
                {
                    [Fact]
                    public void SampleTest()
                    {
                        Trace.Assert(false);
                    }
                }
            ";

        XmlNode assemblyNode = Execute(code);

        XmlNode node = ResultXmlUtility.AssertResult(assemblyNode, "Fail", "TestClass.SampleTest");

        Assert.Null(node.SelectSingleNode("output"));
    }
 public void AssemblyFilenameInXmlMatchesOriginallyPassedNameToExecutor()
 {
     using (MockAssembly mockAssembly = new MockAssembly())
     {
         mockAssembly.Compile("");
         XmlNode assemblyNode = mockAssembly.Run(null);
         ResultXmlUtility.AssertAttribute(assemblyNode, "name", mockAssembly.FileName);
     }
 }
Esempio n. 19
0
    public void TestCommandReturnsStartXml_WithDisplayName()
    {
        MethodInfo         method  = typeof(TestMethodCommandClass).GetMethod("TestMethod");
        Mock <TestCommand> command = new Mock <TestCommand>(Reflector.Wrap(method), "Display Name!", 0);

        command.CallBase = true;

        XmlNode result = command.Object.ToStartXml();

        Assert.Equal("start", result.Name);
        ResultXmlUtility.AssertAttribute(result, "name", "Display Name!");
        ResultXmlUtility.AssertAttribute(result, "type", typeof(TestMethodCommandClass).FullName);
        ResultXmlUtility.AssertAttribute(result, "method", "TestMethod");
    }
    public void LackOfConfigurationFileBugInCLR4()  // http://xunit.codeplex.com/workitem/9696
    {
        string code = @"
            using System;
            using Xunit;

            public class ConfigurationFileExample
            {
                [Fact]
                public void Test()
                {
                    new Uri(""http://localhost:58080/indexes/categoriesByName?query=CategoryName%3ABeverages&start=0&pageSize=25"");
                }
            }";

        XmlNode assemblyNode = Execute(code);

        ResultXmlUtility.AssertResult(assemblyNode, "Pass", "ConfigurationFileExample.Test");
    }
Esempio n. 21
0
    public void ClassWithStaticTest()
    {
        string code =
            @"
                using System;
                using Xunit;

                public class StaticTests
                {
                    [Fact]
                    public static void StaticTestMethod()
                    {
                    }
                }";

        XmlNode assemblyNode = Execute(code);

        ResultXmlUtility.AssertResult(assemblyNode, "Pass", "StaticTests.StaticTestMethod");
    }
    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");
    }
Esempio n. 23
0
    public void FactMethodsCannotHaveArguments()
    {
        string code = @"
            using System;
            using Xunit;

            public class MockTestClass
            {
                [Fact] public void FactWithParameters(int x) { }
            }
        ";

        XmlNode assemblyNode = Execute(code);

        XmlNode testNode    = ResultXmlUtility.AssertResult(assemblyNode, "Fail", "MockTestClass.FactWithParameters");
        XmlNode failureNode = testNode.SelectSingleNode("failure");

        ResultXmlUtility.AssertAttribute(failureNode, "exception-type", typeof(InvalidOperationException).FullName);
        Assert.Equal("System.InvalidOperationException : Fact method MockTestClass.FactWithParameters cannot have parameters", failureNode.SelectSingleNode("message").InnerText);
    }
    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 ThrowsExpectedException()
    {
        string code =
            @"
                using System;
                using Xunit;

                public class MockTestClass
                {
                    [Fact]
                    public void ExpectTest()
                    {
                        Assert.Throws<InvalidOperationException>(delegate { throw new InvalidOperationException(); });
                    }
                }
            ";

        XmlNode assemblyNode = Execute(code);

        ResultXmlUtility.AssertResult(assemblyNode, "Pass", "MockTestClass.ExpectTest");
    }
    public void AssertAreEqualTwoNumbersEqualShouldBePassedResult()
    {
        string code =
            @"
                using System;
                using Xunit;

                public class MockTestClass
                {
                    [Fact]
                    public void SuccessTest()
                    {
                        Assert.Equal(2, 2);
                    }
                }
            ";

        XmlNode assemblyNode = Execute(code);

        ResultXmlUtility.AssertResult(assemblyNode, "Pass", "MockTestClass.SuccessTest");
    }
    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. 28
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);
    }
Esempio n. 30
0
        public void ValueFromUserSpecifiedConfigFile()
        {
            string code =
                @"
                    using System;
                    using System.Configuration;
                    using Xunit;

                    public class MockTestClass
                    {
                        [Fact]
                        public void CheckConfigurationFileEntry()
                        {
                            Assert.Equal(ConfigurationSettings.AppSettings[""ConfigurationValue""], ""42"");
                        }
                    }
                ";

            XmlNode assemblyNode = Execute(code, configFile);

            ResultXmlUtility.AssertResult(assemblyNode, "Pass", "MockTestClass.CheckConfigurationFileEntry");
        }