NUnit
≈ 3 min read
Names in other languages
- ČeštinaNUnit
- DeutschNUnit
- EnglishNUnit
- EspañolNUnit
- FrançaisNUnit
- ItalianoNUnit
- РусскийNUnit
- УкраїнськаNUnit
Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.
NUnit is an open-source unit testing framework for .NET, .NET Framework, and Mono. It serves the same purpose as JUnit does in the Java world, and is one of many in the xUnit family.
NUnit is an open-source unit testing framework for .NET, .NET Framework, and Mono. It serves the same purpose as JUnit does in the Java world, and is one of many in the xUnit family. [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"); }); } // 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); 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 // 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 (MethodThatThrows); Assert.Throws ( delegate { throw new ArgumentException(); }); // Using C# 3.0 Assert.Throws ( () => { 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 ( 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 () .And.Message.EqualTo("message") .And.Property("MyParam").EqualTo(42), delegate { throw new MyException("message", 42); }); } } The NUnit framework discovers the method ExampleTestOfNUnit.TestMultiplication() automatically by reflection. Article Improving Application Quality Using Test-Driven Development provides an introduction to TDD with concrete examples using Nunit Open source tool, which can execute nunit tests in parallel Charlie Poole, co-developer Rob Prouse, co-developer Simone Busoli, co-developer
Text derived from https://en.wikipedia.org/w/index.php?oldid=1367315433 under CC BY-SA 4.0; modifications have been made.