Building Real NUnit Tests

By James Kenny
3 min read

Table of Contents

Using NUnit tests is about testing different aspects of your application. So that as you make changes and add features you don't break anything. But it can be hard to get started with unit testing. Sometimes we are in too much of a rush to add testing to our applications if we don't take the time to add it in at the start but it doesn't have to be.

In this post I'm going to cover building some small real world tests to test parts of an application. It only takes 5 / 10 mins to do this.

Cloudstatus.eu is a web application I created for monitoring Cloud infrastructure we use to build services. It monitors Azure, AWS, Google Cloud, Github and more and sends alerts to your inbox or slack and more places.

To keep everything clean I've created a sample application that is a smaller version of Cloudstatus.eu and added some tests to it.

You can get the source here

If you are looking for how to add NUnit check out my blog post Your first NUnit Test

The Setup
  • NUnit_Demo.Web
  • Nunit_Demo.Data
  • Nunit_Demo.Tests

Nunit_Demo.Web is the MVC application.

Nunit_Demo.Data is a class library that get's called by the HomeController in the Nunit_Demo.Web application. This returns a list of providers.

Nunit_Demo.Tests is where the tests take place and run from.

In Nunit_Demo.Data we have a class ProviderQueries

    public List<Provider> GetProviders()
    {
        List<Provider> _records = new List<Provider>();

        _records.Add(AddProvider(1, "Azure", "Windows Azure", "All Ok", "Running Fine"));
        _records.Add(AddProvider(2, "AWS", "Amazon Web Services", "All Ok", "Running Fine"));


        return _records;
    }


    // This method will throw an expection to see the test failing.
    public List<Provider> GetProvidersNoDatabase()
    {
        List<Provider> _records = new List<Provider>();
        

        throw  new Exception("database missing");

        return _records;
    }

    public Provider AddProvider(int Id, string ProviderCode, string ProviderName, string Title, string Status)
    {
        Provider _record = new Provider
        {
            Id = Id,
            Provider_Code_CH = ProviderCode,
            Provider_Name_VC = ProviderName,
            Current_Title_VC = Title,
            Current_Status_VC = Status,
            Updated_DT = DateTime.Now,
            Display_Date = DateTime.Now.ToString(DateFormat),
            Preview_BT = false
        };

        return _record;
    }
}

I won't cover this in detail but we have a method GetProviders which will add some providers and return the list.

We also have a method called GetProvidersNoDatabase that will throw an Exception.

If you read my last post you'll know I like to watch tests break first just for fun.

The Tests

Ok lets create some tests. I did promise these are quick short tests you can add to any application.

In Nunit_Demo.Tests

Add the nuget packages for Nunit and Nunit test adapter.

We create a class "ProviderTests"

[TestFixture]
class ProviderTest

We add [TestFixture] and create our first method as below.

    [Test]
    public void LoadProviders()
    {
        List<Provider> _providers = new List<Provider>();

        Boolean bResult = true;
        string sStatusMessage = "";
        ProviderQueries PQ = new ProviderQueries();

        _providers = PQ.GetProviders();

        if (_providers.Count != 0)
        {
            bResult = true;
            sStatusMessage = "Yes has providers";
        }
        else
        {
            bResult = false;
            sStatusMessage = "No Providers";
        }

        Assert.IsTrue(bResult, sStatusMessage);
    }

This test calls the ProviderQueries class in the Nunit_Demo.Data class library.

_providers = PQ.GetProviders() will call the method and load the providers list.

if (_providers.Count != 0)

We then check the count for the list. If there are 0 records we know the test failed. This application should always have something.

Assert.IsTrue(bResult, sStatusMessage);

Assert is the basic Pass / fail for nunit tests. bResult is set to true or false depending on the number of records we have.

That's it, in under 20 lines of code we have a testr that will check if our application gets the providers back and we can move on.

Another way to do it

    [Test]
    public void LoadProviderNoDatabases()
    {
        List<Provider> _providers = new List<Provider>();

        ProviderQueries PQ = new ProviderQueries();

        _providers = PQ.GetProvidersNoDatabase();

        if (_providers.Count != 0)
        {
            Boolean bResult = true;

            Assert.IsTrue(bResult, "Providers loaded");
        }
        else
        {
            Assert.IsFalse(false, "No Providers loaded");
        }
    }

ProviderQueries also has a method that will always fail. So we have added that in here as a test.

Open test Explorer and run all we have 3 tests on our sample application our first test from the last post on getting started.

Test_explorer

As you can see 2 tests passed.

  • FirstTests

  • LoadProviders

  • LoadProvidersNoDatabases failed and as you can see in the Message the text from the exception we threw is loaded there.

These are simple and straight forward tests that take very little time to add to your application. Next we will start getting deeper and testing the user flow so what happens when someone signs up.

Tagged in:

NUnit, Unit testing

Last Update: August 21, 2018

About the Author