This article has multiple issues. Please help improve it or discuss these issues on the talk page. (Learn how and when to remove these messages)
|
NUnit is an open-source unit testing framework for the .NET Framework and Mono. It serves the same purpose as JUnit does in the Java world, and is one of many programs in the xUnit family.
Original author(s) | Charlie Poole, James Newkirk, Alexei Vorontsov, Michael Two, Philip Craig, Rob Prouse, Simone Busoli, Neil Colvin |
---|---|
Developer(s) | The NUnit Project, .NET Foundation |
Stable release | 4.0.0
/ 26 November 2023 |
Repository | github |
Written in | C# |
Operating system | .NET Framework, Mono |
Type | Unit testing tool |
License | MIT License for 3.0, BSD-style (modified zlib license) for 2.x |
Website | www |
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.
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.
public void ComplexNumberTest()
{
ComplexNumber result = 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 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);
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.[citation needed]
Example of an NUnit test fixture:[citation needed]
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); });
}
}
The NUnit framework discovers the method Latest online Nunit Tutorials with example so this page for both freshers and experienced candidate who want to get job in Nunit companyExa
Latest online Nunit Tutorials for both freshers and experienced
advertisements
View Tutorials on Nunit