Click here to Skip to main content
13,350,029 members (50,140 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

6.3K views
2 bookmarked
Posted 24 Oct 2014

Simple Unit Testing using NUnit

, 24 Oct 2014
Rate this:
Please Sign up or sign in to vote.
Simple unit testing using NUnit

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 desired 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 won't 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 in identifying and fixing bugs in code.

Filed under: C#, CodeProject, Testing

Tagged: C#, Demonstration, Introduction, MVVM, Net, NUnit, unit testing, Viewmodel, WPF

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Wastedtalent
Software Developer (Senior)
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralShorter one Pin
Gaurav.Kumar.Arora27-Oct-14 11:19
memberGaurav.Kumar.Arora27-Oct-14 11:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.180111.1 | Last Updated 25 Oct 2014
Article Copyright 2014 by Wastedtalent
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid