using System; using System.Collections.Generic; using System.Reflection; using UnitTestSharp; 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() { } } #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, (outputString) => output += outputString, 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, (outputString) => output += outputString, 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, (outputString) => output += outputString, 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, (outputString) => output += outputString, ref totalTests, ref failedTests, ref failedChecks); CheckEqual(2, totalTests); CheckEqual(2, failedTests); CheckEqual(2, failedChecks); 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() { } } #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, (outputString) => output += outputString, 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, (outputString) => output += outputString, 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, (outputString) => output += outputString, 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, (outputString) => output += outputString, ref totalTests, ref failedTests, ref failedChecks); CheckEqual(2, totalTests); CheckEqual(2, failedTests); CheckEqual(2, failedChecks); CheckNotNull(output); } } #endregion [IgnoreFixture] public class TestProvideMethods : TestFixture { public override IList FindTestableMethods(ref int ignored) { ignored = 7; return new List() { GetType().GetMethod(nameof(ManuallyAdded)), }; } [IgnoreTest] public void ManuallyAdded() { } } public class FixtureProvidesMethods : TestFixture { public void SetsIgnored() { var fixture = new TestProvideMethods(); int ignored = 0; fixture.FindTestableMethods(ref ignored); CheckEqual(7, ignored); } public void FindsManuallyAdded() { var fixture = new TestProvideMethods(); int ignored = 0; var methods = fixture.FindTestableMethods(ref ignored); var expected = new List { typeof(TestProvideMethods).GetMethod(nameof(TestProvideMethods.ManuallyAdded)) }; CheckEqual(expected, methods); } } } }