Simple Unit Testing using NUnit

Please note: This page has now been moved. Click here to go to the new site.

Unit testing seems to be all the rage these days, and it does provide many benefits, not least that we can test our code without the need to have a complete application to do so. The MVVM pattern that we used in my previous post to decouple the view model from the view is a pattern designed with testing in mind.

Here I will create simple tests for the random number generator created in that post, using NUnit (as this is my preferred framework, but there are many out there), to demonstrate testing a view model without the need for a working view.

Firstly, we create a new project and add a reference to the existing project and the NUnit.Framework.dll we can obtain from the NUnit website. After that, I have created the outline of a simple test shown below.

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

using NUnit.Framework;
using RandomNumber;

namespace Tests
{
    [TestFixture]
    public class UnitTests
    {
        MainWindowViewModel viewModel;

        [TestFixtureSetUp]
        public void SetUp()
        {
            this.viewModel = new MainWindowViewModel();
        }

        [Test]
        public void test_get_random_number()
        {
            int number;
            string generateNumber = this.viewModel.Number;

            Assert.True(Int32.TryParse(generateNumber, out number));
        }
    }
}

If you noticed, you will quickly spot that this test will fail. This is the desire affect. We should always write a quick test method that we would expect to fail first. This way, we know the test itself is working, and that it wont always pass regardless.

TestFail1

Now we have confirmed the test fails, we have to correct it so that we expect it to pass. To do this, we need to imitate what happens when we click the button. Because we’ve decoupled the view model from the view, this is very simple. Firstly we need to add a reference in our test project to the PresentationCore library, and the System>Windows.Input namespace. This enables us to use commanding. Then we can call the Generate command on our test. This time you should see the test passes.

        [Test]
        public void test_get_random_number()
        {
            int number;

            ICommand generate = this.viewModel.Generate;
            generate.Execute(true);

            string generateNumber = this.viewModel.Number;

            Assert.True(Int32.TryParse(generateNumber, out number));
        }

TestPass1

Although this is a very simple example, I believe it nicely demonstrates how easy it is to test our application through unit testing and MVVM, and the benefits of it. Many detractors suggest that unit testing will increase the development time of a product, which is true. However, through testing we have better quality control, which will lead to less bugs and the extra initial investment in time should be rapidly recouped in the time saved identifying and fixing bugs in code.

Leave a comment