using System.Collections.Generic; using System.Reflection; using UnitTestSharp; using UnitTestSharp.DataDriven; using TestsForUnitTestSharp.DataDriven; namespace TestsForUnitTestSharp { /// /// Tests the test runner functions /// public class TestRunnerTests : TestFixture { public void TODO_FauxTest() { TODO("Allow clients to specify a test as ErrorAsWarning, so the test suite won't fail if that test fails."); TODO("Most everything in TestRunner.cs is untested."); TODO("TestRunner.cs grew from modest beginnings to the behomoth it is now from feature creep. " + "Consider splitting it into more managable, less static chunks."); } public class BasicTests : TestFixture { [IgnoreFixture] class FauxFixture : TestFixture { public void Test() { } void HiddenTest() { } [IgnoreTest] void IgnoredTest() { } } public void ExtractMethodsToRun_Test() { int ignored, hidden; IList testsToRun; string outputExpected = BuildMessages.MSBuildMessage( "Method is non public. Did you forget " + "to mark it public? If you don't want the method tested, " + "mark it with the IgnoreMethod attribute.", "TestsForUnitTestSharp::TestRunnerTests.BasicTests.FauxFixture::HiddenTest", false, null, null ) + "\n"; string outputActual = ""; TestRunner.ExtractMethodsToRun( typeof(FauxFixture), ref outputActual, out testsToRun, out ignored, out hidden); CheckEqual(1, ignored); CheckEqual(1, hidden); CheckEqual(1, testsToRun.Count); CheckEqual(outputExpected, outputActual); } } public class CanCount : TestFixture { [IgnoreFixture] class FauxFixture : TestFixture { public void Test1() { } public void Test2() { Check(false); Check(false); } public void Test3() { Check(false); } } public void CanCountTest() { string output = ""; int totalTests = 0; int failedTests = 0; int failedChecks = 0; TestRunner.RunTestFixture(typeof(FauxFixture), ref output, ref totalTests, ref failedTests, ref failedChecks); CheckEqual(3, totalTests); CheckEqual(2, failedTests); CheckEqual(3, failedChecks); } } public class CanCountDataDriven : TestFixture { [UnitTestSharp.DataDriven.DataDriven(typeof(DataDriven.NumbersTable))] [IgnoreFixture] class FauxFixture : TestFixture { public void Test1() { } public void Test2() { Check(false); } public void Test3() { CheckEqual(-50, DataRow["Numbers"]); } } public void DataDrivenCanCount_NormalTest() { string output = ""; int totalTests = 0; int failedTests = 0; int failedChecks = 0; TestRunner.RunTestFixture(typeof(FauxFixture), ref output, ref totalTests, ref failedTests, ref failedChecks); CheckEqual(9, totalTests); CheckEqual(5, failedTests); CheckEqual(5, failedChecks); } } public class CanCountDataDriven_ClassRunOnce : TestFixture { [DataDriven(typeof(NumbersTable))] [IgnoreFixture] [UnitTestSharp.DataDriven.RunOnce] class FauxFixture : TestFixture { public void Test1() { } public void Test2() { Check(false); } public void Test3() { CheckEqual("-50", DataRow["Numbers"]); } } public void DataDrivenCanCount_ClassRunOnceTest() { string output = ""; int totalTests = 0; int failedTests = 0; int failedChecks = 0; TestRunner.RunTestFixture(typeof(FauxFixture), ref output, ref totalTests, ref failedTests, ref failedChecks); CheckEqual(3, totalTests); CheckEqual(2, failedTests); CheckEqual(2, failedChecks); } } public class CanCountDataDriven_MethodRunOnce : TestFixture { [DataDriven(typeof(NumbersTable))] [IgnoreFixture] class FauxFixture : TestFixture { public void Test1() { } public void Test2() { Check(false); } [UnitTestSharp.DataDriven.RunOnce] public void Test3() { CheckEqual("-50", DataRow["Numbers"]); } } public void DataDrivenCanCount_MethodRunOnceTest() { string output = ""; int totalTests = 0; int failedTests = 0; int failedChecks = 0; TestRunner.RunTestFixture(typeof(FauxFixture), ref output, ref totalTests, ref failedTests, ref failedChecks); CheckEqual(7, totalTests); CheckEqual(4, failedTests); CheckEqual(4, failedChecks); } } public class RunTestFixtureTests : UnitTestSharp.TestFixture { private static int ThrowException() { throw new System.Exception(""); } [IgnoreFixture] public class BadFixture : UnitTestSharp.TestFixture { int x = ThrowException(); } public void ThrowsAnException() { string output = ""; int totalTests = 0, failedTests = 0, failedChecks = 0; TestRunner.RunTestFixture(typeof(BadFixture), ref output, ref totalTests, ref failedTests, ref failedChecks); Check(output.Contains("Failure creating test fixture")); } } } }