Wednesday, August 10, 2011

NUnit - A Unit Testing Framework for .Net Applications

What is NUnit 

NUnit is an open-source unit-testing tool built for .NET, which follows in a long line of similar xUnit testing tools built for other platforms. It provides an easy-to-use framework for writing and running unit tests for your .NET applications. NUnit is an automated unit testing framework for .NET. NUnit is free to use with your .NET projects. NUnit is ported from another product called JUnit, which is a unit testing framework for Java applications. NUnit provides a class library which includes some classes and methods to help you write test scripts. NUnit provides graphical user interface application to execute all the test scripts and show the result as a success/failure report (instead of message boxes from the test script). NUnit is the most popular unit testing framework for .net applications. NUnit does not create any test scripts by itself. You have to write test scripts by yourself, but NUnit allows you to use it's tools and classes to make the unit testing easier.

Test Driven Development

Improved Productivity

A key principle of TDD is that developers are most productive when they are in the process of fixing a bug in an application (note: not looking for the bug). This is one of the few times during development that measurable progress is being made on the project. While developers spend time on other things, like finding bugs, they aren't able to spend as much time writing code to correct defects (for example, fixing bugs). A key benefit of test driven development (TDD) is to maximize the time developers spend in bug-fix mode, thus maximizing their productivity.

Improved Quality

In TDD, all non-trivial features of an application are tested. These tests are written before the actual code is written, and they are run all the time. Whenever a test fails, the developer immediately corrects the failure. So, when the time comes to add new functionality, the developer assumes the functionality exists and writes a test. He then runs the tests (which often won't even compile if they rely on as-yet-undefined classes and methods), and when they fail, he enters bug-fix mode and begins adding code to the application until he can get all tests to pass once more. Often, the process of writing the test for the functionality will provide the developer with a better understanding of how the production code should be designed, resulting in improved final code quality.
TDD also makes code easier to maintain, upgrade, and redesign, and has other benefits which the reader is encouraged to research.


NUnit: A Test Framework for .NET

NUnit provides a simple way for developers to write unit tests of the .NET classes, and comes with a small, sample application that demonstrates its use. The current version is written in C# and relies on attributes rather than inheritance and/or naming conventions to define tests and test suites. The main attributes involved are TestFixture, which applies to a class containing tests, SetUp and TearDown, which are run before and after each test, and Test.

NUnit also provides a simple graphical user interface that lets you select which assembly you want to test and which set of tests within that assembly you want to run. It then runs all of the tests in the assembly (or namespace or class) selected, displaying a green bar if everything passes and a red bar if any tests failed. Details of each failed test are also displayed, making it very easy to locate the cause of the failure.

Aa479010.aspnet-testwithnunit-01(en-us,MSDN.10).gif

Figure 1. NUnit's GUI tool provides instant feedback that everything is running as it should be, according to the tests defined so far.

Aa479010.aspnet-testwithnunit-02(en-us,MSDN.10).gif

Figure 2. NUnit shows the details of a failure caused by an invalid connection string.

There are other testing tools coming to market for .NET, but today NUnit has the largest support base, and is both free and open source, making it something you may want to try first.

Related Tools

NUnit Visual Studio .NET Addin

This addin, which requires NUnit, allows you to run your tests without leaving the Visual Studio .NET IDE. Simply right-click on a project or solution in the Solution Explorer to run all tests within that project or solution. The results are displayed in the Output window. Unfortunately, there are no pretty green or red bars, but if having one less program open is important to you, this can be a useful tool.

Aa479010.aspnet-testwithnunit-03(en-us,MSDN.10).gif

Figure 3. Run tests in Visual Studio .NET with a simple right-click.

Aa479010.aspnet-testwithnunit-04(en-us,MSDN.10).gif

Figure 4. Output from NUnit tests run within Visual Studio .NET

NUnitASP

NUnitASP allows TDD to be extended to the user interface in ASP.NET, by providing a means of testing Web controls. Its usage is beyond the scope of this article, but you may learn more about it from the NUnitASP Web site.

A Simple Application

To demonstrate TDD, I've used it to help me redevelop a multi-tier sample application for ASP.NET. The application demonstrates how to register a user for an application, how to grant them access to secure web pages, and how to allow them to sign in and sign out. The data access layer for this application is concerned with adding users to the database, retrieving user information, and verifying that user logins are correct. I’ve used this sample for a couple of years now and built it with TDD several times, learning new things with each iteration.

No comments: