コード例 #1
0
        public XmlNode ExecuteWithCustomAssemblyName(string code, string assemblyName)
        {
            using (MockAssembly mockAssembly = new MockAssembly(assemblyName))
            {
                string fullAssemblyName = Path.GetFullPath(assemblyName);
                string assemblyPath = Path.GetDirectoryName(fullAssemblyName);
                string xunitCopyFilename = Path.Combine(assemblyPath, "xunit.dll");
                File.Copy(mockAssembly.XunitDllFilename, xunitCopyFilename, true);

                try
                {
                    mockAssembly.Compile(code, null);
                    return mockAssembly.Run(null);
                }
                finally
                {
                    try
                    {
                        if (xunitCopyFilename != null && File.Exists(xunitCopyFilename))
                            File.Delete(xunitCopyFilename);
                    }
                    catch { } // Throws on Mono because of a lack of shadow copying
                }
            }
        }
コード例 #2
0
        public XmlNode ExecuteWithCustomAssemblyName(string code, string assemblyName)
        {
            using (MockAssembly mockAssembly = new MockAssembly(assemblyName))
            {
                string fullAssemblyName  = Path.GetFullPath(assemblyName);
                string assemblyPath      = Path.GetDirectoryName(fullAssemblyName);
                string xunitCopyFilename = Path.Combine(assemblyPath, "xunit.dll");
                File.Copy(mockAssembly.XunitDllFilename, xunitCopyFilename, true);

                try
                {
                    mockAssembly.Compile(code, null);
                    return(mockAssembly.Run(null));
                }
                finally
                {
                    try
                    {
                        if (xunitCopyFilename != null && File.Exists(xunitCopyFilename))
                        {
                            File.Delete(xunitCopyFilename);
                        }
                    }
                    catch { }                     // Throws on Mono because of a lack of shadow copying
                }
            }
        }
コード例 #3
0
    public void PassingTest()
    {
        string code = @"
                using Xunit;

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

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

            xunit task = new xunit
            {
                Assembly = new TaskItem(mockAssembly.FileName),
                BuildEngine = new StubBuildEngine()
            };

            task.Execute();

            Assert.Equal(0, task.ExitCode);
        }
    }
コード例 #4
0
 public XmlNode Execute(string code, string configFile, params string[] references)
 {
     using (MockAssembly mockAssembly = new MockAssembly())
     {
         mockAssembly.Compile(code, references);
         return mockAssembly.Run(configFile);
     }
 }
コード例 #5
0
 public XmlNode Execute(string code, string configFile, params string[] references)
 {
     using (MockAssembly mockAssembly = new MockAssembly())
     {
         mockAssembly.Compile(code, references);
         return(mockAssembly.Run(configFile));
     }
 }
コード例 #6
0
 public void AssemblyFilenameInXmlMatchesOriginallyPassedNameToExecutor()
 {
     using (MockAssembly mockAssembly = new MockAssembly())
     {
         mockAssembly.Compile("");
         XmlNode assemblyNode = mockAssembly.Run(null);
         ResultXmlUtility.AssertAttribute(assemblyNode, "name", mockAssembly.FileName);
     }
 }
コード例 #7
0
    public void ConsoleRunnerPreservesCurrentDirectoryForXmlOutput()
    {
        string code = @"
                using System.IO;
                using Xunit;

                public class ChangeDirectoryTests
                {
                    [Fact]
                    public void ChangeDirectory()
                    {
                        Directory.SetCurrentDirectory(Path.GetTempPath());
                    }
                }
            ";

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

            StringWriter writer = new StringWriter();

            string workingDirectory = Directory.GetCurrentDirectory();
            string xmlFilename = Path.GetFileName(mockAssembly.FileName) + ".xml";
            string fullPathName = Path.Combine(workingDirectory, xmlFilename);

            TextWriter oldOut = Console.Out;
            TextWriter oldError = Console.Error;

            try
            {
                Console.SetOut(writer);
                Console.SetError(writer);

                Program.Main(new[] { mockAssembly.FileName, "/xml", xmlFilename });
            }
            finally
            {
                Console.SetOut(oldOut);
                Console.SetError(oldError);
            }

            Assert.True(File.Exists(fullPathName));
            File.Delete(fullPathName);
        }
    }
