using System; using System.Collections.Generic; using System.Reflection; using UnitTestSharp; namespace TestsForUnitTestSharp { public class TestSelectionTests : TestFixture { [IgnoreFixture] public class SampleFixture : TestFixture { public static int SetupRuns; public override void FixtureSetup() { ++SetupRuns; } public void Plain() { } [Thorough] public void Slow() { } } [IgnoreFixture] [Thorough] public class SlowFixture : TestFixture { public static int SetupRuns; public override void FixtureSetup() { ++SetupRuns; } public void Anything() { } } public static MethodInfo Method(Type fixture, string name) { return fixture.GetMethod(name); } public class FromArgumentsTests : TestFixture { public void NoArguments_LeavesOutThoroughTests() { var selection = TestSelection.FromArguments(new string[0]); CheckFalse(selection.Includes(typeof(SampleFixture), Method(typeof(SampleFixture), "Slow"))); } public void TheFlag_IncludesThoroughTests() { var selection = TestSelection.FromArguments(new[] { "Tests.exe", "--thorough" }); Check(selection.Includes(typeof(SampleFixture), Method(typeof(SampleFixture), "Slow"))); } public void TheFlagInAnotherCase_IncludesThoroughTests() { var selection = TestSelection.FromArguments(new[] { "--THOROUGH" }); Check(selection.Includes(typeof(SampleFixture), Method(typeof(SampleFixture), "Slow"))); } public void ASkipPattern_LeavesOutTestsItMatches() { var selection = TestSelection.FromArguments(new[] { "--skip=SampleFixture.Plain" }); CheckFalse(selection.Includes(typeof(SampleFixture), Method(typeof(SampleFixture), "Plain"))); } public void ASkipPatternInAnotherCase_LeavesOutTestsItMatches() { var selection = TestSelection.FromArguments(new[] { "--SKIP=SampleFixture.Plain" }); CheckFalse(selection.Includes(typeof(SampleFixture), Method(typeof(SampleFixture), "Plain"))); } public void SeveralSkipPatterns_LeaveOutWhatEitherMatches() { var selection = TestSelection.FromArguments( new[] { "--thorough", "--skip=SampleFixture.Plain$", "--skip=SampleFixture.Slow$" }); CheckFalse(selection.Includes(typeof(SampleFixture), Method(typeof(SampleFixture), "Plain"))); CheckFalse(selection.Includes(typeof(SampleFixture), Method(typeof(SampleFixture), "Slow"))); } public void SeveralSkipPatterns_KeepWhatNeitherMatches() { var selection = TestSelection.FromArguments( new[] { "--thorough", "--skip=SampleFixture.Slow$", "--skip=Nothing" }); Check(selection.Includes(typeof(SampleFixture), Method(typeof(SampleFixture), "Plain"))); Check(selection.Includes(typeof(SlowFixture), Method(typeof(SlowFixture), "Anything"))); } public void OtherArguments_AreIgnored() { var selection = TestSelection.FromArguments(new[] { "--times", "--skip", "thorough" }); Check(selection.Includes(typeof(SampleFixture), Method(typeof(SampleFixture), "Plain"))); } public void ABadSkipPattern_Throws() { CheckThrow(typeof(ArgumentException)); TestSelection.FromArguments(new[] { "--skip=(" }); } public void Null_Throws() { CheckThrow(typeof(ArgumentNullException)); TestSelection.FromArguments(null); } } public class NameOfTests : TestFixture { public void IsTheFixtureNamespaceAndNestedTypesThenTheMethodSeparatedByDots() { CheckEqual("TestsForUnitTestSharp.TestSelectionTests.SampleFixture.Plain", TestSelection.NameOf(typeof(SampleFixture), Method(typeof(SampleFixture), "Plain"))); } } public class IncludesTests : TestFixture { public void AnOrdinaryTest_IsIncluded() { var selection = new TestSelection(thorough: false, skipPattern: null); Check(selection.Includes(typeof(SampleFixture), Method(typeof(SampleFixture), "Plain"))); } public void AThoroughTest_IsLeftOut() { var selection = new TestSelection(thorough: false, skipPattern: null); CheckFalse(selection.Includes(typeof(SampleFixture), Method(typeof(SampleFixture), "Slow"))); } public void ATestOfAThoroughFixture_IsLeftOut() { var selection = new TestSelection(thorough: false, skipPattern: null); CheckFalse(selection.Includes(typeof(SlowFixture), Method(typeof(SlowFixture), "Anything"))); } public void AThoroughTest_IsIncludedWhenThorough() { var selection = new TestSelection(thorough: true, skipPattern: null); Check(selection.Includes(typeof(SampleFixture), Method(typeof(SampleFixture), "Slow"))); Check(selection.Includes(typeof(SlowFixture), Method(typeof(SlowFixture), "Anything"))); } public void ATestMatchingTheSkipPattern_IsLeftOut() { var selection = new TestSelection(thorough: true, skipPattern: "SampleFixture"); CheckFalse(selection.Includes(typeof(SampleFixture), Method(typeof(SampleFixture), "Plain"))); } public void ATestNotMatchingTheSkipPattern_IsIncluded() { var selection = new TestSelection(thorough: true, skipPattern: "Nothing"); Check(selection.Includes(typeof(SampleFixture), Method(typeof(SampleFixture), "Plain"))); } } public class FilterTests : TestFixture { public void KeepsTheIncludedMethodsInOrder() { var selection = new TestSelection(thorough: false, skipPattern: null); var methods = new List { Method(typeof(SampleFixture), "Slow"), Method(typeof(SampleFixture), "Plain"), }; IList kept = selection.Filter(typeof(SampleFixture), methods); CheckEqual(1, kept.Count); CheckEqual("Plain", kept[0].Name); } } public class RunnerTests : TestFixture { public void RunTestFixture_RunsOnlyTheIncludedTests() { int totalTests = 0, failedTests = 0, failedChecks = 0; TestRunner.RunTestFixture(typeof(SampleFixture), output => { }, ref totalTests, ref failedTests, ref failedChecks, timings: null, new TestSelection(thorough: false, skipPattern: null)); CheckEqual(1, totalTests); } public void RunTestFixture_WithNoSelection_RunsEveryTest() { int totalTests = 0, failedTests = 0, failedChecks = 0; TestRunner.RunTestFixture(typeof(SampleFixture), output => { }, ref totalTests, ref failedTests, ref failedChecks); CheckEqual(2, totalTests); } public void RunTestFixture_WithNothingIncluded_SkipsTheFixtureSetup() { int totalTests = 0, failedTests = 0, failedChecks = 0; SlowFixture.SetupRuns = 0; TestRunner.RunTestFixture(typeof(SlowFixture), output => { }, ref totalTests, ref failedTests, ref failedChecks, timings: null, new TestSelection(thorough: false, skipPattern: null)); CheckEqual(0, totalTests); CheckEqual(0, SlowFixture.SetupRuns); } public void RunTestFixture_WithSomethingIncluded_RunsTheFixtureSetup() { int totalTests = 0, failedTests = 0, failedChecks = 0; SampleFixture.SetupRuns = 0; TestRunner.RunTestFixture(typeof(SampleFixture), output => { }, ref totalTests, ref failedTests, ref failedChecks, timings: null, new TestSelection(thorough: false, skipPattern: null)); CheckEqual(1, SampleFixture.SetupRuns); } } } }