This article has multiple issues. Please helpimprove it or discuss these issues on thetalk page.(Learn how and when to remove these messages) (Learn how and when to remove this message)
|
| NUnit | |
|---|---|
NUnit 2.4.6GUI on Windows | |
| Original authors | Charlie Poole, James Newkirk, Alexei Vorontsov, Michael Two, Philip Craig, Rob Prouse, Simone Busoli, Neil Colvin |
| Developers | The NUnit Project, .NET Foundation |
| Stable release | 4.4.0 / 10 August 2025; 6 months ago (2025-08-10) |
| Written in | C# |
| Operating system | .NET,.NET Framework,Mono |
| Type | Unit testing tool |
| License | MIT License for 3.0, BSD-style (modifiedzlib license) for 2.x |
| Website | www |
| Repository | github |
NUnit is anopen-sourceunit testingframework for.NET,.NET Framework, andMono. It serves the same purpose asJUnit does in theJava world, and is one of many in thexUnit family.[citation needed]
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.[citation needed]
The NUnitLite runner may be used in situations where a simpler runner is more suitable. It allows developers to create self-executing tests.[citation needed]
NUnit provides a rich set ofassertions as static methods of theAssert 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.[citation needed]
Nunit 3.x is supporting multiple assertions.
[Test]publicvoidComplexNumberTest(){ComplexNumberresult=SomeCalculation();Assert.Multiple(()=>{Assert.AreEqual(5.2,result.RealPart,"Real part");Assert.AreEqual(3.9,result.ImaginaryPart,"Imaginary part");});}
Before NUnit 2.4, a separate method of theAssert class was used for each different assertion. It continues to be supported in NUnit, since many people prefer it.[citation needed]
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.[citation needed]
// Equality assertsAssert.AreEqual(objectexpected,objectactual);Assert.AreEqual(objectexpected,objectactual,stringmessage,paramsobject[]parms);Assert.AreNotEqual(objectexpected,objectactual);Assert.AreNotEqual(objectexpected,objectactual,stringmessage,paramsobject[]parms);// Identity assertsAssert.AreSame(objectexpected,objectactual);Assert.AreSame(objectexpected,objectactual,stringmessage,paramsobject[]parms);Assert.AreNotSame(objectexpected,objectactual);Assert.AreNotSame(objectexpected,objectactual,stringmessage,paramsobject[]parms);// Condition asserts// (For simplicity, methods with message signatures are omitted.)Assert.IsTrue(boolcondition);Assert.IsFalse(boolcondition);Assert.IsNull(objectanObject);Assert.IsNotNull(objectanObject);Assert.IsNaN(doubleaDouble);Assert.IsEmpty(stringaString);Assert.IsNotEmpty(stringaString);Assert.IsEmpty(ICollectioncollection);Assert.IsNotEmpty(ICollectioncollection);
Beginning with NUnit 2.4, a newConstraint-based model was introduced. This approach uses a single method of theAssert class for all assertions, passing aConstraint 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.[citation needed]
Example of an NUnittest fixture:[citation needed]
usingNUnit.Framework;[TestFixture]publicclassExampleTestOfNUnit{[Test]publicvoidTestMultiplication(){Assert.AreEqual(4,2*2,"Multiplication");// Equivalently, since version 2.4 NUnit offers a new and// more intuitive assertion syntax based on constraint objects// [http://www.nunit.org/index.php?p=constraintModel&r=2.4.7]:Assert.That(2*2,Is.EqualTo(4),"Multiplication constraint-based");}}// The following example shows different ways of writing the same exception test.[TestFixture]publicclassAssertThrowsTests{[Test]publicvoidTests(){// .NET 1.xAssert.Throws(typeof(ArgumentException),newTestDelegate(MethodThatThrows));// .NET 2.0Assert.Throws<ArgumentException>(MethodThatThrows);Assert.Throws<ArgumentException>(delegate{thrownewArgumentException();});// Using C# 3.0Assert.Throws<ArgumentException>(()=>{thrownewArgumentException();});}voidMethodThatThrows(){thrownewArgumentException();}}// This example shows use of the return value to perform additional verification of the exception.[TestFixture]publicclassUsingReturnValue{[Test]publicvoidTestException(){MyExceptionex=Assert.Throws<MyException>(delegate{thrownewMyException("message",42);});Assert.That(ex.Message,Is.EqualTo("message"));Assert.That(ex.MyParam,Is.EqualTo(42));}}// This example does the same thing using the overload that includes a constraint.[TestFixture]publicclassUsingConstraint{[Test]publicvoidTestException(){Assert.Throws(Is.Typeof<MyException>().And.Message.EqualTo("message").And.Property("MyParam").EqualTo(42),delegate{thrownewMyException("message",42);});}}
The NUnit framework discovers the methodExampleTestOfNUnit.TestMultiplication() automatically byreflection.[citation needed]
FireBenchmarks is anaddin able to record execution time of unit tests and generateXML,CSV,XHTML performances reports with charts and history tracking. Its main purpose is to enable a developer or a team that work with anagile methodology to integrateperformance metrics and analysis into theunit testing environment, to easily control and monitor the evolution of a software system in terms ofalgorithmic complexity and system resources load.[citation needed]
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 inWindows Forms. As of January 2013, Nunit.Forms is in Alpha release, and no versions have been released since May 2006.[citation needed]
NUnit.ASP is a discontinued[9] 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.[citation needed]