diff --git a/CheatSheets/NUnitCheatSheet.cs b/CheatSheets/NUnitCheatSheet.cs index ec61485..d6f5709 100644 --- a/CheatSheets/NUnitCheatSheet.cs +++ b/CheatSheets/NUnitCheatSheet.cs @@ -13,27 +13,28 @@ namespace NUnitCheatSheet /// Provides the config for all TestFixture classes witin the same namespace. /// [SetUpFixture] - public class TestFixtureSetUp - { + public class TestFixtureSetUp + { /// /// SetUp; run once before all tests in a test fixture. /// Run once for each TestFixture in the same namespace. /// - [SetUp] + [SetUp] public void RunBeforeAnyTests() { - // Set up code here. + // Set up code here. } + /// /// TearDown; run once after all tests in a test fixture. /// Run once for each TestFixture in the same namespace. /// - [TearDown] + [TearDown] public void RunAfterAnyTests() { - // Clear up code here. + // Clear up code here. } - } + } /// /// Factory class for providing test data for tests with the TestCaseSource attribue. @@ -43,16 +44,16 @@ public class TestCaseDataFactory /// /// TestCaseSource tests can consume IEnumerable properties which return TestCaseData /// - public static IEnumerable TestCasesDataForTestCaseSourceTest + public static IEnumerable TestCasesDataForTestCaseSourceTest { - get - { - yield return new TestCaseData( 1, "1" ).Returns( 2 ); // Defines the test data and the expected return - yield return new TestCaseData( 2, "2" ).Returns( 4 ); - yield return new TestCaseData( 0, "a" ) + get + { + yield return new TestCaseData( 1, "1" ).Returns( 2 ); // Defines the test data and the expected return + yield return new TestCaseData( 2, "2" ).Returns( 4 ); + yield return new TestCaseData( 0, "a" ) .Throws(typeof(ArgumentException)); // Defines the test data and the expected throw exception - } - } + } + } } /// @@ -66,7 +67,7 @@ public class Test /// public static object[] CaseSourceTestData = { - new object[] { 1, 1.1m, "2.1" }, + new object[] { 1, 1.1m, "2.1" }, new object[] { 2, 2.2m, "4.2" }, new object[] { 3, 3.3m, "6.3" }, }; @@ -75,23 +76,26 @@ public class Test /// /// TestFixtureSetUp called once before any tests have been run in the same TestFixture /// - [TestFixtureSetUp] public void FixtureSetUp() - { + [TestFixtureSetUp] + public void FixtureSetUp() + { // Set up code here. } + /// /// TestFixtureTearDown called once after all tests have been run in the same TestFixture /// - [TestFixtureTearDown] public void FixtureTearDown() - { + [TestFixtureTearDown] + public void FixtureTearDown() + { // Clear up code here. } /// - /// SetsUp is called once before each Test within the same TestFxiture + /// SetsUp is called once before each Test within the same TestFixture /// [SetUp] public void SetUp() - { + { // Set up code here. // If this throws an exception no Test in the TestFixture are run. } @@ -99,9 +103,10 @@ [SetUp] public void SetUp() /// /// TearsDown is called once after each Test within the same TestFixture. /// - [TearDown] public void TearDown() - { - // Clear up code here. + [TearDown] + public void TearDown() + { + // Clear up code here. // Will not run if no tess are run due to [SetUp] throwing an exception } # endregion @@ -120,16 +125,16 @@ public void ExpectedExceptionAttributeTest() /// Explicit defines tests which are only run manualy. /// [Test, Explicit("Test has to be run explicitly")] - public void ExplicitAttributeTest() - { + public void ExplicitAttributeTest() + { } /// /// Ignore marks tests which are not run even if run manually /// [Test, Ignore("Ignore this test")] - public void IgnoreAttribueTest() - { + public void IgnoreAttribueTest() + { } /// @@ -137,7 +142,7 @@ public void IgnoreAttribueTest() /// [Test, MaxTimeAttribute(2000)] public void MaxtimeAttributeTest() - { + { } /// @@ -154,35 +159,36 @@ public void TimeOutTest() /// [Test, Platform("Mono"), Culture("en-GB")] public void PlatformAttributeTest() - { + { // Can also provide exclusion culture and platform // Test case is ignored if Platform or Cultrue can not be provided - } + } - /// t + /// /// Property: categorises the test witin the test output XML. /// [Test, Property("PropertyName", "Value")] public void PropertyAttributeTest() - {} + { + } /// /// Custom Property: categorises the test within the test output XML. /// [Test, CustomPropertyAttribute(CustomPropertyValue.One)] public void CustomPropertyAttributeTest() - {} + { + } #region Parameterized /// /// Combinatorial: The test is run with each combination of Values for each parameter /// - [Test, Combinatorial] public void CombinatorialAttributeTest ([Values(1,2,3)] int a, [Values("a", "b", "c")] string b) { - // Called 9 times with parameter pairs of: {1,a}, {1,b}, {1,c}, {2,a}, {3,b}.... + // Called 9 times with parameter pairs of: {1,a}, {1,b}, {1,c}, {2,a}, {3,b}.... } /// @@ -190,7 +196,7 @@ public void CombinatorialAttributeTest ([Values(1,2,3)] int a, [Values("a", "b", /// [Test] public void RandomAttributeTest( - [Random(1, 10, 2)] int value) + [Random(1, 10, 2)] int value) { // Called 2 times with a random integer between 1 and 10 Assert.That(value, Is.InRange(1,10)); @@ -201,8 +207,8 @@ public void RandomAttributeTest( /// [Test, Sequential] public void SequentialAttributeTest( - [Values(1,2,3)] int x, - [Values("A","B")] string s) + [Values(1,2,3)] int x, + [Values("A","B")] string s) { // Test runs for parameter pairs {1,A}, {2,B}, {3, null} } @@ -212,8 +218,8 @@ public void SequentialAttributeTest( /// [Test] public void RangeAttributeTest( - [Range(0.0, 1, 0.5)] decimal value) - { + [Range(0.0, 1, 0.5)] decimal value) + { // Test run for parameters, 0.0, 0.5, 1.0 Assert.That(value, Is.InRange(0m,1m)); } @@ -249,7 +255,7 @@ public decimal TestCaseSourceTest(int a, string b) [TestCase(3,3, Result=6)] public int TestCaseTest(int a, int b) { - return( a + b); + return( a + b); } #endregion @@ -259,7 +265,6 @@ public int TestCaseTest(int a, int b) [Test(), Category("Assertion based testing")] public void TestAssertions () { - // Equality Assert.AreEqual (true, true); Assert.AreNotEqual (true, false); @@ -348,7 +353,7 @@ public void TestAssertions () FileAssert.AreNotEqual (new FileInfo ("MyFile.txt"), new FileInfo ("MyFile2.txt")); FileAssert.AreNotEqual ("MyFile.txt", "MyFile2.txt"); FileAssert.AreNotEqual (new FileStream ("MyFile.txt", FileMode.Open), new FileStream ("MyFile2.txt", FileMode.Open)); - + // Utilities: will cause the test to stop immediatly and mark the test as: Assert.Pass (); @@ -367,7 +372,6 @@ public void TestAssertions () [Category("Constraint based testing")] public void TestConstraints () { - // Numerical Equality Assert.That (1, Is.EqualTo (1)); Assert.That (1, Is.Not.EqualTo (2)); @@ -397,7 +401,7 @@ public void TestConstraints () Assert.That (new double[] {1.0,2.0,3.0}, Is.EqualTo (new int[] {1,2,3})); Assert.That (new double[] {1.0,2.0,3.0}, Is.Not.EqualTo (new int[] {1,2,3, 4})); Assert.That (new double[] {1.0,2.0,3.0,4.0}, Is.EqualTo (new int[,] {{1,2}, {3,4}}).AsCollection); // Compare data and not collection (flattens a collection of collections) - + // Customer Comparer Assert.That( int.MinValue, Is.EqualTo(int.MaxValue).Using(new WhoCaresComparer())); @@ -436,7 +440,7 @@ public void TestConstraints () Assert.That(new MemoryStream(), Is.TypeOf(typeof(MemoryStream))); // Is type and not ancestor Assert.That(new object(), Is.AssignableFrom(typeof(MemoryStream))); // Can be assigned from Assert.That(new MemoryStream(), Is.AssignableTo(typeof(Stream))); // Can be assignable to - + Assert.That(new MemoryStream(), Is.InstanceOf()); // Is type or ancestor Assert.That(new MemoryStream(), Is.TypeOf()); // Is type and not ancestor Assert.That(new object(), Is.AssignableFrom()); // Can be assigned from @@ -460,7 +464,7 @@ public void TestConstraints () Assert.That( new List(){1,2,3}, Has.Member(1)); // Contains Assert.That( new List(){1,2,3}, Is.EquivalentTo( new List(){3,2,1})); // Same data without carring about the order Assert.That( new List(){1,2,}, Is.SubsetOf( new List(){3,2,1})); // All are cotained. - + // Property Constraint Assert.That( new List(), Has.Property("Count").EqualTo(0)); // Checks public property @@ -508,18 +512,19 @@ public void TestConstraints () Is.EqualTo(new List(){1,2,3})); } } + # region Elements consumed by the tests above public class WhoCaresComparer : IEqualityComparer { - public bool Equals(int b1, int b2) - { + public bool Equals(int b1, int b2) + { return true; - } + } - public int GetHashCode(int aNumber) - { + public int GetHashCode(int aNumber) + { return aNumber.GetHashCode(); - } + } } public class ReverseComparer : IComparer @@ -533,14 +538,14 @@ public int Compare (object a, object b) public enum CustomPropertyValue { - One, - Two, - } + One, + Two, + } [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)] public class CustomPropertyAttribute : PropertyAttribute { - public CustomPropertyAttribute( CustomPropertyValue value ) : base( "Custom", value.ToString() ) + public CustomPropertyAttribute( CustomPropertyValue value ) : base( "Custom", value.ToString() ) { } }