Monday, August 16, 2010

Unit Testing , NUnit and Assert

Unit Testing
Most of us are used to testing the software once it completes the software development cycle. And many of us have a perspective of testing being just bug-fixing. Testing the software at the very end results in error detection at the very end as well, which may result in costing us more than if we had known the issue at the early stage.

Unit testing is a method by which we can test the usability of the piece of our source code. It helps us to determine the function we have written is fit to be used.
Moreover, it also helps us to determince if the function we have written still works at the later date, hence, helping us in regression testing.

NUnit
NUnit is one of the many tools we can use for Unit Testing.
It is an open source product which can be downloaded from http://www.nunit.org

NUnit requires test cases and one or more test fixtures. Each test must contain at least one assertion.

Example

Lets assume we want to test this function
 public static int Divide(int x, int y)
        {
            return x / y;
        }

To test this function in NUnit , we are required to create a test fixture and then a test itself.

[TestFixture]
    public class FunctionsTest
    {
        [Test]
        public void DivideTest()
        {
            Assert.AreEqual(4, Divide(12, 3));
        }
    }

No comments:

Post a Comment