Udemy logo

microsoft access 2010 tutorialIn the world of computer programming, no task is as despised as business logic debugging. The debugging wouldn’t be so bad if we could easily find them in our code, but most business logic errors only happen while the programs run. Some of them even happen with no fault from the programmer. Either way, we have to fix every bug in our programs long before our end users get their hands on them, and the only way to do that is to thoroughly test our programs. Fortunately, we have special tools such as the NUnit C# unit-testing framework to help us out.

Learn how to handle C# errors at Udemy

What is NUnit?

NUnit exists to help programmers make sure their programs work as advertised. Originally ported  Java , the NUnit C# testing framework lets you test your C# programs in discrete logical units. It comes with all you need to properly test your code against your business requirements. You just have to add a few attribute interfaces into your code, and then run your applications in the especially-designed GUI app. The framework works with any .Net capable language including both C# and VB.Net.

Explore the fundamentals of C# at Udemy

Using NUnit with C#

NUnit works by separating your application into discrete, isolate modules. It then tests each module to see if it works the way you designed it. But, before NUnit can test your stuff, you have to install it. You do that by downloading the application from the NUnit.org website and then run the executable. The installation process is fairly straight forward, and you should be able to complete it in a few minutes. Once complete, the installer will load the user interface

To use the NUnit C# testing system, you have to load your project into the NUnit GUI by selecting Open Project from the File menu, and then selecting your test file. NUnit accepts any properly prepared DLL or executable file that has your desired unit test cases. If your file works, NUnit enables the Run button so you can begin the tests.

The NUnit C# Testing Framework

As I said in the beginning, your files have to be properly prepared before NUnit will test them. That means you have implement the NUnit testing framework in your code. The framework is the core of the NUnit system, and it provides all test attributes and classes that you need to handle every test function and then some. To use them, you have to include the NUnit.framework namespace in your application, and then add appropriate classes and events to your code.

Brush up on your C# class object programming skills at Udemy

The TextFixture and Test Attributes

The TextFixture attribute tells the NUnit system which class in a program to test. It is the first thing the Test Runner app looks for during each and every test you start with the GUI.

The NUnit C# Test attribute marks your test methods. Your test methods must be public, return void, accept no arguments, and your program cannot run it while the TestFixture attribute is active, but you are otherwise free to name the method as you see fit. You can also have as many Test methods as your application needs

Like with all C# attributes, you place these attributes before your test code as I have done in the following example.

using System;
using NUnit.Framework;
using System.Text;
namespace UNitTesting {
     public class Program
     public void Test()

The NUnit C# Assert Class

The NUnit Framework uses its Assert class to check you classes against their desired results. You don’t have to initiate to use its methods such as AreEqual and AreNotEqual. They are all immediately available with the NUnit namespace.

The ExpectedException and Ignore Attributes

ExpectedException handles exception assertions. It ensures that certain exceptions will occur. This is the opposite of the try catch block, but you need it in order for NUnit to track the exceptions while they occur. You implement the ExpectedException attribute right after the Test attribute like I did in the following example. The attribute requires you pass it the exception you need.

You use the Ignore attribute to signify which methods NUnit should skip for any reason. It has an optional string argument you can use if you need to display anything.

public class Program
     public void ExceptionTest()
          int i=10,j=0,x;
          x = i / j;
    [Ignore("NUnit skips this method")]
    public void SkipTest()

Running a NUnit C# Test

Now that you understand the basic, it is time for me to show you how to use NUnit in your programs. The procedure is simple. You just need to implement what is known as test-driven project development.

  1. You first step is to simply create your program classes as you normally do making sure that you enclose your program classes in a namespace. This namespace will be how you link your program class files with your test ones.
  2. You then create a new C# class library project that will serve as your test library of classes. You can name this library anything you want, but I recommend that you link the name to you main program as much as you can.
  3. Within your new text project, you should include references to the NUnit framework, and your main program’s namespace. This is how your test classes will know what they are testing.
  4. Create your test class file and include the namespaces for NUnit and your program as I showed above.
  5. The next step is to write your test class and its methods. You need to write a test class for any and all classes you have in your main program class file, and for any tests you need done.
  6. Finally, you just have to build the project, and prepare it for running.

One you have your test class ready to go, you then move things over to the NUnit C# testing application. You load your test project into the app and click the Run button. NUnit will then test run your program with a progress bar under the test control buttons. The bar will be green if things go according to plan. It will turn red if there is a problem. You can then use the tabs below the progress bar to check your program’s output and information on any errors NUnit finds along the way.

Page Last Updated: June 2014

Top courses in C#

Complete C# Unity Game Developer 3D
Ben Tristem, Rick Davidson, GameDev.tv Team, Gary Pettie
4.7 (36,904)
C#/.NET - 50 Essential Interview Questions (Mid Level)
Krystyna Ślusarczyk
4.9 (102)
Highest Rated
C# Intermediate: Classes, Interfaces and OOP
Mosh Hamedani
4.5 (30,848)
Learn C# By Building Applications
Avetis Ghukasyan
4.6 (2,457)
Learn Parallel Programming with C# and .NET
Dmitri Nesteruk
4.5 (3,087)
Advanced Topics in C#
Dmitri Nesteruk
4.7 (294)

More C# Courses

C# students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.

Request a demo