using System; using UnitTestSharp; using UnitTestSharp.DataDriven; namespace TestsForUnitTestSharp { public class TestFixtureTests : TestFixture { public class TestMemberCounting_NoneHidden : TestFixture { public void Test() { CheckEqual(1, Tests); CheckEqual(0, HiddenTests); CheckEqual(0, IgnoredMethods); } } [UnitTestSharp.NoNotifyUnmarkedHidden] public class TestMemberCounting_OneHidden : TestFixture { private void HiddenTest() { } public void Test() { CheckEqual(1, Tests); CheckEqual(1, HiddenTests); CheckEqual(0, IgnoredMethods); } } public class TestMemberCounting_Ignored : TestFixture { [UnitTestSharp.IgnoreTest] public void IgnoredMethod() { } public void Test() { CheckEqual(1, Tests); CheckEqual(0, HiddenTests); CheckEqual(1, IgnoredMethods); } } #region SetupAndTeardownThrowing #region MockFixtures [IgnoreFixture] public class FixtureSetup_Throw : TestFixture { public override void FixtureSetup() { throw new Exception("A fake problem during setup."); } public void Test1() { } public void Test2() { } } [IgnoreFixture] public class FixtureTeardown_Throw : TestFixture { public override void FixtureTeardown() { throw new Exception("A fake problem during setup."); } public void Test1() { } public void Test2() { } } [IgnoreFixture] public class TestSetup_Throw : TestFixture { public override void TestSetup() { throw new Exception("A fake problem during setup."); } public void Test1() { } public void Test2() { } } [IgnoreFixture] public class TestTeardown_Throw : TestFixture { public override void TestTeardown() { throw new Exception("A fake problem during setup."); } public void Test1() { } public void Test2() { } } [IgnoreFixture] [DataDriven("ExcelReaderTest.xls", "Numbers")] public class DataRowTeardown_Throw : TestFixture { public override void DataRowTeardown() { throw new Exception("A fake problem during setup."); } public void Test1() { } public void Test2() { } } [IgnoreFixture] [DataDriven("ExcelReaderTest.xls", "Numbers")] public class DataRowSetup_Throw : TestFixture { public override void DataRowSetup() { throw new Exception("A fake problem during setup."); } public void Test1() { } public void Test2() { } } #endregion public class FixturesThrowingAtWeirdTimes : TestFixture { public void OnFixtureSetup() { string output = ""; int totalTests = 0, failedTests = 0, failedChecks = 0; Type type = typeof(FixtureSetup_Throw); TestRunner.RunTestFixture(type, ref output, ref totalTests, ref failedTests, ref failedChecks); CheckEqual(2, totalTests); CheckEqual(2, failedTests); CheckEqual(1, failedChecks); CheckNotNull(output); } public void OnFixtureTeardown() { string output = ""; int totalTests = 0, failedTests = 0, failedChecks = 0; Type type = typeof(FixtureTeardown_Throw); TestRunner.RunTestFixture(type, ref output, ref totalTests, ref failedTests, ref failedChecks); CheckEqual(2, totalTests); CheckEqual(0, failedTests); CheckEqual(1, failedChecks); CheckNotNull(output); } public void OnTestSetup() { string output = ""; int totalTests = 0, failedTests = 0, failedChecks = 0; Type type = typeof(TestSetup_Throw); TestRunner.RunTestFixture(type, ref output, ref totalTests, ref failedTests, ref failedChecks); CheckEqual(2, totalTests); CheckEqual(2, failedTests); CheckEqual(2, failedChecks); CheckNotNull(output); } public void OnTestTeardown() { string output = ""; int totalTests = 0, failedTests = 0, failedChecks = 0; Type type = typeof(TestTeardown_Throw); TestRunner.RunTestFixture(type, ref output, ref totalTests, ref failedTests, ref failedChecks); CheckEqual(2, totalTests); CheckEqual(2, failedTests); CheckEqual(2, failedChecks); CheckNotNull(output); } public void OnDataRowSetup() { string output = ""; int totalTests = 0, failedTests = 0, failedChecks = 0; Type type = typeof(DataRowSetup_Throw); TestRunner.RunTestFixture(type, ref output, ref totalTests, ref failedTests, ref failedChecks); CheckEqual(6, totalTests); CheckEqual(6, failedTests); CheckEqual(3, failedChecks); //for the 3 data rows CheckNotNull(output); } public void OnDataRowTeardown() { string output = ""; int totalTests = 0, failedTests = 0, failedChecks = 0; Type type = typeof(DataRowTeardown_Throw); TestRunner.RunTestFixture(type, ref output, ref totalTests, ref failedTests, ref failedChecks); CheckEqual(6, totalTests); CheckEqual(0, failedTests); CheckEqual(3, failedChecks); //for the three data rows CheckNotNull(output); } } #endregion #region Timeouts #region MockFixtures [IgnoreFixture] public class FixtureSetup_InfiniteLoop : TestFixture { public override void FixtureSetup() { while (true) ; } public void Test1() { } public void Test2() { } } [IgnoreFixture] public class FixtureTeardown_InfiniteLoop : TestFixture { public override void FixtureTeardown() { while (true) ; } public void Test1() { } public void Test2() { } } [IgnoreFixture] public class TestSetup_InfiniteLoop : TestFixture { public override void TestSetup() { while (true) ; } public void Test1() { } public void Test2() { } } [IgnoreFixture] public class TestTeardown_InfiniteLoop : TestFixture { public override void TestTeardown() { while (true) ; } public void Test1() { } public void Test2() { } } [IgnoreFixture] [DataDriven("ExcelReaderTest.xls", "Numbers")] public class DataRowSetup_InfiniteLoop : TestFixture { public override void DataRowSetup() { while (true) ; } public void Test1() { } public void Test2() { } } [IgnoreFixture] [DataDriven("ExcelReaderTest.xls", "Numbers")] public class DataRowTeardown_InfiniteLoop : TestFixture { public override void DataRowTeardown() { while (true) ; } public void Test1() { } public void Test2() { } } #endregion public class FixturesInfiniteLoops : TestFixture { int? oldTestTimeout; int? oldFixtureTimeout; bool ignoreDebugger; public override void FixtureSetup() { oldFixtureTimeout = TestRunner.SetupTeardownDefaultTimeoutInMilliseconds; oldTestTimeout = TestRunner.TestDefaultTimeoutInMilliseconds; ignoreDebugger = TestRunner.IgnoreDebugger; TestRunner.SetupTeardownDefaultTimeoutInMilliseconds = 10; TestRunner.TestDefaultTimeoutInMilliseconds = System.Threading.Timeout.Infinite; TestRunner.IgnoreDebugger = true; } public override void FixtureTeardown() { TestRunner.SetupTeardownDefaultTimeoutInMilliseconds = oldFixtureTimeout; TestRunner.TestDefaultTimeoutInMilliseconds = oldTestTimeout; TestRunner.IgnoreDebugger = ignoreDebugger; } public void OnFixtureSetup() { string output = ""; int totalTests = 0, failedTests = 0, failedChecks = 0; Type type = typeof(FixtureSetup_InfiniteLoop); TestRunner.RunTestFixture(type, ref output, ref totalTests, ref failedTests, ref failedChecks); CheckEqual(2, totalTests); CheckEqual(2, failedTests); CheckEqual(1, failedChecks); CheckNotNull(output); } public void OnFixtureTeardown() { string output = ""; int totalTests = 0, failedTests = 0, failedChecks = 0; Type type = typeof(FixtureTeardown_InfiniteLoop); TestRunner.RunTestFixture(type, ref output, ref totalTests, ref failedTests, ref failedChecks); CheckEqual(2, totalTests); CheckEqual(0, failedTests); CheckEqual(1, failedChecks); CheckNotNull(output); } public void OnTestSetup() { string output = ""; int totalTests = 0, failedTests = 0, failedChecks = 0; Type type = typeof(TestSetup_InfiniteLoop); TestRunner.RunTestFixture(type, ref output, ref totalTests, ref failedTests, ref failedChecks); CheckEqual(2, totalTests); CheckEqual(2, failedTests); CheckEqual(2, failedChecks); CheckNotNull(output); } public void OnTestTeardown() { string output = ""; int totalTests = 0, failedTests = 0, failedChecks = 0; Type type = typeof(TestTeardown_InfiniteLoop); TestRunner.RunTestFixture(type, ref output, ref totalTests, ref failedTests, ref failedChecks); CheckEqual(2, totalTests); CheckEqual(2, failedTests); CheckEqual(2, failedChecks); CheckNotNull(output); } public void OnDataRowSetup() { string output = ""; int totalTests = 0, failedTests = 0, failedChecks = 0; Type type = typeof(DataRowSetup_InfiniteLoop); TestRunner.RunTestFixture(type, ref output, ref totalTests, ref failedTests, ref failedChecks); CheckEqual(6, totalTests); CheckEqual(6, failedTests); CheckEqual(3, failedChecks); //for 3 data rows CheckNotNull(output); } public void OnDataRowTeardown() { string output = ""; int totalTests = 0, failedTests = 0, failedChecks = 0; Type type = typeof(DataRowTeardown_InfiniteLoop); TestRunner.RunTestFixture(type, ref output, ref totalTests, ref failedTests, ref failedChecks); CheckEqual(6, totalTests); CheckEqual(0, failedTests); CheckEqual(3, failedChecks); //for 3 data rows CheckNotNull(output); } } #endregion [DataDriven("ExcelReaderTest.xls", "Numbers")] public class DataRowSetupTeardown_Fixture : TestFixture { double value = 0; double[] values = { 1, 1.25, -50 }; public override void DataRowSetup() { if (value != 0) throw new Exception(); value = double.Parse(DataRow["Numbers"]); } public void Test_1() { CheckEqual(values[DataRow.VirtualRowNumber], value); } public override void DataRowTeardown() { value = 0; } } } }