コード例 #8
0
    public void TestsFromAbstractBaseClassesShouldBeExecuted()
    {
        string code = @"
            using Xunit;

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

        XmlNode xmlFromExecutorWrapper;
        XmlNode xmlFromMate;

        using (MockAssembly mockAssembly = new MockAssembly())
        {
            mockAssembly.Compile(code);
            xmlFromExecutorWrapper = mockAssembly.Run();
            xmlFromMate = mockAssembly.RunWithMate();
        }

        // Make sure that we have these (and only these) attributes
        AssertExactAttributeList(
            xmlFromExecutorWrapper,
            "name", "configFile", "total", "passed", "failed", "skipped", "environment",
            "time", "run-date", "run-time", "test-framework"
        );
        AssertExactAttributeList(
            xmlFromMate,
            "name", "configFile", "total", "passed", "failed", "skipped", "environment",
            "time", "run-date", "run-time", "test-framework"
        );

        // Only compare values on assembly node, because we know that MATE
        // uses the actual XML from Executor for classes & tests. We can't
        // do equivalence for "time", "run-date", "run-time" because of variance.
        AssertAttributeEqual(xmlFromExecutorWrapper, xmlFromMate, "name");
        AssertAttributeEqual(xmlFromExecutorWrapper, xmlFromMate, "configFile");
        AssertAttributeEqual(xmlFromExecutorWrapper, xmlFromMate, "total");
        AssertAttributeEqual(xmlFromExecutorWrapper, xmlFromMate, "passed");
        AssertAttributeEqual(xmlFromExecutorWrapper, xmlFromMate, "failed");
        AssertAttributeEqual(xmlFromExecutorWrapper, xmlFromMate, "skipped");
        AssertAttributeEqual(xmlFromExecutorWrapper, xmlFromMate, "environment");
        AssertAttributeEqual(xmlFromExecutorWrapper, xmlFromMate, "test-framework");
    }
コード例 #9
0
    public void MultiAssemblyAcceptanceTest()
    {
        string code =
            @"
                using System;
                using Xunit;

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

        using (MockAssembly mockAssembly1 = new MockAssembly())
        using (MockAssembly mockAssembly2 = new MockAssembly())
        {
            mockAssembly1.Compile(code);
            mockAssembly2.Compile(code);
            Mock<ITestMethodRunnerCallback> callback = new Mock<ITestMethodRunnerCallback>();
            callback.Setup(c => c.TestStart(It.IsAny<TestMethod>())).Returns(true);
            callback.Setup(c => c.TestFinished(It.IsAny<TestMethod>())).Returns(true);
            MultiAssemblyTestEnvironment mate = new MultiAssemblyTestEnvironment();
            mate.Load(mockAssembly1.FileName);
            mate.Load(mockAssembly2.FileName);
            TestAssembly testAssembly1 = mate.EnumerateTestAssemblies().Where(a => a.AssemblyFilename == mockAssembly1.FileName).Single();
            TestAssembly testAssembly2 = mate.EnumerateTestAssemblies().Where(a => a.AssemblyFilename == mockAssembly2.FileName).Single();
            TestMethod assembly1Method = testAssembly1.EnumerateTestMethods().Single();
            TestMethod assembly2Method = testAssembly1.EnumerateTestMethods().Single();

            mate.Run(mate.EnumerateTestMethods(), callback.Object);

            callback.Verify(c => c.AssemblyStart(testAssembly1));
            callback.Verify(c => c.TestStart(assembly1Method));
            callback.Verify(c => c.TestFinished(assembly1Method));
            callback.Verify(c => c.AssemblyFinished(testAssembly1, 1, 0, 0, It.IsAny<double>()));
            callback.Verify(c => c.AssemblyStart(testAssembly2));
            callback.Verify(c => c.TestStart(assembly2Method));
            callback.Verify(c => c.TestFinished(assembly2Method));
            callback.Verify(c => c.AssemblyFinished(testAssembly2, 1, 0, 0, It.IsAny<double>()));
        }
    }
コード例 #10
0
    public void ConsoleRunnerPreservesCurrentDirectoryForXmlOutput()
    {
        string code = @"
                using System.IO;
                using Xunit;

                public class ChangeDirectoryTests
                {
                    [Fact]
                    public void ChangeDirectory()
                    {
                        Directory.SetCurrentDirectory(Path.GetTempPath());
                    }
                }
            ";

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

            string workingDirectory = Directory.GetCurrentDirectory();
            string xmlFilename = Path.GetFileName(mockAssembly.FileName) + ".xml";
            string fullPathName = Path.Combine(workingDirectory, xmlFilename);
            xunit task = new xunit
                             {
                                 Assembly = new TaskItem(mockAssembly.FileName),
                                 Xml = new TaskItem(xmlFilename),
                                 BuildEngine = new StubBuildEngine()
                             };

            task.Execute();

            Assert.True(File.Exists(fullPathName));
            File.Delete(fullPathName);
        }
    }
