This article is about NUnit. Let us now try to understand the following concepts.

What NUnit is

NUnit is a Unit Testing Framework in .NET that allows us to create automated tests. There are many benefits of creating automated tests. Let's talk about some of the benefits that are very important.
Producing higher-quality code decreases the cost of defects in a project. If defects are found early they cost less to fix.

Note: The following are the various automation tools of unit testing frameworks in various programming languages.
Downloading and setting up NUnit

You can download NUnit from http://www.nunit.org/

Once your download is completed, you can unzip the folder and run that application to use it.

Creating a Class Library Project

Let us now create a simple test class using C# that adds two numbers and test it. Use the following procedure to create a Test Class in Visual Studio using C#.

Step 1:
Create a Class Library Project

I am using Visual Studio 2012 Professional.

To create a Class Library project first go to File -> New -> Project then select Visual C# in the installed templates then select Class Library. Provide an appropriate name (I used IntroNUnitTest) then click “OK”.

new project


Figure: Creating Class Library

Once you click the “OK” button, you will be able to see your project in the Solution Explorer (in the top corner of the right side of Visual Studio).

Step 2: Add a reference to your project

Now you should add the reference of NUnit to your project. This can be done with the following procedure.

Right-click on the reference then select Add reference-> Browse then select nunit.framework.dll then click “OK”.

Step 3: Creating a Test Class

Right-click on the project then select Add -> Class then provide a name (I used MyNUnitTestClass) then click “Add”.

add new item

Figure: Adding a Test Class to the project

After you click Add you will see a class as in the following figure.

visual studio test case

Figure: Class

After your class has been created, you should add using NUnit.Framework in your class.

The following are some important conditions to follow:
introunit test

Figure: Test class with 4 tests

In this example, I am just trying to compare the two variables x and y. I assigned a value of 20 to each variable. I have written a total of 4 tests (PositiveTest, NegativeTest, ExpectedExceptionTest and NotImplementedException).

Running Tests

After writing all the tests in your test class, we should run all those tests to check whether or not all the tests are ing. In order to run the tests go to the NUnit folder then select NUnit Application (.exe) and double-click on it then select File-> Open Project then select your project (in my case it is IntroNUnitTest.dll) then click Run.

unittest

Figure: NUnit application for running tests.

Then after you click on the Run button you'll see the numbers of tests that have ed, failed and didn't execute.

test cases

Figure: Test Results: 2 ed, 1 Failed and 1 didn't run (because we used the Ignore option in the Test attribute).

If you want to run only a specific test then you can right-click inside the test that you want to run and select Run test(s).

Other Tools

We can also use other tools to run the tests, they are: