예제 #1
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);
    }
예제 #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 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);
    }
예제 #4
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);
        }
    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)");
    }
예제 #6
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");
    }
예제 #7
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"));
    }
예제 #8
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 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");
    }
예제 #10
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 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 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");
    }
예제 #13
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");
        }
예제 #14
0
        public void ShouldNotBeExcutingInTheSameAppDomain()
        {
            string codeTemplate =
                @"
                    using System;
                    using System.Diagnostics;
                    using Xunit;

                    public class AppDomainTest
                    {{
                        [Fact]
                        public void TestDomainName()
                        {{
                            Assert.False({0} == AppDomain.CurrentDomain.Id);
                        }}
                    }}";

            string code = String.Format(codeTemplate, AppDomain.CurrentDomain.Id);

            XmlNode assemblyNode = Execute(code);

            ResultXmlUtility.AssertResult(assemblyNode, "Pass", "AppDomainTest.TestDomainName");
        }
예제 #15
0
    public void AssertAreEqualTwoNumbersNotEqualShouldThrowException()
    {
        string code = @"
            using System;
            using Xunit;

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

        XmlNode assemblyNode = Execute(code);

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

        ResultXmlUtility.AssertAttribute(failureNode, "exception-type", typeof(EqualException).FullName);
    }
    public void CurrentDirectoryWhenRunningTestsIsTestAssemblyPath()
    {
        string code = @"
            using System.IO;
            using Xunit;

            public class ChangeDirectoryTests
            {
                [Fact]
                public void ChangeDirectory()
                {
                    string tempPath = Path.GetFullPath(Path.GetTempPath()).TrimEnd('\\');
                    string currentPath = Path.GetFullPath(Directory.GetCurrentDirectory()).TrimEnd('\\');
                    Assert.Equal(tempPath, currentPath);
                }
            }
        ";

        string  assemblyName = Path.Combine(Path.GetTempPath(), Path.GetTempFileName());
        XmlNode assemblyNode = ExecuteWithCustomAssemblyName(code, assemblyName);

        ResultXmlUtility.AssertResult(assemblyNode, "Pass", "ChangeDirectoryTests.ChangeDirectory");
    }
예제 #17
0
    public void TimingForFailedTestShouldReflectActualRunTime()
    {
        string code = @"
            using System;
            using Xunit;

            public class MockTestClass
            {
                [Fact]
                public void TwoNumbersAreNotEqual()
                {
                    System.Threading.Thread.Sleep(100);
                    Assert.Equal(2, 3);
                }
            }
        ";

        XmlNode assemblyNode = Execute(code);

        XmlNode testNode = ResultXmlUtility.AssertResult(assemblyNode, "Fail", "MockTestClass.TwoNumbersAreNotEqual");

        Assert.NotEqual("0.000", testNode.Attributes["time"].Value);
    }
    public void TheoryViaPropertyAcceptanceTest()
    {
        string code = @"
                using System.Collections.Generic;
                using Xunit.Extensions;

                public class Stub
                {
                    public static IEnumerable<object[]> MyTestData
                    {
                        get { yield return new object[] { 1, ""hello world"", 2.3 }; }
                    }

                    [Theory, PropertyData(""MyTestData"")]
                    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)");
    }
예제 #19
0
    public void TestIsMarkedSkippedAndDoesNotExecute()
    {
        string code =
            @"
                using System;
                using Xunit;

                public class MockTestClass
                {
                    [Fact(Skip = ""the reason"")]
                    public void FailedTestThatShouldBeSkipped()
                    {
                        Assert.Equal(2, 3);
                    }
                }
            ";

        XmlNode assemblyNode = Execute(code);

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

        Assert.Equal("the reason", messageNode.InnerText);
    }
예제 #20
0
    public void UsesCorrectExternalRunner()
    {
        string code = @"
            using System;
            using System.Collections.Generic;
            using System.Reflection;
            using Xunit;
            using Xunit.Sdk;

            namespace Test.AcceptanceTest.ExternalRunner
            {
                public class StubExternalRunner : ITestClassCommand
                {
                    ITypeInfo typeUnderTest;

                    public object ObjectUnderTest
                    {
                        get { return null; }
                    }

                    public ITypeInfo TypeUnderTest
                    {
                        get { return typeUnderTest; }
                        set { typeUnderTest = value; }
                    }

                    public Exception ClassFinish() { return null; }

                    public Exception ClassStart() { return null; }

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

                    public IEnumerable<ITestCommand> EnumerateTestCommands(IMethodInfo testMethod)
                    {
                        yield return new StubRunnerTestCommand(testMethod);
                    }

                    public IEnumerable<IMethodInfo> EnumerateTestMethods()
                    {
                        yield return Reflector.Wrap(typeof(ClassUnderTest).GetMethod(""Method""));
                    }

                    public bool IsTestMethod(IMethodInfo testMethod)
                    {
                        return testMethod.Name == ""Method"";
                    }
                }

                public class StubRunnerTestCommand : TestCommand
                {
                    public StubRunnerTestCommand(IMethodInfo method)
                        : base(method, null, 0) { }

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

                [RunWith(typeof(StubExternalRunner))]
                public class ClassUnderTest
                {
                    public void Method() {}
                }
            }
        ";

        XmlNode assemblyNode = Execute(code);

        ResultXmlUtility.AssertResult(assemblyNode, "Pass", "Test.AcceptanceTest.ExternalRunner.ClassUnderTest.Method");
    }