Assertions (or asserts for short) are helper methods that can assist us in determining whether a method under test is performing correctly or not. They let us assert conditions, data, and so on. All assertion methods will report failures (that’s when the assertion is false) or errors (that’s when we get an unexpected exception) and report these through the NUnit test runner. when a failure or error occurs, execution of the current test method is aborted. Other Test within the same test fixture will still be run.
Classic Asserts
*message in all previous statements is optional.
Constrain-Based Asserts
These are new assertion style introduced in NUnit 2.4. That new style enable many expectations to be evaluated together in a single assertion:
Assert.That(4, Is.LessThan(5) & Is.GreatThan(0));
NUnit and Exceptions
Part of your code behavior may be throwing a specific exception in certain cases. If you want to assert that your code throws the exceptions that it designed to throw, you need to tag your test method with attribute: [ExpectedException(typeof(ExpectedException))].
1: [TestFixture]
2: public class ImportListTest
3: {
4: [Test]
5: [ExpectedException(typeof(ArgumentNullException))]
6: public void NullList()
7: {
8: Class1.ImportList(null);
9: }
10: }
This test method in now expected to throw an exception [from the call to ImportList(null)]. If it doesn’t, the test will fail. If a different exception is thrown (even a superclass of the one specified), the test fails. The test passes only if the exact exception specified is thrown.
Temporarily ignoring tests
If you wrote the test code first and will start writing code incrementally (something like TDD), you may temporarily ignore tests that you still implementing the code required to pass them. You could just tag these tests with attribute [Ignore(“Optional Message”)] . NUnit will report that these tests were skipped and show a yellow bar in the GUI, so you won’t forget about it later.