コード例 #11
0
        public void NonTestMethodInClassWithTestMethod()
        {
            string code = @"
                    using Xunit;

                    public class TestClass
                    {
                        public void NonTestMethod()
                        {
                        }

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

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

                XmlNode lastNode = null;

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

                Assert.Equal("class", lastNode.Name);
                Assert.Equal(0, lastNode.ChildNodes.Count);   // Empty class node
            }
        }
コード例 #12
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);
            }
        }
コード例 #13
0
        public void CanCancelBetweenTestMethodRuns()
        {
            string code = @"
                    using Xunit;

                    public class TestClass
                    {
                        [Fact]
                        public void TestMethod1()
                        {
                        }

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

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

                XmlNode lastNode = null;

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

                Assert.Equal(0, lastNode.ChildNodes.Count);   // Cancels from the start of the first test
            }
        }
コード例 #14
0
    public void SingleAssemblyAcceptanceTest()
    {
        string code =
            @"
                using System;
                using Xunit;

                public class MockTestClass
                {
                    [Fact]
                    public void SuccessTest()
                    {
                        Assert.Equal(2, 2);
                    }
                    [Fact]
                    public void FailureTest()
                    {
                        Assert.Equal(2, 3);
                    }
                    [Fact(Skip=""I'm too lazy to run today"")]
                    public void SkippingTest()
                    {
                        Assert.Equal(2, 4);
                    }
                }
            ";

        using (MockAssembly mockAssembly = new MockAssembly())
        {
            mockAssembly.Compile(code);
            Mock<ITestMethodRunnerCallback> callback = new Mock<ITestMethodRunnerCallback>();
            callback.Setup(c => c.TestStart(It.IsAny<TestMethod>())).Returns(true);
            callback.Setup(c => c.TestFinished(It.IsAny<TestMethod>())).Returns(true);
            MultiAssemblyTestEnvironment mate = new MultiAssemblyTestEnvironment();
            mate.Load(mockAssembly.FileName);
            TestAssembly testAssembly = mate.EnumerateTestAssemblies().Single();
            TestMethod passingMethod = testAssembly.EnumerateTestMethods().Where(m => m.MethodName == "SuccessTest").Single();
            TestMethod failingMethod = testAssembly.EnumerateTestMethods().Where(m => m.MethodName == "FailureTest").Single();
            TestMethod skippedMethod = testAssembly.EnumerateTestMethods().Where(m => m.MethodName == "SkippingTest").Single();

            mate.Run(mate.EnumerateTestMethods(), callback.Object);

            callback.Verify(c => c.AssemblyStart(testAssembly));
            callback.Verify(c => c.TestStart(passingMethod));
            callback.Verify(c => c.TestFinished(passingMethod));
            callback.Verify(c => c.TestStart(failingMethod));
            callback.Verify(c => c.TestFinished(failingMethod));
            callback.Verify(c => c.TestStart(skippedMethod), Times.Never());
            callback.Verify(c => c.TestFinished(skippedMethod));
            callback.Verify(c => c.AssemblyFinished(testAssembly, 3, 1, 1, It.IsAny<double>()));
            var passingMethodResult = Assert.IsType<TestPassedResult>(passingMethod.RunResults[0]);
            Assert.Null(passingMethodResult.Output);
            var failingMethodResult = Assert.IsType<TestFailedResult>(failingMethod.RunResults[0]);
            Assert.Null(failingMethodResult.Output);
            Assert.Equal("Xunit.Sdk.EqualException", failingMethodResult.ExceptionType);
            var skippedMethodResult = Assert.IsType<TestSkippedResult>(skippedMethod.RunResults[0]);
        }
    }
コード例 #15
0
        public void InvalidClassName()
        {
            string code = @"
                    using Xunit;

                    public class TestClass
                    {
                    }";

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

                using (ExecutorWrapper wrapper = new ExecutorWrapper(assembly.FileName, null, false))
                    Assert.Throws<ArgumentException>(() => wrapper.RunClass("TestClassIsNotMe", null));
            }
        }
コード例 #16
0
        public void InvalidMethodName()
        {
            string code = @"
                    using Xunit;

                    public class TestClass
                    {
                        public void DummyMethod() {}
                    }";

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

                using (ExecutorWrapper wrapper = new ExecutorWrapper(assembly.FileName, null, false))
                    Assert.Throws<ArgumentException>(
                        () => wrapper.RunTests("TestClass",
                                               new List<string> { "DummyMethod", "DummyMethod2" },
                                               null));
            }
        }
