Beginning Unit Testing Tutorial in C# with NUnit (Part 1)

So how do you get started with Unit Testing? Well, you need a Unit Testing framework and some basic know how. This article will help you with both.

NUnit is a commonly used testing framework for C#. Well, NUnit has a guide  but I found it incomplete for a newbie, though perfectly acceptable for an experienced developer.
http://nunit.org/index.php?p=quickStart&r=2.6

Lets assume that you have a project with this class:

namespace NUnitBasics
{
    public class SimpleAddSubtractTool
    {
        public int Add(int value1, int value2)
        {
            return value1 + value2;
        }

        public int Subtract(int value1, int value2)
        {
            return value1 - value2;
        }
    }
}

You need to test these methods and find bugs in them and despite how simple they appear, there are bugs in the above methods.

Step 1 – Install NUnit

  1. Go to this url and download the latest version of NUnit.
    http://nunit.org/?p=download
  2. Run the installer.

Step 2 – Create an NUnit testing project in Visual Studio

You can do this manually, as described below, or you can download my templates: NUnit Project Template for Visual Studio

Part 1 – Creating the Project

  1. In Visual Studio, open the solution that holds the project and code that needs to be tested.
  2. Right-click on the solution and choose Add | New Project.
  3. In the tree on the left, select Visual C# | Windows.
    Note: You would think you would select a Test project, but that is for the testing framework built into Visual Studio, MSTest.
  4. Click to highlight Class Library.
  5. Provide a project name.
    Important: It is usually best practice to give the test project the same name as the project it is test only with the word “test” at the end. For example, a project called MyProject would have a corresponding test project called MyProjectTests.
  6. Click Ok.

Part 2 – Configuring the project

  1. Click Reference | Add Reference.
  2. Click the first tab: .NET
  3. Find and add nunit.framework and nunit.mocks.
  4. Click Reference | Add Reference (again).
  5. Choose Projects.
  6. Select the project that is being tested.
  7. Click Ok.
  8. Delete the Class1.cs as it is not used.

Step 3 – Start your first Unit Test

You can add a class and manually change it to be an NUnit test class as described below, or you can download my template: NUnit Item Template for Visual Studio

Part 1 – Create the class file

  1. Right-click on your project and choose Add | Class.
  2. Name the class after the class you are going to test. For example, a class called SimpleAddSubtractTool would have a corresponding test class called SimpleAddSubtractToolTests.
  3. Click OK.

Part 2 – Convert to an NUnit class file

  1. Add a using reference to NUnit.Framwork.
  2. Add the [TestFixture] attribute above the class name.
  3. Add Setup and Tear down attributes and methods if needed.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;

namespace NUnitBasicsTests
{
    /// <summary>
    /// A test class for ...
    /// </summary>
    [TestFixture]
    public class SampleTest
    {
        #region Setup and Tear down
        /// <summary>
        /// This runs only once at the beginning of all tests and is used for all tests in the
        /// class.
        /// </summary>
        [TestFixtureSetUp]
        public void InitialSetup()
        {

        }

        /// <summary>
        /// This runs only once at the end of all tests and is used for all tests in the class.
        /// </summary>
        [TestFixtureTearDown]
        public void FinalTearDown()
        {

        }

        /// <summary>
        /// This setup funcitons runs before each test method
        /// </summary>
        [SetUp]
        public void SetupForEachTest()
        {
        }

        /// <summary>
        /// This setup funcitons runs after each test method
        /// </summary>
        [TearDown]
        public void TearDownForEachTest()
        {
        }
        #endregion
    }
}

You are now ready to create your first test.

Note: For this basic class you can actually delete the Setup and Tear down region as it won’t be used, however, it is good to know how to use it when you get started.

Beginning Unit Testing Tutorial in C# with NUnit (Part 2)

Return to C# Unit Test Tutorial

2 Comments

  1. […] For Nunit Fundamentals, you can refer this link. […]

  2. Alan says:

    Thanks! This was very helpful!

Leave a Reply

How to post code in comments?

*