unit testing part 1 - nunit and nunitaddin

15
Unit Testing documentation (Part 1) NMRENH Department BRUKER Biospin Page 1/15 Unit Testing with NUnit (http://www.nunit.org) & NUnitAddin (http://www.netlogics.ch/devcenter/display/NLC/NUnitAddin)

Upload: alexandre-soonnight

Post on 08-Nov-2014

19 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Unit Testing Part 1 - NUnit and NUnitAddIn

Unit Testing documentation (Part 1) NMRENH Department

BRUKER Biospin Page 1/15

Unit Testing with

NUnit (http://www.nunit.org)

&

NUnitAddin (http://www.netlogics.ch/devcenter/display/NLC/NUnitAddin)

Page 2: Unit Testing Part 1 - NUnit and NUnitAddIn

Unit Testing documentation (Part 1) NMRENH Department

BRUKER Biospin Page 2/15

Introduction

Unit testing is a procedure used to validate that individual units of source code are working properly. A unit is the smallest testable part of an application. In procedural programming a unit may be an individual program, function, procedure, etc., while in object-oriented programming, the smallest unit is a method; which may belong to a base/super class, abstract class or derived/child class.

Ideally, each test case is independent from the others; mock objects and test harnesses can be used to assist testing a module in isolation. Unit testing is typically done by developers and not by end-users.

The goal of unit testing is to isolate each part of the program and show that the individual parts are correct. A unit test provides a strict, written contract that the piece of code must satisfy. As a result, it affords several benefits.

Limitations of unit testing

Testing, in general, cannot be expected to catch every error in the program. The same is true for unit testing. By definition, it only tests the functionality of the units themselves. Therefore, it may not catch integration errors, performance problems, or other system-wide issues. Unit testing is more effective if it is used in conjunction with other software testing activities.

Like all forms of software testing, unit tests can only show the presence of errors; it cannot show the absence of errors.

Software testing is a combinatorial problem. For example, every boolean decision statement requires at least two tests: one with an outcome of "true" and one with an outcome of "false". As a result, for every line of code written, programmers often need 3 to 5 lines of test code. Therefore, it is unrealistic to test all possible input combinations for any non-trivial piece of software without an automated characterization test generation tool such as NUnit Factory used with C Sharp code.

To obtain the intended benefits from unit testing, a rigorous sense of discipline is needed throughout the software development process. It is essential to keep careful records, not only of the tests that have been performed, but also of all changes that have been made to the source code of this or any other unit in the software. Use of a version control system is essential. If a later version of the unit fails a particular test that it had previously passed, the version-control software can provide a list of the source code changes (if any) that have been applied to the unit since that time.

Page 3: Unit Testing Part 1 - NUnit and NUnitAddIn

Unit Testing documentation (Part 1) NMRENH Department

BRUKER Biospin Page 3/15

NUnit (http://www.nunit.org)

Page 4: Unit Testing Part 1 - NUnit and NUnitAddIn

Unit Testing documentation (Part 1) NMRENH Department

BRUKER Biospin Page 4/15

The version used in this document is the NUnit Version 2.4.6 If you are using a different version, some menu can be different

Installation : To download the last version, go of http://www.nunit.org Run the installer; you have a succession of screen, simply follow the instructions:

Click Next button

Check I accept… and click Next button

Page 5: Unit Testing Part 1 - NUnit and NUnitAddIn

Unit Testing documentation (Part 1) NMRENH Department

BRUKER Biospin Page 5/15

Select options to install (as default all options are selected) then click Next button

Click Install button

Setup is now installing the application

Page 6: Unit Testing Part 1 - NUnit and NUnitAddIn

Unit Testing documentation (Part 1) NMRENH Department

BRUKER Biospin Page 6/15

Presentation:

� Console mode:

Note: If you will use this mode, you can add the NUnit directory as an environment variable (with this solution you don’t need to jump into the NUnit directory when you start “NUnit-console.exe”). Open the Start menu, select Settings and then Control Panel. Double click on the System icon, select the tab Erweitert then Environment Variables, double click on Path in the listBox System variables. Insert the path from the bin directory from NUnit application (default path must be C:\Programme\NUnit 2.4.6\bin if you don’t change him during the previous installation) without delete the current content from the editBox. Click OK to validate. To test if the environment variable is good detected, open a Command Prompt and type “NUnit-console /help”, this opens the list of all parameter available with NUnit application.

You must see a screen like this:

You are now able to use the NUnit-console application.

Page 7: Unit Testing Part 1 - NUnit and NUnitAddIn

Unit Testing documentation (Part 1) NMRENH Department

BRUKER Biospin Page 7/15

Example of use: >NUnit-console MyAssembly.dll /xml=TestResult.xml

This command create a XML Result file which contain the result from exectution of the Tests classes include in the file MyAssembly.dll Concrete example on my projet: >NUnit-console MySiasTest.exe /xml=TestResult.xml

Note: This mode is recommended if you will special result that is not available in the Graphic interface or if you make always the same procedure. This mode is faster in certain cases (such as when we would directly have a XML file with all results of tests), because on the graphic interface we must make several manipulation to arrived at the same result (See next chapter).

Page 8: Unit Testing Part 1 - NUnit and NUnitAddIn

Unit Testing documentation (Part 1) NMRENH Department

BRUKER Biospin Page 8/15

� Graphic interface:

This mode is the most used, it allows us to see directly the results of tests. You have just to open the Assembly wich contain the tests class and to execute it.

Click on File, then Open and select an assembly or a project by double clicking on it. If your assembly or project contains one or more valid tests class, these latter opens automatically in the main window. Click on the Run button to execute the tests. If you will execute several different assemblies simultaneously, you must click on the menu Project → Add assembly… and select the new assembly to add.

If test passed, a green icon will appear at the begin line, else if test failed a red icon appear and if the test are ignored the icon will be yellow.

Main windows:

On this screen we can see that all tests are Ok

Creation of an XML Result file (necessary for NUnit2Report):

To generate an XML file you must select “Tools → Save Result as XML...”

Page 9: Unit Testing Part 1 - NUnit and NUnitAddIn

Unit Testing documentation (Part 1) NMRENH Department

BRUKER Biospin Page 9/15

Visual Studio integration:

Once the software is installed, we must integrate it into Visual Studio 2005 to easily generate xml tests reports.

To enable the Visual Studio integration you must check the checkbox “Enable Visual Studio Support” in the option window (menu Tools → Options…).

Page 10: Unit Testing Part 1 - NUnit and NUnitAddIn

Unit Testing documentation (Part 1) NMRENH Department

BRUKER Biospin Page 10/15

Now, we configure Visual Studio to run Nant directly by its interface with icons or keyboard shortcut:

� Open Visual Studio � Choose on the Menu: "Tools → External tools ..." � Click on "Add" button � Indicate the following values:

- Title: NUnit - Command: nunit-console.exe (or the complete path if you don’t have

create an environment variable) - Arguments: ResultTest.exe (or.dll) /xml=$(ProjectDir)TestResult.xml - Initial directory : $(TargetDir) - Check only "Close on exit" (close the Console automatically at end

compiling) � Memories the position from the created line in the list “Menu contents” (in this

example, it is the position four) � Click on “OK”. If an alert message is displayed, answer “No” at the question

Page 11: Unit Testing Part 1 - NUnit and NUnitAddIn

Unit Testing documentation (Part 1) NMRENH Department

BRUKER Biospin Page 11/15

Now, we join a keyboard shortcut at our NUnit tool: � Go to "Tools → Options…" and click on "Keyboard..." � Select the line “Tools.ExternalCommand4” � Set to line “Shortcuts for selected command” the keyboard shortcut you

want, "CTRL + F9" for example. Be careful not to choose a shortcut already taken and click on the "Assign"

� If an alert is displayed to answer you to create a new scheme, answer "Yes" and give it the name you want.

Finaly, we join our NUnit tool at a button shortcut in the Toolbar: � Go to "Tools → Customize…" and click on the tab "Toolbars..." � Click on “New” and choose a name (For example NUnit ), then “OK ”. A little

floating toolbar with the previous name as title appear on screen � Click on tab “Commands”, choose category “Tools” � In Area “Commands”, drag and drop the line “External Command 4” in the

floating toolbar � Click on “Modify Selection” and give a name at the button and a specific icon � Drag and drop the floating toolbar to the standard toolbar at the top � Close the window

You are now ready to use the NUnit tool.

Page 12: Unit Testing Part 1 - NUnit and NUnitAddIn

Unit Testing documentation (Part 1) NMRENH Department

BRUKER Biospin Page 12/15

If the following error occurs when you click on the NUnit button, then you must probably build an assembly for the library nunit.framework 2.4.6.0

Procedure to build an assembly from the library nunit.framework:

� Open the Start menu, select All Program → Microsoft .NET Framework SDK v2.0 and click on SDK Command Prompt

� The Command Prompt looks like this:

� Go into your NUnint directory (in our example it is C:\Programme\NUnit 2.4.6\bin) � Type the command “gacutil /i nunit.framework.dll” � If all well done you should have this result:

Now normally the error message System.IO.FileNotFoundExeption should not appear the next time you run the NUnit plug-in.

Page 13: Unit Testing Part 1 - NUnit and NUnitAddIn

Unit Testing documentation (Part 1) NMRENH Department

BRUKER Biospin Page 13/15

NUnitAddin (http://www.netlogics.ch/devcenter/display/NLC/NUnitAddin)

Page 14: Unit Testing Part 1 - NUnit and NUnitAddIn

Unit Testing documentation (Part 1) NMRENH Department

BRUKER Biospin Page 14/15

NUnitAddin integration in Visual Studio 2005:

Features

• Read Visual Studio 2005 files: .sln • Build visual tree from .sln files • Run tests in Visual Studio 2005

Prerequisite

NUnitAddin just requires the following components installed on your syste:

• Microsoft.Net SDK 2.0 framework • VisualStudio 2005 (all versions)

Page 15: Unit Testing Part 1 - NUnit and NUnitAddIn

Unit Testing documentation (Part 1) NMRENH Department

BRUKER Biospin Page 15/15

Download

NUnitAddin is available as sources or binary distribution:

• Latest sources (sourceforge) • Latest binaries (sourceforge)

Installation

• Extract the zip contents into My Documents\Visual Studio 2005\Addins • Start VisualStudio • In Tools>Addin Manager check NUnitAddin and Startup columns • Now activate the Add-in in Tools>NUnitAddin • The Add-in should now be visible (you can dock it and resize as desired)