Tuesday, March 08, 2011

Unit Testing with NUnit 2.5.9


Introduction

NUnit is a xunit based unit-testing framework written in C# for Microsoft.Net. It comes with a testing framework assembly and a test runner for displaying the test results.

Implementation

  1. Download NUnit framework from here and install it.

    image

  2. Add a Visual Studio Class Library Project and name it as Calculate.Test.

    image

  3. Add reference to the Project that need to test.
  4. Add reference to NUnit framework

    image

    The Solution structure will looks like this

    image

  5. Open CalculateTest.cs file and decorate the public class with TestFixture attribute. The TextFixture attribute compiled into an assembly when we build the project.

    Let’s try for a fail case

    image

  6. Run NUnit runner application from Windows Program Menu

    image

  7. Select Open Project from File menu and load the Calculate.Test.dll. The test runner will load the assembly and using reflection API’s looks for any type that has a TestFixture attribute on it. Once the test runner identifies the TestFixture class, it will create an instance of that class and executes all the methods decorated with Test attribute.

    Click the Run button to see the result.

    image

  8. Modify the test case as shown below to fix the test case.

    image

  9. Run the test will give the green signal.

    image

Thanks,
Bimal

No comments:

Post a Comment