Test-Driven Development in Microsoft .NET (Microsoft Professional)

In Chapter 1, Test-Driven Development Practices, we discussed the need for a framework to support the development, management, and execution of automated programmer tests. In this appendix, we introduce NUnit, which is a testing framework for all Microsoft .NET programming languages. Initially ported from JUnit, the current version (2.1.4) is written entirely in C# and has been completely redesigned to take advantage of many .NET language features ”for example, custom attributes and other reflection- related capabilities.

To demonstrate NUnit s capabilities, we will start with a very simple example: adding two integer numbers .

NUnit Quick Start

The initial step, if you have not taken it already, is to download NUnit from www.nunit.org . The file, NUnit.msi, is a Microsoft Windows Installation file. After downloading the installation program, double-click the file to start the installation procedure. The installation program suggests that the program be installed into C:\Program Files\NUnit V2.1. In most cases, the default directory is adequate.

Step 1. Create Visual Studio Project for your test code.

Let s start by creating a new project in Microsoft Visual Studio .NET. Select Visual C# Projects as the type of project and Class Library as the template. Name the project NUnitQuickStart . Figure A-1 is a Visual Studio .NET screen shot that demonstrates this step:

Figure A-1: Creating the first NUnit project

Step 2. Add a reference to the NUnit Framework.

When building this example in Microsoft Visual Studio .NET, you ll need to add a reference to the nunit.framework.dll , as follows :

  1. Right-click the References folder in the Solution Explorer and select Add Reference.

  2. Select the nunit.framework component from the .NET tab and press the Select and OK buttons in the Add Reference dialog box.

Figure A-2 demonstrates this step:

Figure A-2: Adding a reference to nunit.framework.dll to the project

Step 3. Add a class to the project.

Add the NumbersFixture class to the project. Here s the code for this example:

using System; using NUnit.Framework; namespace NUnitQuickStart { [TestFixture] public class NumbersFixture { [Test] public void AddTwoNumbers() { int a = 1; int b = 2; int sum = a + b; Assert.AreEqual(3, sum); } } }

Step 4. Set up your Visual Studio Project to use the NUnit-Gui test runner.

To automatically run the NUnit-Gui test runner from within Visual Studio .NET, you need to set up NUnit-Gui as your startup application. Here s how to do it:

  1. Right-click your NUnitQuickStart project in the Solution Explorer.

  2. Select Properties from the context menu.

  3. Click the Configuration Properties folder in the left panel of the dialog box that displays.

  4. Select Debugging from the list that appears under the Configuration Properties folder.

  5. In the Start Action section on the right side of the Properties dialog box, choose Program from the drop-down box as the value for Debug Mode.

  6. Press the Apply button.

  7. Set nunit-gui.exe to be the Start Application. You can either type in the full path to nunit-gui.exe or use the browse button to navigate to it. (The default location is C:\Program Files\NUnit V2.1\bin\.)

Figure A-3 helps to demonstrate this step:

Figure A-3: Setting up NUnit-Gui as the test runner for the project

Step 5. Compile and run your test.

Now build the solution. After it is successfully compiled, start the application; the NUnit-Gui test runner appears. When you start NUnit-Gui for the first time, it opens with no tests loaded. Select Open from the File menu and browse to the location of NUnitQuickStart . dll . When you load the assembly with tests, the test runner creates a visual representation of the tests packaged in the loaded assembly. In the example, the test assembly has only one test, and the test assembly s structure looks something like that shown in Figure A-4.

Figure A-4: Visual representation of tests from the NUnitQuickStart test assembly in the NUnit-Gui test runner

Click the Run button. The tree nodes turn green, and the progress bar in the test runner window turns green. Green indicates success!

Step 6. Become familiar with the NUnit-Gui layout.

Let s take a closer look at the layout of the test runner window. In the center of the right panel, you ll see a test progress bar. The color of this bar reflects the status of the test s execution:

A status bar at the bottom shows the following panels:

The main File menu has the following contents:

The View menu has the following contents:

Now let s have a look at the right panel. You are already familiar with the Run button and the progress bar. There is also a Stop button next to the Run button: clicking this button terminates the execution of the tests being run. Below the progress bar is a text window with the following four tabs above it:

Категории