Thursday, August 19, 2010

TestCaseAttribute a friend in NUnit

Most of us are already familiar with the NUnit. I hope we all have written lots and lots of test cases to check our code. Among many attributes of NUnit, one of my favourite is a  'TestCaseAttribute'.

As quoted on Nunit.org "TestCaseAttribute serves the dual purpose of marking a method with parameters as a test method and providing inline data to be used when invoking that method."

This attribute helps us to run few common test on a single go and also helps us to group thest test cases together.
eg. 
        /// <summary>
        ///A test for Divide
        ///</summary>
        [TestCase(12, 3, Result = 4)]
        [TestCase(12, 2, Result = 6)]
        [TestCase(12, 4, Result = 3)]        
        [TestCase(12, 0, ExpectedException = typeof(System.DivideByZeroException),
             TestName = "DivisionByZeroThrowsExceptionType")]
        [TestCase(12, 0, ExpectedExceptionName = "System.DivideByZeroException",
             TestName = "DivisionByZeroThrowsException")]      
        public double DivideTest(int x , int y)
        { 
            return Functions.Divide(x, y);
         }
 I hope we can all use this attribute and use this functionality to make our life lot easier.

No comments:

Post a Comment