コード例 #17
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);
            }
        }
コード例 #18
0
        public void AssemblyWithMultipleTestsAndMultipleClasses()
        {
            string code = @"
                    using Xunit;

                    public class JustAPlainOldClass
                    {
                        public class Class1
                        {
                            [Fact] public void Test1() {}
                            [Fact] public void Test2() {}
                        }

                        public class Class2
                        {
                            [Fact] public void Test3() {}
                        }
                    }";

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

                using (ExecutorWrapper wrapper = new ExecutorWrapper(assembly.FileName, null, false))
                    Assert.Equal(3, wrapper.GetAssemblyTestCount());
            }
        }
コード例 #19
0
        public void AssemblyWithNoTests()
        {
            string code = @"
                    public class JustAPlainOldClass
                    {
                    }";

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

                using (ExecutorWrapper wrapper = new ExecutorWrapper(assembly.FileName, null, false))
                    Assert.Equal(0, wrapper.GetAssemblyTestCount());
            }
        }
コード例 #20
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);
        }
コード例 #21
0
    public void MultiAssemblySearchFilterAcceptanceTest()
    {
        string code =
            @"
                using System;
                using Xunit;

                public class MockTestClass
                {
                    [Fact]
                    public void Test1()
                    {
                    }

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

        using (MockAssembly mockAssembly1 = new MockAssembly())
        using (MockAssembly mockAssembly2 = new MockAssembly())
        {
            mockAssembly1.Compile(code);
            mockAssembly2.Compile(code);
            Mock<ITestMethodRunnerCallback> callback = new Mock<ITestMethodRunnerCallback>();
            callback.Setup(c => c.TestStart(It.IsAny<TestMethod>())).Returns(true);
            callback.Setup(c => c.TestFinished(It.IsAny<TestMethod>())).Returns(true);
            MultiAssemblyTestEnvironment mate = new MultiAssemblyTestEnvironment();
            mate.Load(mockAssembly1.FileName);
            mate.Load(mockAssembly2.FileName);
            TestAssembly testAssembly1 = mate.EnumerateTestAssemblies().Where(a => a.AssemblyFilename == mockAssembly1.FileName).Single();
            TestAssembly testAssembly2 = mate.EnumerateTestAssemblies().Where(a => a.AssemblyFilename == mockAssembly2.FileName).Single();
            TestMethod assembly1Test1Method = testAssembly1.EnumerateTestMethods().Where(m => m.MethodName == "Test1").Single();
            TestMethod assembly1Test2Method = testAssembly1.EnumerateTestMethods().Where(m => m.MethodName == "Test2").Single();
            TestMethod assembly2Test1Method = testAssembly2.EnumerateTestMethods().Where(m => m.MethodName == "Test1").Single();
            TestMethod assembly2Test2Method = testAssembly2.EnumerateTestMethods().Where(m => m.MethodName == "Test2").Single();

            mate.Run(mate.EnumerateTestMethods(m => m.MethodName.Contains("t2")), callback.Object);

            callback.Verify(c => c.TestStart(assembly1Test1Method), Times.Never());
            callback.Verify(c => c.TestStart(assembly1Test2Method));
            callback.Verify(c => c.TestStart(assembly2Test1Method), Times.Never());
            callback.Verify(c => c.TestStart(assembly2Test2Method));
        }
    }
コード例 #22
0
        public void SuccessfulConstructionCanReturnXunitDllVersion()
        {
            string code = @"
                    using Xunit;

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

            AssemblyName xunitName = XunitAssemblyName;

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

                using (ExecutorWrapper wrapper = new ExecutorWrapper(assembly.FileName, null, false))
                    Assert.Equal(xunitName.Version.ToString(), wrapper.XunitVersion);
            }
        }
コード例 #23
0
        public void SuccessfulConstructionCanReturnConfigFilename()
        {
            string code = @"
                    using Xunit;

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

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

                using (ExecutorWrapper wrapper = new ExecutorWrapper(assembly.FileName, @"C:\Foo\bar.config", false))
                    Assert.Equal(@"C:\Foo\bar.config", wrapper.ConfigFilename);
            }
        }
