NUnit is an
open-source
Open source is source code that is made freely available for possible modification and redistribution. Products include permission to use and view the source code, design documents, or content of the product. The open source model is a decentrali ...
unit testing
Unit testing, component or module testing, is a form of software testing by which isolated source code is tested to validate expected behavior.
Unit testing describes tests that are run at the unit-level to contrast testing at the Integration ...
framework for the
.NET Framework and
Mono. It serves the same purpose as
JUnit
JUnit is a test automation framework for the Java programming language. JUnit is often used for unit testing, and is one of the xUnit frameworks.
JUnit is linked as a JAR at compile-time. The latest version of the framework, JUnit 5, resides ...
does in the
Java
Java is one of the Greater Sunda Islands in Indonesia. It is bordered by the Indian Ocean to the south and the Java Sea (a part of Pacific Ocean) to the north. With a population of 156.9 million people (including Madura) in mid 2024, proje ...
world, and is one of many programs in the
xUnit
xUnit is a label used for an automated testing software framework that shares significant structure and functionality that is traceable to a common progenitor SUnit.
The SUnit framework was ported to Java by Kent Beck and Erich Gamma as JUn ...
family.
Features
* Tests can be run from a console runner, within Visual Studio through a Test Adapter, or through 3rd party runners.
* Tests can be run in parallel.
* Strong support for data driven tests.
* Supports multiple platforms including
.NET Core,
Xamarin Mobile,
Compact Framework and
Silverlight
Microsoft Silverlight is a discontinued application framework designed for writing and running rich internet applications, similar to Adobe's runtime, Adobe Flash. While early versions of Silverlight focused on streaming media, later version ...
.
* Every test case can be added to one or more categories, to allow for selective running.
NUnit provides a console runner (nunit3-console.exe), which is used for batch execution of tests. The console runner works through the NUnit Test Engine, which provides it with the ability to load, explore and execute tests. When tests are to be run in a separate process, the engine makes use of the nunit-agent program to run them.
The NUnitLite runner may be used in situations where a simpler runner is more suitable. It allows developers to create self-executing tests.
Assertions
NUnit provides a rich set of
assertions as static methods of the
Assert
class. If an assertion fails, the method call does not return and an error is reported. If a test contains multiple assertions, any that follow the one that failed will not be executed. For this reason, it's usually best to try for one assertion per test.
Nunit 3.x is supporting multiple assertions.
estpublic void ComplexNumberTest()
Classical
Before NUnit 2.4, a separate method of the
Assert
class was used for each different assertion. It continues to be supported in NUnit, since many people prefer it.
Each assert method may be called without a message, with a simple text message or with a message and arguments. In the last case the message is formatted using the provided text and arguments.
// Equality asserts
Assert.AreEqual(object expected, object actual);
Assert.AreEqual(object expected, object actual, string message, params object[] parms);
Assert.AreNotEqual(object expected, object actual);
Assert.AreNotEqual(object expected, object actual, string message, params object[] parms);
// Identity asserts
Assert.AreSame(object expected, object actual);
Assert.AreSame(object expected, object actual, string message, params object[] parms);
Assert.AreNotSame(object expected, object actual);
Assert.AreNotSame(object expected, object actual, string message, params object[] parms);
// Condition asserts
// (For simplicity, methods with message signatures are omitted.)
Assert.IsTrue(bool condition);
Assert.IsFalse(bool condition);
Assert.IsNull(object anObject);
Assert.IsNotNull(object anObject);
Assert.IsNaN(double aDouble);
Assert.IsEmpty(string aString);
Assert.IsNotEmpty(string aString);
Assert.IsEmpty(ICollection collection);
Assert.IsNotEmpty(ICollection collection);
Constraint based
Beginning with NUnit 2.4, a new ''Constraint-based'' model was introduced. This approach uses a single method of the
Assert
class for all assertions, passing a
Constraint
object that specifies the test to be performed. This constraint-based model is now used internally by NUnit for all assertions. The methods of the classic approach have been re-implemented on top of this new model.
Example
Example of an NUnit
test fixture
A test fixture is a device used to consistently test some item, device, or piece of software. Test fixtures are used in the testing of electronics, software and physical devices.
Electronics
In testing electronic equipment such as circuit boa ...
:
using NUnit.Framework;
estFixturepublic class ExampleTestOfNUnit
// The following example shows different ways of writing the same exception test.
estFixturepublic class AssertThrowsTests
// This example shows use of the return value to perform additional verification of the exception.
estFixturepublic class UsingReturnValue
// This example does the same thing using the overload that includes a constraint.
estFixturepublic class UsingConstraint
The NUnit framework discovers the method
ExampleTestOfNUnit.TestMultiplication()
automatically by
reflection.
Extensions
FireBenchmarks is an
addin able to record execution time of unit tests and generate
XML
Extensible Markup Language (XML) is a markup language and file format for storing, transmitting, and reconstructing data. It defines a set of rules for encoding electronic document, documents in a format that is both human-readable and Machine-r ...
,
CSV,
XHTML
Extensible HyperText Markup Language (XHTML) is part of the family of XML markup languages which mirrors or extends versions of the widely used HyperText Markup Language (HTML), the language in which Web pages are formulated.
While HTML, pr ...
performances reports with charts and history tracking. Its main purpose is to enable a developer or a team that work with an
agile methodology to integrate
performance metric
A performance indicator or key performance indicator (KPI) is a type of performance measurement. KPIs evaluate the success of an organization or of a particular activity (such as projects, programs, products and other initiatives) in which it e ...
s and analysis into the
unit testing
Unit testing, component or module testing, is a form of software testing by which isolated source code is tested to validate expected behavior.
Unit testing describes tests that are run at the unit-level to contrast testing at the Integration ...
environment, to easily control and monitor the evolution of a software system in terms of
algorithmic complexity and system resources load.
NUnit.Forms is an expansion to the core NUnit framework and is also open source. It specifically looks at expanding NUnit to be able to handle testing user interface elements in
Windows Forms
Windows Forms, also known as WinForms, is a free, open-source graphical user interface (GUI) class library for building Windows desktop applications, included as a part of Microsoft .NET, .NET Framework or Mono, providing a platform to write c ...
. As of January 2013, Nunit.Forms is in Alpha release, and no versions have been released since May 2006.
NUnit.ASP is a discontinued
expansion to the core NUnit framework and is also open source. It specifically looks at expanding NUnit to be able to handle testing user interface elements in ASP.Net.
See also
*
Test automation
In software testing, test automation is the use of software separate from the software being tested to control the execution of tests and the comparison of actual outcomes with predicted outcomes. Test automation can automate some repetitive bu ...
*
List of unit testing frameworks for .NET programming languages (includes column indicating which are based on xUnit)
*
XUnit.net
*
JUnit
JUnit is a test automation framework for the Java programming language. JUnit is often used for unit testing, and is one of the xUnit frameworks.
JUnit is linked as a JAR at compile-time. The latest version of the framework, JUnit 5, resides ...
References
Bibliography
* Hunt, Andrew; Thomas, David (2007). ''Pragmatic Unit Testing in C# with NUnit, 2nd Ed.'' The Pragmatic Bookshelf (Raleigh), 2007. .
* Newkirk, Jim; Vorontsov, Alexei (2004). ''Test-Driven Development in Microsoft .NET.'' Microsoft Press (Redmond), 2004. .
* Hamilton, Bill (2004). ''NUnit Pocket Reference''.
O'Reilly
O'Reilly () is a common Irish surname. The O'Reillys were historically the kings of East Bréifne in what is today County Cavan. The clan were part of the Connachta's Uí Briúin Bréifne kindred and were closely related to the Ó Ruairc ( ...
(Cambridge), 2004. .
External links
*{{official website, http://www.nunit.org
GitHub SiteLaunchpad Site(no longer maintained)
video demonstration
NUnit.Forms home pageNUnitAsp homepage*Articl
Improving Application Quality Using Test-Driven Developmentprovides an introduction to TDD with concrete examples using Nunit
Open source tool, which can execute nunit tests in parallelCharlie Poole co-developer
Rob Prouse co-developer
Simone Busoli co-developer
.NET software
Extreme programming
Unit testing frameworks
Free software programmed in C Sharp
Articles with example C Sharp code
Software using the MIT license