In this post we will learn how to run Unit Tests from Visual Studio using NUnit. We will be covering these points:

  • Install NUnit 3
  • Create our first NUnit 3 test
  • Run the test from Visual Studio

Installation of NUnit 3

We can install NUnit as a NuGet package:

Install Nunit using the NuGet package manager

As we see in the image we need to install these 3 packages:

  • NUnit: it contains the actual NUnit framework.
  • NUnit.Console: this package is kind of optional. It contains a console that will allow us to run the tests.
  • NUnit3TestAdapter: this package will allow us to run the defined test from Visual Studio. So, if we are happy running the tests using the console, we are covered with the NUnit.Console package and we don´t need to install this one.  Since I intend to describe in this post how to run the test from inside Visual Studio I ´ll go ahead and install it.

Writing our first NUnit test

Let´s imagine we are asked to implement a Cube class that allow us to create cubes by specifying the length of their edge. We must also implement a getVolume method that returns the volume of the Cube:

long getVolume()

Writing our CubeTest Class

In good TDD tradition we will start by writing the test before we implement the actual functionality.

We will create a class containing our tests:

[TestFixture]
namespace Test_TDD1
{
    [TestFixture]
    public class CubeTest
    {
    }
}

We use the TestFixture attribute to indicate that CubeTest is a test class.

Now we are going to write our test method for the getVolume feature. We use the Test attribute to indicate that our TestCubeVolume method is a a test method:

[Test]
public void TestCubeVolume()
{
	Cube cube = new Cube(3);
	long volume = cube.getVolume();

	Assert.That(27, Is.EqualTo(volume));
}

In our test method we´ll create a Cube object and we´ll specify an edge length of 3: therefore, we know that our getVolume method must return a result of 27.

In order to verify that, we use this assert line:

Assert.That(27, Is.EqualTo(volume));

If the volume calculated using the getVolume() function is equal to 27 the test will pass, otherwise it will fail.

Notice that in previous versions of NUnit we had different types of Assert methods:

  • Assert.AreEqual(expected, actual): it checks if the expected value is equal to the actual value.
  • Assert.IsTrue(bool): Checks if bool can be evaluated to True.
  • Assert.IsNull(object) : Checks if object is null.

…  and many more. In NUnit 3 we use a slightly different syntax (although the old one is still available): an Assert.That method  which takes constraint objects as an argument.

At this point our test will not even compile, since we have yet to implement a Cube class with the required getVolume method. Let´s solve that.

Implementing the required functionality

We implement a Cube class with a getVolume method:

namespace Figures
{
    public class Cube
    {
        long edge { get; set; }

        public Cube(long edge)
        {
            this.edge = edge;
        }

        public long getVolume()
        {
            return edge * edge * edge;
        }
    }
}

Now we need to add to our test class the reference to the Cube class. In the end our test class looks like this:

using Figures;
using NUnit.Framework;

namespace Test_TDD1
{
    [TestFixture]
    public class CubeTest
    {
        [Test]
        public void TestCubeVolume()
        {
            Cube cube = new Cube(3);
            long volume = cube.getVolume();

            Assert.That(27, Is.EqualTo(volume));
        }
    }
}

Running NUnit 3 tests from Visual Studio

As we saw before, in order to run NUnit 3 tests from visual Studio we need to install the NUnit3TestAdapter package.

Then we can access the test by going to Test > Run which will display different options like run all tests, run only the ones that failed, repeat the last run, etc.

Run all tests from Visual Studio

Running the test results in a Test Explorer window we see displayed the tests that were run and their results: failed or passed.

Thanks for reading. If you found this post useful you can subscribe to my blog to be notified of new posts.

Advertisements