NUnit, NUnit test adapter, Visual Studio 2015–simple example of using

Let’s see how to add supporting of NUnit to the Visual Studio 2015 project.  I will describe a simple example without unnecessary things.

At first step let’s create a simple console application with name  test_unit and add new class to it with name Class1 as shown below

image

Below is code of class Class1

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace test_nunit
{
    public class Class1
    {
        public bool isValidLogFileName(string logFileName)
        {
            if (!logFileName.EndsWith(".log"))
            {
                return false;
            }

            return true;
        }
    }
}

There is method isValidLogFileName which returns false if extension of file isn’t  end with “log” and false elsewhere.

At the second step we have to create library which will tests our class. Thus create another project in our solution with test project name + “.UnitTests” as shown below

image

Create class that will be used for testing with name of tested class + Tests. Our class is Class1 so the class for testing should be Class1Tests and code to the created class should be as shown below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NUnit.Framework;


namespace test_nunit.UnitTests
{
    [TestFixture]
    public class Class1Tests
    {
        [Test]
        public void IsValidFileName_BadExtension_ReturnsFalse()
        {
            Class1 cl = new Class1();
            bool result = cl.isValidLogFileName("some.foo");

            Assert.False(result);
        }

        [Test]
        public void IsValidFileName_OkExtension_ReturnsTrue()
        {
            Class1 cl = new Class1();
            bool result = cl.isValidLogFileName("some.log");

            Assert.True(result);

        }
    }
}

At the third step we have to install NUnit trough Visual Studio package manager using command

Install-Package NUnit

After this you will find in your project hierarchy directories dll files which realize NUnit functionality as shown below

image

In theory package manager should add reference to one of the dll automatically but in my case this hasn’t happened so I just added reference to net45 nunit.framework.dll manually (you should add reference to the nunit.framework.dll only for library used for testing). Also we have to add reference to the project that will be tested (without that we can’t create object of tested class).

image

At the fourth step we have to install NUnit Test Adapter. This adapter allow us run test in Visual Studio. Also we can use difference approaches including commercial products to achieve that but this is very easy as I think. To install NUnit Test Adapter go to “Tools \ Extensions and Updates”. Then select “online extensions” and find NUnit Test Adapter as shown below and then download and install this one.

image

After we need to restart Visual Studio in order for the changes to take effect.

And finally we can run our tests. Just go to “Test / Run / All tests” or press Cntrl+R, A. We will see that all test are passed.

image

Let’s make bug in our isValidLogFileName function. Just remove  ! before logFileName. Now our tests are not passed.

image

Take a look at  the names of test methods used by NUnit framework. The name should be “tested function name”_”Scenario under test”_”Expected behavior”.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s