/// <summary> /// Runs the specified test. /// </summary> /// <param name="className"></param> /// <param name="methodName"></param> /// <returns>True to indicate success, false to indicate fail</returns> public static bool RunExampleTest(string className, string methodName) { using (MockAssembly assembly = new MockAssembly()) { XmlNode lastNode = null; XmlNode returnValue = null; using (ExecutorWrapper wrapper = new ExecutorWrapper("Coulda.Examples.dll", null, false)) { returnValue = wrapper.RunTest(className, methodName, node => { lastNode = node; return true; }); } XmlNode resultNode = ResultXmlUtility.GetResult(lastNode); //ResultXmlUtility.AssertAttribute(resultNode, "result", "Pass"); if(resultNode.Attributes["result"].Value == "Pass") { return true; } } return false; }
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); } }
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 } } }
public XmlNode Execute(string code, string configFile, params string[] references) { using (MockAssembly mockAssembly = new MockAssembly()) { mockAssembly.Compile(code, references); return mockAssembly.Run(configFile); } }
public void AssemblyFilenameInXmlMatchesOriginallyPassedNameToExecutor() { using (MockAssembly mockAssembly = new MockAssembly()) { mockAssembly.Compile(""); XmlNode assemblyNode = mockAssembly.Run(null); ResultXmlUtility.AssertAttribute(assemblyNode, "name", mockAssembly.FileName); } }
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); } }
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"); }
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>())); } }
public void Should_be_able_to_run_the_simple_example() { using (MockAssembly assembly = new MockAssembly()) { XmlNode lastNode = null; XmlNode returnValue = null; using (ExecutorWrapper wrapper = new ExecutorWrapper("Coulda.Examples.dll", null, false)) returnValue = wrapper.RunClass("Coulda.Examples.SimpleExample", node => { lastNode = node; return true; }); XmlNode resultNode = ResultXmlUtility.GetResult(lastNode); ResultXmlUtility.AssertAttribute(resultNode, "name", "My example test should be able to assert"); ResultXmlUtility.AssertAttribute(resultNode, "type", "Coulda.Examples.SimpleExample"); ResultXmlUtility.AssertAttribute(resultNode, "method", "A_simple_example"); ResultXmlUtility.AssertAttribute(resultNode, "result", "Pass"); } }
public void Should_be_able_to_have_a_failing_test() { using (MockAssembly assembly = new MockAssembly()) { XmlNode lastNode = null; XmlNode returnValue = null; using (ExecutorWrapper wrapper = new ExecutorWrapper("Coulda.Examples.dll", null, false)) returnValue = wrapper.RunClass("Coulda.Examples.FailingExample", node => { lastNode = node; return true; }); XmlNode resultNode = ResultXmlUtility.GetResult(lastNode); ResultXmlUtility.AssertAttribute(resultNode, "name", "A test with an assertion should be able to fail"); ResultXmlUtility.AssertAttribute(resultNode, "type", "Coulda.Examples.FailingExample"); ResultXmlUtility.AssertAttribute(resultNode, "method", "A_failing_example"); ResultXmlUtility.AssertAttribute(resultNode, "result", "Fail"); } }
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); } }
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); } }
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)); } }
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 } }
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); } }
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 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 } }
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 } }
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); } }
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 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); } }
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); } }
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()); } }
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); }
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); } }
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)); } }
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 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); } }
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()); } }
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); } }