This documentation covers the NUnit 2.6.4 release,
31 | a bugfix release to NUnit 2.6.4. This is expected to be the last release of NUnit
32 | version 2, with all future work being done on NUnit 3.0.
33 |
34 |
Where applicable, we have marked sections with the version in which a feature
35 | first appeared.
36 |
37 |
If you are new to NUnit, we suggest you begin by reading the
38 | Getting Started section of this site.
39 | Those who have used earlier releases may want to begin with the
40 | Upgrading section.
41 |
42 |
See the
43 | Release Notes for more information on this release.
44 |
45 |
All documentation is included in the release packages of NUnit. Beginning with NUnit
46 | 2.4.2, you may choose to download the documentation
47 | separately.
NUnit is designed to be extended in a number of ways.
31 |
32 |
Extensions to the NUnit framework - the part of NUnit that is referenced
33 | by tests - usually take the form of
34 | Custom Constraints, written by users to
35 | encapsulate tests that pertain to their specific projects.
36 |
37 |
Extending the features found within NUnit itself depends on the use of
38 | NUnit Addins.
39 | Currently, The Addin mechanism only supports extensions to the NUnit core -
40 | the part of NUnit that builds and executes test suites. However, the API that
41 | is used provides for the future ability to extend the client side of NUnit,
42 | including the GUI.
If you haven't already done so, go to our Download page, select a version of NUnit and download it. The
31 | Installation page
32 | contains instructions for installing on your system.
33 |
34 |
To get started using NUnit, read the Quick Start page. This article demonstrates the development process with NUnit in the
35 | context of a C# banking application. Check the
36 | Samples page for additional examples,
37 | including some in VB.Net, J# and managed C++.
38 |
39 |
Which Test Runner to use?
40 |
41 |
NUnit has two different ways to run your tests. The
42 | console runner, nunit-console.exe,
43 | is the fastest to launch, but is not interactive.
44 | The gui runner,
45 | nunit.exe, is a Windows Forms application that allows you to work
46 | selectively with your tests and provides graphical feedback.
In general, there are two approaches to running tests built against earlier
31 | versions of the NUnit framework from 2.0 through 2.5.10:
32 |
33 |
34 |
Run the tests without recompilation. This is the best approach when no
35 | further changes are to be made to a project. Ensure that the framework
36 | assembly against which the tests were built continues to be avaialble
37 | in the test directory.
38 |
Recompile the tests referencing the latest version of the framework assembly.
39 | This is usually the best option when a project is still under development.
40 |
41 |
42 |
Note:
43 | NUnit 2.6 no longer recognizes "old style" test cases - those
44 | identified by a name beginning with "Test". If you have such tests, you will
45 | need to convert them to use the TestAttribute and recompile your tests. In such cases, it makes sense to recompile against
46 | the latest framework.
47 |
48 |
From NUnit 1.x
49 |
50 |
Tests originally compiled against a version of NUnit prior to 2.0 will need
51 | to be rewritten and recompiled in order to run under NUnit 2.6.
52 |
53 |
54 |
Permission is hereby granted, free of charge, to any person obtaining
33 | a copy of this software and associated documentation files (the
34 | "Software"), to deal in the Software without restriction, including
35 | without limitation the rights to use, copy, modify, merge, publish,
36 | distribute, sublicense, and/or sell copies of the Software, and to
37 | permit persons to whom the Software is furnished to do so, subject to
38 | the following conditions:
39 |
40 |
The above copyright notice and this permission notice shall be
41 | included in all copies or substantial portions of the Software.
42 |
43 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
44 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
45 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
46 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
47 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
48 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
49 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
50 |
51 |
Notice
52 |
53 |
NUnit 2.6 assemblies distributed with this adapter are subject to the
54 | NUnit license.
55 |
56 |
57 |
A SuiteBuilder is an addin used to build a test fixture from a type. NUnit itself
32 | uses a SuiteBuilder to recognize and build TestFixtures.
33 |
34 |
Extension Point
35 |
An addin may use the host to access this extension point by name:
36 |
37 |
The extension object passed to Install must implement the ISuiteBuilder interface:
42 |
43 |
44 | public interface ISuiteBuilder
45 | {
46 | bool CanBuildFrom( Type type );
47 | Test BuildFrom( Type type );
48 | }
49 |
50 |
51 |
CanBuildFrom should return true if the specified Type is one from which
52 | the builder is able to create a fixture. This usually involve examining
53 | the Type and its attriutes.
54 |
55 |
The BuildFrom method should return a test fixture completely populated
56 | with its contained test cases. Return null if it is not possible to
57 | build a fixture using the provided Type.
58 |
59 |
60 |
Assertions are central to unit testing in any of the xUnit frameworks, and NUnit
31 | is no exception. NUnit provides a rich set of assertions as static methods of
32 | the Assert class.
33 |
34 |
If an assertion fails, the method call does not return and an error is reported.
35 | If a test contains multiple assertions, any that follow the one that failed
36 | will not be executed. For this reason, it's usually best to try for one
37 | assertion per test.
38 |
39 |
Each method may be called without a message, with a simple text message or with
40 | a message and arguments. In the last case the message is formatted using the
41 | provided text and arguments.
42 |
43 |
Two Models
44 |
45 |
Before NUnit 2.4, a separate method of the Assert class was used for each
46 | different assertion. We call this the
47 | classic model.
48 | It continues to be supported in NUnit, since many people prefer it.
49 |
50 |
Beginning with NUnit 2.4, a new
51 | constraint-based model was introduced. This approach uses a single method of the Assert class
52 | for all assertions, passing a Constraint object that specifies
53 | the test to be performed.
54 |
55 |
This constraint-based model is now used internally by NUnit
56 | for all assertions. The methods of the classic approach have been
57 | re-implemented on top of this new model.
58 |
59 |
65 |
66 |
EventListeners are able to respond to events that occur in the course
32 | of a test run, usually by recording information of some kind. Note that
33 | EventListeners called asynchronously with respect to test execution and
34 | are not able to affect the actual execution of the test.
35 |
36 |
Extension Point
37 |
Addins use the host to access this extension point by name:
38 |
39 |
The nunit-agent.exe program is used by other runners when the tests are being
31 | run in a separate process. It is not intended for direct execution by users.
32 |
33 |
NUnit runs tests in a separate process in several situations:
34 |
35 |
36 |
When the program needs to be run under a different framework or version
37 | from the one being used by NUnit itself.
38 |
When the user requests process-level isolation through the command line
39 | or the NUnit settings.
40 |
41 |
42 |
Debugging
43 |
44 |
When debugging tests that are run in a separate process, it is
45 | not possible to do so by simply running the console or gui runner
46 | under the debugger. Rather, it is necessary to attach the debugger
47 | to the nunit-agent process after the tests have been loaded.
48 |
49 |
When running under the Gui, NUnit will continue to use the same
50 | process to reload tests so that it is not normally necessary to
51 | re-attach to a new process. However, if the settings are changed
52 | in a way that requires a different process - for example, by changing
53 | the version of the runtime that is being used - the old process will
54 | be terminated and a new one created. In that case, it's necessary
55 | to re-attach to the new process.
56 |
57 |
58 |
This software is provided 'as-is', without any express or implied warranty. In
36 | no event will the authors be held liable for any damages arising from the use
37 | of this software.
38 |
39 |
Permission is granted to anyone to use this software for any purpose, including
40 | commercial applications, and to alter it and redistribute it freely, subject to
41 | the following restrictions:
42 |
43 |
44 |
The origin of this software must not be misrepresented; you must not claim
45 | that you wrote the original software. If you use this software in a product, an
46 | acknowledgment (see the following) in the product documentation is required.
47 |
Altered source versions must be plainly marked as such, and must not be
53 | misrepresented as being the original software.
54 |
55 |
This notice may not be removed or altered from any source distribution.
56 |
57 |
58 |
License Note
59 |
This license is based on
60 | the open source zlib/libpng license. The idea was to keep the license
61 | as simple as possible to encourage use of NUnit in free and commercial
62 | applications and libraries, but to keep the source code together and to give
63 | credit to the NUnit contributors for their efforts. While this license allows
64 | shipping NUnit in source and binary form, if shipping a NUnit variant is the
65 | sole purpose of your product, please let
66 | us know.
An Extenders Guide will be published in the future. At the moment, writing an
31 | extension is a bit of an adventure. Extension authors are advised to join the
32 | nunit-developer list and post questions and comments there.
33 |
34 |
For the moment, the following tips may be of assistance.
35 |
36 |
The nunit.core.interfaces assembly is intended to be stable in the future
37 | while the nunit.core assembly will change from release to release. Right now,
38 | both assemblies are still in flux, but extensions that depend solely on the interfaces
39 | assembly will have a much better chance of surviving NUnit version changes. Unfortunately,
40 | this is rather difficult to do without duplicating a great deal of NUnit code. Most
41 | of the add-in samples provided with NUnit are currently version dependent.
42 |
43 |
If you place the definition of a custom attribute in the same assembly as your
44 | add-in, then user tests are dependent on the add-in assembly. If the add-in is
45 | version-dependent, then the user tests will also be version-dependent. We suggest
46 | placing any types referenced by user tests in a separate assembly, particularly if
47 | your extension relies on nunit.core.
48 |
49 |
If using Visual Studio, set Copy Local to false for any references to nunit.core
50 | or nunit.core.interfaces. This is especially important if you are also building
51 | NUnit itself.
52 |
53 |
There is currently no mechanism to allow decorators to apply in a particular order.
54 | NUnit applies decorators in the order in which they are returned through reflection,
55 | which may vary among different runtimes.
56 |
57 |
Avoid trying to "stretch" the existing extension points to do more than they were
58 | intended to do. Rather, let us know what further extension points you would like to see!
59 |
The extension object passed to Install must implement the ITestDecorator interface:
41 |
42 |
43 | public interface ITestDecorator
44 | {
45 | Test Decorate( Test test, MemberInfo member );
46 | }
47 |
48 |
49 |
The Decorate method may do several things, depending on what it needs
50 | to accomplish:
51 |
52 |
Return test unmodified
53 |
Modify properties of the test object and return it
54 |
Replace test with another object, either discarding the
55 | original or aggregating it in the new test.
56 |
57 |
58 |
Depending on what the decorator does, it may need to run
59 | ahead of other decorators or after them. Decorators should
60 | be installed using the Install method overload that takes
61 | a priority. The priorities range from 1 to 9 and decorators
62 | with lower priority values are installed first. The following
63 | standard values are defined for use if desired:
64 |