Appendix A: NUnit Primer

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 . 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:

click to expand
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:

click to expand
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:

click to expand
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.

click to expand
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:

  • Green indicates that all the tests executed so far are successful.

  • Yellow means that some tests are ignored but there are no failures.

  • Red signals that there are failures.

A status bar at the bottom shows the following panels:

  • Status shows the current status of the tests being run. When all tests finish, the status changes to Completed . During the running of the tests, the status shows Running: < test-name > where < test-name > is the name of the current test being run.

  • Test Cases shows the total count of test cases found in the loaded assembly. Note that this is the number of leaf nodes in the test tree. ( Leaf nodes are nodes without descendants.)

  • Tests Run shows a running count of tests that have been completed.

  • Failures shows a running count of all tests that have failed so far.

  • Time displays how long it took to run the tests (time is displayed in seconds).

The main File menu has the following contents:

  • New Project allows you to create a new project. A project is a collection of test assemblies; this mechanism lets you organize tests from multiple test assemblies and treat them as a group .

  • Open loads a new test assembly or a previously saved NUnit project file.

  • Close closes the currently loaded test assembly or currently loaded NUnit project.

  • Save lets you save the current NUnit project in a file; if you re working with a single assembly, this menu item allows you to create a new NUnit project and save it in a file.

  • Save As allows you to save the current NUnit project as a file.

  • Reload forces a reload of the current test assembly or the NUnit project; the NUnit-Gui automatically watches for changes to the currently loaded test assemblies.

    When the assembly changes, the test runner reloads the test assembly. (The currently loaded test assembly is not reloaded while the tests are being run; the test assembly can be reloaded only between the test runs.) There is one caveat: if the test assembly depends on other assemblies, the test runner does not see changes to any dependant assemblies. Forcing a reload makes all dependent assembly changes visible to the test runner, as well.

  • Recent Files shows the history of the five most recent test assemblies or NUnit projects loaded in the test runner (this list is kept in the Windows registry and is maintained per user , so you see your tests only if you share your PC). The number of recent assemblies can be changed using the Options menu item, accessible from the main Tool menu.

  • Exit leaves the program.

The View menu has the following contents:

  • Expand expands the currently selected node in the tree by one level.

  • Collapse collapses the currently selected node in the tree.

  • Expand All recursively expands all nodes in the tree that are below the currently selected node.

  • Collapse All recursively collapses all nodes in the tree that are below the currently selected node.

  • Expand Fixtures expands all nodes in the tree that represent test fixtures.

  • Collapse Fixtures collapses all nodes in the tree that represent test fixtures.

  • Properties opens the dialog box that displays the properties of the currently selected node in the tree.

    The Tools menu has these items:

  • Save Results as XML saves the most recent results of running the tests as an XML file.

  • Options lets you customize some of the NUnit test runner behavior.

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:

  • The Errors and Failures window shows the tests that did not pass. In our case, this window is empty.

  • The Tests Not Run window shows the tests that did not get executed.

  • The Console.Error window shows the error messages that the running tests produced. These are the messages that an application code can output using the Console.Error output stream.

  • The Console.Out window shows text messages printed by the running tests to the Console.Out output stream.

Test-Driven Development in Microsoft .NET
Test-Driven Development in Microsoft .NET (Microsoft Professional)
ISBN: 0735619484
EAN: 2147483647
Year: 2004
Pages: 85

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: