using System; using UnitTestSharp; namespace TestsForUnitTestSharp { public class TestTimingsTests : TestFixture { public class IsRequestedTests : TestFixture { public void TheFlag_IsRequested() { Check(TestTimings.IsRequested(new[] { "--times" })); } public void TheFlagAmongOthers_IsRequested() { Check(TestTimings.IsRequested(new[] { "Tests.exe", "--other", "--times" })); } public void TheFlagInAnotherCase_IsRequested() { Check(TestTimings.IsRequested(new[] { "--TIMES" })); } public void NoArguments_IsNotRequested() { CheckFalse(TestTimings.IsRequested(new string[0])); } public void OtherArguments_AreNotRequested() { CheckFalse(TestTimings.IsRequested(new[] { "Tests.exe", "--time", "times" })); } public void Null_Throws() { CheckThrow(typeof(ArgumentNullException)); TestTimings.IsRequested(null); } } public class ReportTests : TestFixture { public void NothingRun_HasOnlyTheHeading() { string[] lines = new TestTimings().Report().Split(new[] { "\r\n", "\n" }, StringSplitOptions.None); CheckEqual("Fixture and test times, in milliseconds:", lines[0]); CheckEqual("", lines[1]); CheckEqual(2, lines.Length); } public void ListsTheSlowestFixtureFirst() { var timings = new TestTimings(); timings.AddFixture("Quick", 1); timings.AddFixture("Slow", 5); string report = timings.Report(); CheckGreater(report.IndexOf("Quick"), report.IndexOf("Slow")); } public void ListsTheSlowestTestOfAFixtureFirst() { var timings = new TestTimings(); timings.AddTest("Fixture", "Quick", 1); timings.AddTest("Fixture", "Slow", 5); string report = timings.Report(); CheckGreater(report.IndexOf("Quick"), report.IndexOf("Slow")); } public void ShowsEachTestWithItsTime() { var timings = new TestTimings(); timings.AddTest("Fixture", "Test", 12.34); CheckEqualRegex(@"12\.3 +Test", timings.Report()); } public void ShowsHowManyTestsAFixtureRan() { var timings = new TestTimings(); timings.AddTest("Fixture", "First", 1); timings.AddTest("Fixture", "Second", 1); CheckEqualRegex(@"Fixture +\(2 tests\)", timings.Report()); } public void AFixtureTime_CountsMoreThanItsTests() { var timings = new TestTimings(); timings.AddTest("Fixture", "Test", 1); timings.AddFixture("Fixture", 9); CheckEqualRegex(@"9\.0 +Fixture", timings.Report()); } public void WithoutAFixtureTime_TheFixtureTakesTheSumOfItsTests() { var timings = new TestTimings(); timings.AddTest("Fixture", "First", 1); timings.AddTest("Fixture", "Second", 2); CheckEqualRegex(@"3\.0 +Fixture", timings.Report()); } public void ADecimalCommaCulture_StillWritesAPoint() { var timings = new TestTimings(); timings.AddTest("Fixture", "Test", 1.5); System.Threading.Thread thread = System.Threading.Thread.CurrentThread; System.Globalization.CultureInfo saved = thread.CurrentCulture; try { thread.CurrentCulture = new System.Globalization.CultureInfo("de-DE"); CheckEqualRegex(@"1\.5 +Test", timings.Report()); } finally { thread.CurrentCulture = saved; } } } public class RunnerTests : TestFixture { [IgnoreFixture] class FauxFixture : TestFixture { public void First() { } public void Second() { } [Parameterized("Values")] public void Third(int value) { } public static readonly int[] Values = { 1, 2 }; } public void RunTestFixture_RecordsEachTestAndTheFixture() { var timings = new TestTimings(); int totalTests = 0, failedTests = 0, failedChecks = 0; TestRunner.RunTestFixture(typeof(FauxFixture), output => { }, ref totalTests, ref failedTests, ref failedChecks, timings); string report = timings.Report(); CheckEqualRegex(@"FauxFixture +\(4 tests\)", report); CheckEqualRegex("First", report); CheckEqualRegex("Second", report); CheckEqualRegex(@"Third\[0\]", report); CheckEqualRegex(@"Third\[1\]", report); } public void RunTestFixture_WithoutTimings_RunsTheSameTests() { int totalTests = 0, failedTests = 0, failedChecks = 0; TestRunner.RunTestFixture(typeof(FauxFixture), output => { }, ref totalTests, ref failedTests, ref failedChecks); CheckEqual(4, totalTests); } } } }