コード例 #24
0
    public void MultiAssemblyTraitFilterAcceptanceTest()
    {
        string code =
            @"
                using System;
                using Xunit;

                public class MockTestClass
                {
                    [Fact]
                    [Trait(""Trait1"", ""Value1"")]
                    public void Value1Test()
                    {
                    }

                    [Fact]
                    [Trait(""Trait1"", ""Value2"")]
                    public void Value2Test()
                    {
                    }
                }
            ";

        using (MockAssembly mockAssembly1 = new MockAssembly())
        using (MockAssembly mockAssembly2 = new MockAssembly())
        {
            mockAssembly1.Compile(code);
            mockAssembly2.Compile(code);
            Mock<ITestMethodRunnerCallback> callback = new Mock<ITestMethodRunnerCallback>();
            callback.Setup(c => c.TestStart(It.IsAny<TestMethod>())).Returns(true);
            callback.Setup(c => c.TestFinished(It.IsAny<TestMethod>())).Returns(true);
            MultiAssemblyTestEnvironment mate = new MultiAssemblyTestEnvironment();
            mate.Load(mockAssembly1.FileName);
            mate.Load(mockAssembly2.FileName);
            TestAssembly testAssembly1 = mate.EnumerateTestAssemblies().Where(a => a.AssemblyFilename == mockAssembly1.FileName).Single();
            TestAssembly testAssembly2 = mate.EnumerateTestAssemblies().Where(a => a.AssemblyFilename == mockAssembly2.FileName).Single();
            TestMethod assembly1Value1Method = testAssembly1.EnumerateTestMethods().Where(m => m.MethodName == "Value1Test").Single();
            TestMethod assembly1Value2Method = testAssembly1.EnumerateTestMethods().Where(m => m.MethodName == "Value2Test").Single();
            TestMethod assembly2Value1Method = testAssembly2.EnumerateTestMethods().Where(m => m.MethodName == "Value1Test").Single();
            TestMethod assembly2Value2Method = testAssembly2.EnumerateTestMethods().Where(m => m.MethodName == "Value2Test").Single();

            mate.Run(mate.EnumerateTestMethods(m => m.Traits["Trait1"].FirstOrDefault() == "Value1"), callback.Object);

            callback.Verify(c => c.TestStart(assembly1Value1Method));
            callback.Verify(c => c.TestStart(assembly1Value2Method), Times.Never());
            callback.Verify(c => c.TestStart(assembly2Value1Method));
            callback.Verify(c => c.TestStart(assembly2Value2Method), Times.Never());
        }
    }
コード例 #25
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);
            }
        }
コード例 #26
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);
        }
コード例 #27
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);
            }
        }
コード例 #28
0
        public void AssemblyWithNoTests()
        {
            string code = @"
                    using Xunit;

                    public class PlainOldDotNetClass
                    {
                    }";

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

                XmlNode lastNode = null;

                using (ExecutorWrapper wrapper = new ExecutorWrapper(assembly.FileName, null, false))
                    wrapper.RunAssembly(node => { lastNode = node; return true; });

                Assert.NotNull(lastNode);   // Always get an <assembly> node, even if there are no tests
                Assert.Equal(0, lastNode.ChildNodes.Count);
            }
        }
コード例 #29
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);
            }
        }
コード例 #30
0
        public void ClassWhichHasNoTests()
        {
            string code = @"
                    using Xunit;

                    public class PlainOldDotNetClass
                    {
                    }";

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

                XmlNode lastNode = null;

                using (ExecutorWrapper wrapper = new ExecutorWrapper(assembly.FileName, null, false))
                    wrapper.RunClass("PlainOldDotNetClass", node => { lastNode = node; return true; });

                Assert.Equal("class", lastNode.Name);
                Assert.Equal(0, lastNode.ChildNodes.Count);   // Empty class node
            }
        }
