All About xUnit
Testing Exceptions
When the code under test is expected to throw an exception,
the test must include special handling.
Otherwise, the test would terminate abnormally, whereas it should really pass
if the exception is thrown as designed.In most situations, the easy way needs very little code to checks for an exception.Call the code under the test with no special try-catch handling. Instead,
for NUnit (starting with version 3.x) use the the test must include special handling.
Assert.Throws()
function. The first parameter is the typeof
the expected exception and the second parameter is the method to test wrapped in a delegate: For MSTest include the expected exception's type — using typeof
or the type's literal name — as a test attribute:
Note the differences between NUnit and MSTest. For MSTest when you use the
ExpectedException
attribute with the optional string argument it uses the string
as the message to display if the exception is not thrown.
In NUnit you can get full control by capturing the returned exception from the
Assert.Throws()
function. In MSTest you can
get full control by writing the handling code yourself.
(Reading...)
Formatting Hints:
To format... | use this markup: |
---|---|
source code | {source:lang=java|lineNumbers=false}...{source} (Supported languages: java, cpp, csharp, c, python.) |
bold text | __some text__ |
italicized text | ~~some text~~ |
underlined text | @@some text@@ |
a method or class name | ``myMethod(...)`` |