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
(section)
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!
== 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}}
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)