示例#1
0
        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());
        }
示例#2
0
        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));
        }
示例#3
0
        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;
        }
示例#4
0
        public void GivenThereIsTwoTestClassesWithOneTestMethodInEachAndTwoRunnersThenEachRunnerShouldRunOneOfTheMethods()
        {
            var methodRunner1 = new Mock<IMethodRunner>();
            var methodRunner2 = new Mock<IMethodRunner>();
            var assembly = new TestAssembly("");
            var method1 = assembly.AddTestClass("type1").AddTestMethod("method1");
            var method2 = assembly.AddTestClass("type2").AddTestMethod("method2");

            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 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));
        }