NUnit Testing Framework

NUnit Explained

NUnit is a freely available, open source product that provides a testing framework and test runners.

NUnit provides an easy way to mark and run individual tests and fixtures by using categories. You can associate different test methods with one or more categories, and then select which categories you want to exclude (or include) when running the tests.

To use NUnit add a reference to nunit.framework.dll in the Visual Studio project in order to compile and use test code.

NUnit Integration

You should distribute NUnit via your version control system.

Many .NET projects define both a src/ folder and a lib/ folder. The src/ folder contains the source code to the project, and the lib/ folder contains pre-compiled components (usually third-party).

In this context, you will have a lib/nunit/ folder that contains the NUnit binary distribution. Your projects and Nant files would reference the nunit.framework.dll in this folder, and developers would run the nunit.exe GUI from this folder via a shortcut.

This keeps developer versions of NUnit synchronized, making deploying any upgrades or customizations easy and also keeps the environment consistent. Avoid installing NUnit on a local workstation to reduce confusion.

NUnit and WinForms Applications

To unit test Windows Forms, use NUnitForms framework (NUnitForms.dll), which is an extension of NUnit.

NUnitForms can also simulate a user changing fields in the GUI via the keyboard, and all other kinds of things, relatively easily.

NUnitForms has a way to deal with Modal dialogs, using the ExpectModal method in the NUnitFormTest class. To use it (or any other NUnitForms methods), we derive our fixture from NUnitFormTest and call ExpectModal, passing the name of the caption (title) of the modal dialog. When a modal dialog is displayed that has the specified caption, the handler method is called.

NUnit and Web Applications

In many modern web applications, much of the important end-user functionality is on the client-side in JavaScript. The server-side code mostly accepts AJAX requests that either retrieve or store data in the database with some data validation and logging.

To unit test the JavaScript use JsUnit, which provides a framework and a testrunner that can run within most modern browsers.

You can assert that your JavaScript code is having the correct effect on specific DOM elements, such as adding or removing styles and/or child nodes.

To test web applications beyond JavaScript and our server-side objects, there is a free, open source tool called Selenium.