Learn Basic Unit Testing With Visual Studio 2012 and Nunit

This article is based on the my session in January of 2014 at the C# Corner Delhi Chapter. Whoever missed it or wants a review of the session, this article will be a guide for the basics of a unit testing walkthrough.

With this article we’ll go step-by-step to set up the environment and a few basic test scenarios. To start hands-on you need the following installed on your machine.

Download

Visual studio

If you already have Visual Studio installed then you need to install the Nuget Extension Manager extension of Visual Studio.

Nuget Extension Manager

Now you’re ready to install the libraries that are available on the Official Nuget server. For this exercise we’ll be using the Nunit testing framework. The Nuget manager will install the Nunit Framework and Nunit test adapter from the Visual Studio itself. So as in the old days you don’t need to go to the web and download the Nunit package and then add it to Visual Studio. Nuget is the new RAD tool that won’t let you go off the Visual Studio Interface.

So now we have the tools so let’s get started:

1. Launch Visual Studio and create a new Class Library project.

create new Class Library project

2. Now let’s write some logical methods for the example, just simple numeric and string operations.
 

public class LogicalClass

{

    /// <summary>

    /// A sample divide operation

    /// </summary>

    /// <param name="a">Any positive integer</param>

    /// <param name="b">A non zero value</param>

    /// <returns>result of divide operation as decimal</returns>

    public decimal Divide(int a, int b)

    {

        if (b == 0)

        {

            throw new DivideByZeroException("Lets ensure.");

        }

 

        return (a / b);

    }

 

    /// <summary>

    /// The concat two names.

    /// </summary>

    /// <param name="name1"> The name 1. </param>

    /// <param name="name2"> The name 2. </param>

    /// <returns> The <see cref="string"/>. </returns>

    public string ConcatTwoNames(string name1, string name2)

    {

        return string.Format("{0}.{1}", name1, name2);

    }

}

 
Now since this is a Class Library and to ensure that my logic is working fine without debugging, we’ll write some more code that will unit test the methods.

3. Let’s proceed with creating a new project as a Class Library called DemoLibrary.UnitTests.

Class library

Now before we start writing the unit tests for the LogicalClass we would need the Testing framework. You can also use the same Microsoft.Tests but I’m quite fond of Nunit. But there’s no such difference which framework to use, if you’re interested in comparing these two framework capabilities then you can visit the comparison list:
 

Comparing the MSTest and Nunit Frameworks

4. To install the Nunit framework right-click on the unit test project and select Manage Nuget package. This will open the window showing the online Packages available. Search for Nunit and you’ll see the results shown as in the following image.

Nunit

Install the Nunit and Nunit Test Adapter for VS2012 and 2013. Why do we need a test adapter? To find and run the Unit tests the Nunit test adapter will do the work for you. You can also install the extension of the Nunit Test Adapter. Go to Extension Manager then Search for “Nunit Test Adapter” and install.  Though Visual Studio can also find tests and you can run them by going to the Test  menu and selecting "Windows" –> "Test Explorer".

 
Test Explorer

Now the setup is finished, so start writing the Unit tests. Let’s add a class named LogicalClassTests; this is a naming convention you must follow since this will make your tests easily identifiable just by the name of the test class.

Now the test class will have the following things that you need for learning how to make a test looking perfect and runnable.

name of the Test class

TestFixture Class Attribute: This attribute is to mark a class as a test suite that will contain tests.

Test Method Attribute: This attribute is to mark a method as a Unit test. Only methods with this attribute will be runnable by the test runner.

Setup: To initialize the object to be tested and initial values required for the test. In a future article we’ll learn how to Fake/Mock the dependencies like database, File System, or any other external system while writing tests. This will be part of the setup.

Act: Act is actually when you call the method that you’re going to test. So you provide the test values to the method and receive an output that will be tested against the expected result.

Assert: Assert is the statement that will check the expected values by the result.

Similarly, you’ll write the tests for all the positive scenarios of a method.


Similar Articles