Click here to Skip to main content
14,636,957 members
Articles » Development Lifecycle » Testing and QA » UI Testing
Technical Blog
Posted 24 Oct 2014

Tagged as


2 bookmarked

Simple Unit Testing using NUnit

Rate this:
0.00 (No votes)
Please Sign up or sign in to vote.
0.00 (No votes)
24 Oct 2014CPOL
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
    public class UnitTests
        MainWindowViewModel viewModel;

        public void SetUp()
            this.viewModel = new MainWindowViewModel();

        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.


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.

public void test_get_random_number()
    int number;

    ICommand generate = this.viewModel.Generate;

    string generateNumber = this.viewModel.Number;

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


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

Image 3 Image 4 Image 5 Image 6 Image 7 Image 8 Image 9 Image 10


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


About the Author

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

Comments and Discussions

GeneralShorter one Pin
Gaurav Aroraa27-Oct-14 10:19
professionalGaurav Aroraa27-Oct-14 10: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.