public void GivenThereIsATestClassWithATestCleanupThenItShouldRunTheTestCleanupAfterTheTestMethodAndOnlyForTheTestMethodsInThatTestClass() { var methodRunner1 = new Mock<IMethodRunner>(); var methodRunner2 = new Mock<IMethodRunner>(); var assembly = new TestAssembly(""); var type1 = assembly.AddTestClass("type1"); var testClean1 = type1.AddTestCleanup("testClean1"); var method1 = type1.AddTestMethod("method1"); var method2 = type1.AddTestMethod("method2"); var type2 = assembly.AddTestClass("type2"); var method3 = type2.AddTestMethod("method3"); var method4 = type2.AddTestMethod("method4"); var runner = new Runner() { MethodRunners = new List<IMethodRunner> { methodRunner1.Object, methodRunner2.Object }, Assemblies = new List<TestAssembly>() { assembly }, }; runner.Run(); methodRunner1.Verify(m => m.Invoke(It.Is<IEnumerable<Method>>(e => e.SequenceEqual(new List<Method> { method1, testClean1, method3 }))), Times.Once()); methodRunner2.Verify(m => m.Invoke(It.Is<IEnumerable<Method>>(e => e.SequenceEqual(new List<Method> { method2, testClean1, method4 }))), Times.Once()); }
public void GivenThereIsATestClassWithAAssemblyInitThenItShouldRunTheAssemblyInitOnceForAllTheTestsInTheAssembly() { var methodRunner1 = new Mock<IMethodRunner>(); var methodRunner2 = new Mock<IMethodRunner>(); var assembly = new TestAssembly(""); var type1 = assembly.AddTestClass("type1"); var assemblyInit = assembly.SetAssemblyInit(new Method("assemblyInit",type1)); var method1 = type1.AddTestMethod("method1"); var method2 = type1.AddTestMethod("method2"); var type2 = assembly.AddTestClass("type2"); var method3 = type2.AddTestMethod("method3"); var method4 = type2.AddTestMethod("method4"); var runner = new Runner() { MethodRunners = new List<IMethodRunner> { methodRunner1.Object, methodRunner2.Object }, Assemblies = new List<TestAssembly>() { assembly }, }; var report1 = new SuccessMethodExecutionReport() { Method = method1, ConsoleOutput = "Executed 1" }; var report2 = new SuccessMethodExecutionReport() { Method = method2, ConsoleOutput = "Executed 2" }; var report3 = new SuccessMethodExecutionReport() { Method = method3, ConsoleOutput = "Executed 3" }; var report4 = new SuccessMethodExecutionReport() { Method = method4, ConsoleOutput = "Executed 4" }; var reportForRunner1 = new List<MethodExecutionReport> { report1, report3 }; var reportForRunner2 = new List<MethodExecutionReport> { report2, report4 }; methodRunner1.Setup(m => m.Invoke(It.Is<IEnumerable<Method>>(e => e.SequenceEqual(new List<Method> { assemblyInit, method1, method3 })))).Returns(reportForRunner1); methodRunner2.Setup(m => m.Invoke(It.Is<IEnumerable<Method>>(e => e.SequenceEqual(new List<Method> { assemblyInit, method2, method4 })))).Returns(reportForRunner2); runner.Run(); var actualReport = runner.Run(); Assert.AreEqual(actualReport.Count(), 4); Assert.IsTrue(actualReport.Contains(report1)); Assert.IsTrue(actualReport.Contains(report2)); Assert.IsTrue(actualReport.Contains(report3)); Assert.IsTrue(actualReport.Contains(report4)); }
public TestAssembly Load(string assemblyPath) { var assembly = System.Reflection.Assembly.LoadFrom(assemblyPath); var testAssembly = new TestAssembly(assemblyPath); foreach (var type in assembly.GetTypes()) { if (type.GetCustomAttributes(typeof(TestClassAttribute), true).Any()) { var testClass = testAssembly.AddTestClass(type.FullName); var testInit = type.GetMethods().FirstOrDefault(m => m.GetCustomAttributes(typeof(TestInitializeAttribute), true).Any()); var testCleanup = type.GetMethods().FirstOrDefault(m => m.GetCustomAttributes(typeof(TestCleanupAttribute), true).Any()); if (testInit != null) { testClass.AddTestInit(testInit.Name); } if (testCleanup != null) { testClass.AddTestCleanup(testCleanup.Name); } foreach (var method in type.GetMethods()) { if (method.GetCustomAttributes(typeof(TestMethodAttribute), true).Any()) { testClass.AddTestMethod(method.Name); } else if (method.GetCustomAttributes(typeof(AssemblyInitializeAttribute), true).Any()) { testAssembly.SetAssemblyInit(new Method(method.Name, testClass)); } else if (method.GetCustomAttributes(typeof(AssemblyCleanupAttribute), true).Any()) { testAssembly.SetAssemblyCleanup(new Method(method.Name, testClass)); } } } } return testAssembly; }
public void GivenThereIsTwoTestMethodsInTheSameTestClassAndTwoRunnersThenEachRunnerShouldRunOneOfTheMethods() { var methodRunner1 = new Mock<IMethodRunner>(); var methodRunner2 = new Mock<IMethodRunner>(); var type = new TestAssembly("").AddTestClass(""); TestMethod method1 = type.AddTestMethod("method1"); TestMethod method2 = type.AddTestMethod("method2"); var runner = new Runner() { MethodRunners = new List<IMethodRunner> { methodRunner1.Object, methodRunner2.Object }, Assemblies = new List<TestAssembly>() { type.TestAssembly }, }; var report1 = new SuccessMethodExecutionReport() { Method = method1, ConsoleOutput = "Executed 1" }; var report2 = new SuccessMethodExecutionReport() { Method = method2, ConsoleOutput = "Executed 2" }; var reportForRunner1 = new List<MethodExecutionReport> { report1 }; var reportForRunner2 = new List<MethodExecutionReport> { report2 }; methodRunner1.Setup(m => m.Invoke(It.Is<IEnumerable<Method>>(e => e.SequenceEqual(new List<Method> { method1 })))).Returns(reportForRunner1); methodRunner2.Setup(m => m.Invoke(It.Is<IEnumerable<Method>>(e => e.SequenceEqual(new List<Method> { method2 })))).Returns(reportForRunner2); var actualReport = runner.Run(); Assert.AreEqual(actualReport.Count(), 2); Assert.IsTrue(actualReport.Contains(report1)); Assert.IsTrue(actualReport.Contains(report2)); }
public void GivenThereIsTwoTestMethodsInTheSameTestClassAndOneRunnerThenItShouldRunTheseMethods() { var methodRunner = new Mock<IMethodRunner>(); var type = new TestAssembly("").AddTestClass(""); TestMethod method1 = type.AddTestMethod("method1"); TestMethod method2 = type.AddTestMethod("method2"); var runner = new Runner() { MethodRunners = new List<IMethodRunner> { methodRunner.Object }, Assemblies = new List<TestAssembly>() { type.TestAssembly }, }; runner.Run(); methodRunner.Verify(m => m.Invoke(It.Is<IEnumerable<Method>>(e => e.SequenceEqual(new List<Method> { method1, method2 }))), Times.Once()); var report1 = new SuccessMethodExecutionReport() { Method = method1, ConsoleOutput = "Executed 1" }; var report2 = new SuccessMethodExecutionReport() { Method = method2, ConsoleOutput = "Executed 2" }; var expectedReport = new List<MethodExecutionReport> { report1, report2 }; methodRunner.Setup(m => m.Invoke(It.Is<IEnumerable<Method>>(e => e.SequenceEqual(new List<Method> { method1, method2 })))).Returns(expectedReport); var actualReport = runner.Run(); Assert.AreEqual(actualReport.Count(), 2); Assert.IsTrue(actualReport.Contains(report1)); Assert.IsTrue(actualReport.Contains(report2)); }
public void GivenThereIsOnlyOneTestMethodThenTheRunnerShouldRunThatMethod() { var methodRunner = new Mock<IMethodRunner>(); var method = new TestAssembly("").AddTestClass("").AddTestMethod("method1"); var runner = new Runner() { MethodRunners = new List<IMethodRunner> { methodRunner.Object }, Assemblies = new List<TestAssembly>() { method.OwningTestAssembly }, }; var expectedReport = new List<MethodExecutionReport> { new SuccessMethodExecutionReport(){Method = method,ConsoleOutput="Executed"} }; methodRunner.Setup(m => m.Invoke(It.Is<IEnumerable<Method>>(e => e.First() == method))).Returns(expectedReport); var actualReport = runner.Run(); Assert.AreEqual(expectedReport[0], actualReport.First()); }
public TestClass(string name, TestAssembly assembly) : base(name, assembly) { TestMethods = new List<TestMethod>(); this.TestAssembly = assembly; }