Since a lot of my future posts are going to be about NUnit, I want to make sure that you all have the basics of NUnit covered before we start talking in depth about the framework. In this post, I am going to go through how to build your first unit test.

I am going to make some assumptions in this post, primarily that you have the basic understanding of .NET development and that you've used Visual Studio before. If that is all new to you, check out Microsoft's site on Visual Studio for getting started - Getting Started

Prerequisites:

  • .NET Framework - Download
  • IDE (Visual Studio and Visual Studio Code are what I use) - Download
  • MSBuild (Only needed if you don't have Visual Studio) - Download

Before we begin, I want to point out the NUnit landing page on GitHub - NUnit. This is going to be where we get all the necessary code, executables, code samples, and documentation for using NUnit. I will be referencing their GitHub frequently for documentation, and it's just a good place to have bookmarked for when you are developing with NUnit.


Method Under Test

Now let's get started! In this example, I am going to have two projects. One project that holds the code and one that holds the tests.

  • BlogSamples
  • BlogSamples.NUnit

I am going to use the classic programming example of 'Hello, World.' In BlogSamples, we have a class HelloWorld and a method Hello.

public static string Hello(string greeting)  
{
    return $"Hello, {greeting}.";
}

Hello is a simple method that just takes in a string, mutates it, and returns the mutated string. So Hello("Bojangles") would return, Hello, Bojangles.

Now to set up our tests for Hello.


Installing NUnit

In order to write our NUnit tests for Hello, we need to add NUnit framework to our BlogSamples.NUnit project. The easiest way is going to be using Nuget. All the ways to install NUnit are listed here - Install

Inside Visual Studio, go to Tools -> NuGet Package Manager -> Package Manager Console. You will see the package manager console open at the bottom of the development window. In the window, type Install-Package -Project BlogSamples.NUnit NUnit and hit return. This will grab the latest stable build of NUnit and install it to our BlogSamples.NUnit project. You will see Successfully installed 'NUnit 3.8.1' to BlogSamples.NUnit in the console if it successfully installed.

Under references, you should see that you now have a reference to nunit.framework. This will be what we will be using to build up our tests.


Writing the Test

Now that we have our reference to NUnit, we need to add a project reference to the BlogSamples project. This will allow us to instantiate our HelloWorld class to test it. I'll point you to this page if you don't know how to add a reference - Add Reference. When we have all the references we need, we can start writing the tests for our Hello method.

When I am writing unit tests, I like to make a class with the same name of the class that we are going to be testing. In BlogSamples, we have our Hello method in the HelloWorld class. So we will create the same Hello method in the HelloWorld class inside of the BlogSamples.NUnit project. Using this naming convention makes it very easy for discovering where the unit tests are for a particular class.

Once we create our Hello test method, we want to add the using statement for NUnit - using Nunit.Framework. Then we will declare our HelloWorld class as an NUnit TestFixture. We do this by adding the [TestFixture] attribute to the class. Now we have something that looks like this...

using NUnit.Framework;

namespace BlogSamples.NUnit  
{
    [TestFixture]
    public class HelloWorld
    {
    }
}

Now that we have laid out the structure of the test class, we can add our first test case. Our first iteration of our test might look like this...

[Test]
public void Hello()  
{
    string greeting = BlogSamples.HelloWorld.Hello("get-testy");
    Assert.That(greeting.Equals("Hello, get-testy.", StringComparison.Ordinal));
}

For those that have never seen an NUnit test, it can seem like a lot is going on in this method. Let's break down what comprises an nunit test.

  1. The first thing is that we decorate the test method with the [Test] attribute. It is one of many attributes that you can put on a method to classify it as a test. In later posts, I will go over all the different ways that you can define tests. For now, the most basic way to create a test is to use the [Test] attribute. More info can be found here on Attributes.
  2. Since we are testing the Hello method in the HelloWorld class, we actually want to call that method with something that we define. This way we know exactly what the outcome with be. In this instance, we are sending in "get-testy" to Hello, so we would expect our method to return "Hello, get-testy.".
  3. After we call the Method Under Test, we want to verify the results from method. We do this in the Assertion. In NUnit3, they have changed their Assertion model to be constraint based. This is in favor of their classic model, where in our case we might use Assert.AreEqual("Hello, get-testy.", greeting). Using the constraint based model gives a lot more freedom to do what we want inside the assertions. More info can be found here on Assertions.

Running the Test

There are a couple ways that we can run NUnit tests.

  1. The Visual Studio NUnit3 Adapter - Adapter
  2. NUnit3 Console - Console
  3. NUnit3 GUI - GUI

Depending on your needs, there will always be a better way to run your tests.

  • If you are going to be actively writing tests, it is probably the best to use the Visual Studio adapter. That way when you are writing your code and your tests, you don't need to leave your IDE in order to run them.
  • If you are going to be running your tests nightly, or during your build process, you are going to want to use the nunit console.
  • If you are going to want to run all existing tests, not making any changes, against your code changes, you might use the GUI. **The NUnit3 GUI is still in preview, so I might wait to use this until it gets fully release.

Let's just use the Visual Studio adapter for this example. We can install the extension into our Visual Studio environment by going to Tools -> Extension Manager. Search for NUnit 3.0 Test Adapter and click install. I had to restart my Visual Studio in order for the installation to complete, it might be different for you. Just follow the directions that it gives you.

Once you have the extension installed, we want to open up the Test Explorer. If you don't see it, go to Test -> Windows -> Test Explorer. By default, when you first open Visual Studio, it won't have any tests loaded. Once you build your code, you should see the new test we created. Now it is as simple as right-clicking on the Hello test, and selecting Run Selected Tests.


Now we should have everything set up in our development environment. We can start getting into more advanced topics. Let me know if this was helpful, too in-depth, not enough in-depth, etc. Please share your thoughts!

© 2018. All Rights Reserved.

Proudly published with Ghost