コード例 #31
0
        public void AcceptanceTest()
        {
            string code =
                @"
                    using System;
                    using Xunit;

                    namespace Namespace1
                    {
                        public class Class1
                        {
                            [Fact]
                            [Trait(""Name!"", ""Value!"")]
                            public void Passing()
                            {
                                Assert.Equal(2, 2);
                            }

                            [Fact]
                            public void Failing()
                            {
                                Assert.Equal(2, 3);
                            }

                            [Fact(Skip=""Skipping"")]
                            public void Skipped() {}

                            [Fact(Name=""Custom Test Name"")]
                            public void CustomName() {}
                        }
                    }

                    namespace Namespace2
                    {
                        public class OuterClass
                        {
                            public class Class2
                            {
                                [Fact]
                                public void Passing()
                                {
                                    Assert.Equal(2, 2);
                                }
                            }
                        }
                    }
                ";

            XmlNode assemblyNode = null;
            string filename = null;

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

                using (ExecutorWrapper wrapper = new ExecutorWrapper(assembly.FileName, null, false))
                    assemblyNode = wrapper.EnumerateTests();
            }

            Assert.Equal(filename, assemblyNode.Attributes["name"].Value);

            XmlNodeList classNodes = assemblyNode.SelectNodes("class");
            Assert.Equal(classNodes.Count, 2);

            XmlNode class1Node = classNodes[0];
            Assert.Equal("Namespace1.Class1", class1Node.Attributes["name"].Value);

            XmlNodeList class1MethodNodes = class1Node.SelectNodes("method");
            Assert.Equal(class1MethodNodes.Count, 4);
            XmlNode passingNode = class1Node.SelectSingleNode(@"//method[@method=""Passing""]");
            Assert.NotNull(passingNode);
            Assert.Equal("Namespace1.Class1.Passing", passingNode.Attributes["name"].Value);
            XmlNodeList traitsNodes = passingNode.SelectNodes("traits/trait");
            XmlNode traitNode = (XmlNode)Assert.Single(traitsNodes);
            Assert.Equal("Name!", traitNode.Attributes["name"].Value);
            Assert.Equal("Value!", traitNode.Attributes["value"].Value);
            Assert.NotNull(class1Node.SelectSingleNode(@"//method[@method=""Failing""]"));
            XmlNode skipNode = class1Node.SelectSingleNode(@"//method[@method=""Skipped""]");
            Assert.NotNull(skipNode);
            Assert.Equal("Skipping", skipNode.Attributes["skip"].Value);
            XmlNode customNameNode = class1Node.SelectSingleNode(@"//method[@method=""CustomName""]");
            Assert.NotNull(customNameNode);
            Assert.Equal("Custom Test Name", customNameNode.Attributes["name"].Value);

            XmlNode class2Node = classNodes[1];
            Assert.Equal("Namespace2.OuterClass+Class2", class2Node.Attributes["name"].Value);

            XmlNodeList class2MethodNodes = class2Node.SelectNodes("method");
            Assert.Equal(class2MethodNodes.Count, 1);
            Assert.Equal("Namespace2.OuterClass+Class2", class2MethodNodes[0].Attributes["type"].Value);
            Assert.Equal("Passing", class2MethodNodes[0].Attributes["method"].Value);
        }
コード例 #32
0
    public void MultiAssemblyGetTraitsAcceptanceTest()
    {
        string code1 =
            @"
                using System;
                using Xunit;

                public class MockTestClass
                {
                    [Fact]
                    [Trait(""Trait1"", ""Value1"")]
                    public void Value1Test()
                    {
                    }

                    [Fact]
                    [Trait(""Trait1"", ""Value2"")]
                    public void Value2Test()
                    {
                    }

                    [Fact]
                    [Trait(""Trait2"", ""Value1"")]
                    public void Trait2Value1Test()
                    {
                    }
                }
            ";

        string code2 =
            @"
                using System;
                using Xunit;

                public class MockTestClass
                {
                    [Fact]
                    [Trait(""Trait1"", ""Value1"")]
                    public void OtherTest1()
                    {
                    }

                    [Fact]
                    [Trait(""Trait3"", ""Value42"")]
                    public void Crazy()
                    {
                    }
                }
            ";

        using (MockAssembly mockAssembly1 = new MockAssembly())
        using (MockAssembly mockAssembly2 = new MockAssembly())
        {
            mockAssembly1.Compile(code1);
            mockAssembly2.Compile(code2);
            MultiAssemblyTestEnvironment mate = new MultiAssemblyTestEnvironment();
            mate.Load(mockAssembly1.FileName);
            mate.Load(mockAssembly2.FileName);

            MultiValueDictionary<string, string> result = mate.EnumerateTraits();

            var trait1 = result["Trait1"];
            Assert.Equal(2, trait1.Count());
            Assert.Contains("Value1", trait1);
            Assert.Contains("Value2", trait1);
            var trait2 = result["Trait2"];
            Assert.Single(trait2);
            Assert.Contains("Value1", trait2);
            var trait3 = result["Trait3"];
            Assert.Single(trait3);
            Assert.Contains("Value42", trait3);
        }
    }