Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
NUnit
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
{{about|the C# unit testing framework|the NGINX web application server|NGINX Unit}} {{multiple issues| {{more citations needed|date=January 2017}} {{original research|date=January 2017}} }} {{Infobox software | name = NUnit | logo = Nunit logo 250.png | logo size = 200px | screenshot = NUnit GUI.png | screenshot size = 300px | caption = NUnit 2.4.6 [[GUI]] on Windows | author = Charlie Poole, James Newkirk, Alexei Vorontsov, Michael Two, Philip Craig, Rob Prouse, Simone Busoli, Neil Colvin | developer = The NUnit Project,<br />[[.NET Foundation]] | latest release version = 4.0.0 | latest release date = {{Start date and age|2023|11|26|df=y}} | repo = {{URL|https://github.com/nunit}} | programming language = [[C Sharp (programming language)|C#]] | operating system = [[.NET Framework]], [[Mono (software)|Mono]] | genre = [[Unit testing]] tool | license = [[MIT License]] for 3.0, BSD-style (modified [[zlib license]]) for 2.x | website = {{URL|http://www.nunit.org}} }} '''NUnit''' is an [[open-source software|open-source]] [[unit testing]] [[software framework|framework]] for the [[.NET Framework]] and [[Mono (software)|Mono]]. It serves the same purpose as [[JUnit]] does in the [[Java (programming language)|Java]] world, and is one of many programs in the [[xUnit]] family.{{citation needed|date=January 2017}} == Features == * Tests can be run from a console runner, within Visual Studio through a Test Adapter,<ref>{{cite web|title=NUnit 3 Test Adapter|url=https://visualstudiogallery.msdn.microsoft.com/0da0f6bd-9bb6-4ae3-87a8-537788622f2d}}</ref> or through 3rd party runners. * Tests can be run in parallel.<ref>{{cite web|title=Parallelizable Attribute|website=[[GitHub]] |url=https://github.com/nunit/nunit/wiki/Parallelizable-Attribute}}</ref> * Strong support for data driven tests.<ref>{{cite web|title=TestCaseData|website=[[GitHub]] |url=https://github.com/nunit/nunit/wiki/TestCaseData}}</ref> * Supports multiple platforms including [[.NET Core]],<ref>{{cite web|last1=Prouse|first1=Rob|title=Testing .NET Core using NUnit 3|url=http://www.alteridem.net/2015/11/04/testing-net-core-using-nunit-3/|date=2015-11-04}}</ref> [[Xamarin|Xamarin Mobile]],<ref>{{cite web|last1=Prouse|first1=Rob|title=NUnit 3.0 Test Runner for Android and iOS|url=http://www.alteridem.net/2015/03/25/nunit-3-0-test-runner-for-android-and-ios/|date=2015-03-25}}</ref> [[.NET Compact Framework|Compact Framework]]<ref>{{cite web|title=NUnit Version 3 for Compact Framework|url=https://www.nuget.org/packages/NUnitCF/}}</ref> and [[Microsoft Silverlight|Silverlight]].<ref>{{cite web|title=NUnit Version 3 for SilverLight 5.0|url=https://www.nuget.org/packages/NUnit.SL50/}}</ref> * Every test case can be added to one or more categories, to allow for selective running.<ref>{{cite web|url=https://github.com/nunit/nunit/wiki/Category-Attribute|title=CategoryAttribute|website=[[GitHub]] |accessdate=2015-12-15}}</ref> 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|date=January 2017}} 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|date=January 2017}} == Assertions == NUnit provides a rich set of [[Assertion (software development)|assertions]] as static methods of the <code>Assert</code> 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|date=January 2017}} Nunit 3.x is supporting multiple assertions. <syntaxhighlight lang="c#"> [Test] public void ComplexNumberTest() { ComplexNumber result = SomeCalculation(); Assert.Multiple(() => { Assert.AreEqual(5.2, result.RealPart, "Real part"); Assert.AreEqual(3.9, result.ImaginaryPart, "Imaginary part"); }); } </syntaxhighlight> === Classical === Before NUnit 2.4, a separate method of the <code>Assert</code> class was used for each different assertion. It continues to be supported in NUnit, since many people prefer it.{{citation needed|date=January 2017}} 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|date=January 2017}} <syntaxhighlight lang="csharp"> // 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); </syntaxhighlight> === Constraint based === Beginning with NUnit 2.4, a new ''Constraint-based'' model was introduced. This approach uses a single method of the <code>Assert</code> class for all assertions, passing a <code>Constraint</code> 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|date=January 2017}} == Example == Example of an NUnit [[test fixture#Test fixture in xUnit|test fixture]]:{{citation needed|date=January 2017}} <syntaxhighlight lang="csharp"> using NUnit.Framework; [TestFixture] public class ExampleTestOfNUnit { [Test] public void TestMultiplication() { 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] public class AssertThrowsTests { [Test] public void Tests() { // .NET 1.x Assert.Throws(typeof(ArgumentException), new TestDelegate(MethodThatThrows)); // .NET 2.0 Assert.Throws<ArgumentException>(MethodThatThrows); Assert.Throws<ArgumentException>( delegate { throw new ArgumentException(); }); // Using C# 3.0 Assert.Throws<ArgumentException>( () => { throw new ArgumentException(); }); } void MethodThatThrows() { throw new ArgumentException(); } } // This example shows use of the return value to perform additional verification of the exception. [TestFixture] public class UsingReturnValue { [Test] public void TestException() { MyException ex = Assert.Throws<MyException>( delegate { throw new MyException("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] public class UsingConstraint { [Test] public void TestException() { Assert.Throws(Is.Typeof<MyException>() .And.Message.EqualTo("message") .And.Property("MyParam").EqualTo(42), delegate { throw new MyException("message", 42); }); } } </syntaxhighlight> The NUnit framework discovers the method <code>ExampleTestOfNUnit.TestMultiplication()</code> automatically by [[reflection (computer science)|reflection]].{{citation needed|date=January 2017}} == Extensions == '''FireBenchmarks''' is an [[plug-in (computing)|addin]] able to record execution time of unit tests and generate [[XML]], [[Comma-separated values|CSV]], [[XHTML]] 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]]s and analysis into the [[unit testing]] environment, to easily control and monitor the evolution of a software system in terms of [[Analysis of algorithms|algorithmic complexity]] and system resources load.{{citation needed|date=January 2017}} '''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]]. As of January 2013, Nunit.Forms is in Alpha release, and no versions have been released since May 2006.{{citation needed|date=January 2017}} '''NUnit.ASP''' is a discontinued<ref>{{cite web|url=http://nunitasp.sourceforge.net/|title=NUnit.ASP website main page|publisher=[[SourceForge]]|accessdate=2008-04-15}}</ref> 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|date=January 2017}} == See also == {{Portal|Free and open-source software}} * [[Test automation]] * [[List of unit testing frameworks#.NET programming languages|List of unit testing frameworks for .NET programming languages]] (includes column indicating which are based on xUnit) * [[XUnit.net]] * [[JUnit]] == References == {{Reflist}} == Bibliography == * Hunt, Andrew; Thomas, David (2007). ''Pragmatic Unit Testing in C# with NUnit, 2nd Ed.'' The Pragmatic Bookshelf (Raleigh), 2007. {{ISBN|0-9776166-7-3}}. * Newkirk, Jim; Vorontsov, Alexei (2004). ''Test-Driven Development in Microsoft .NET.'' Microsoft Press (Redmond), 2004. {{ISBN|0-7356-1948-4}}. * Hamilton, Bill (2004). ''NUnit Pocket Reference''. [[O'Reilly Media|O'Reilly]] (Cambridge), 2004. {{ISBN|0-596-00739-6}}. == External links == *{{official website|http://www.nunit.org}} *[https://github.com/nunit GitHub Site] *[https://launchpad.net/nunitv2 Launchpad Site] (no longer maintained) *[http://www.parlezuml.com/tutorials/tdd.html Test-driven Development with NUnit & Test-driven.NET] video demonstration *[http://nunitforms.sourceforge.net/ NUnit.Forms home page] *[http://nunitasp.sourceforge.net/ NUnitAsp homepage] *Article [https://web.archive.org/web/20130124034916/http://www.methodsandtools.com/archive/archive.php?id=20 Improving Application Quality Using Test-Driven Development] provides an introduction to TDD with concrete examples using Nunit *[http://tbox.codeplex.com Open source tool, which can execute nunit tests in parallel] *[http://www.charliepoole.org/ Charlie Poole], co-developer *[http://www.alteridem.net/ Rob Prouse], co-developer *[https://simoneb.github.io/ Simone Busoli], co-developer [[Category:.NET software]] [[Category:Extreme programming]] [[Category:Unit testing frameworks]] [[Category:Free software programmed in C Sharp]] [[Category:Articles with example C Sharp code]] [[Category:Software using the MIT license]]
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)
Pages transcluded onto the current version of this page
(
help
)
:
Template:About
(
edit
)
Template:Citation needed
(
edit
)
Template:Cite web
(
edit
)
Template:ISBN
(
edit
)
Template:Infobox software
(
edit
)
Template:Multiple issues
(
edit
)
Template:Official website
(
edit
)
Template:Portal
(
edit
)
Template:Reflist
(
edit
)