For example the code below,
[Test()]
public void Test( )
{
try{
GetNumber( );
}
catch( Exception ex ){
/* fail here */
}
...
}
I want to fail my test when GetNumber method throw an exception.
Please advise.
Many thanks.
Assert. Fail(); Assert. Fail(string message, params object[] params); Here's an example of its use to create a private assertion that tests whether a string contains an expected value.
You can add a new attribute in the nunit based on the attribute repeat and rebuild the library. It's very simple. Show activity on this post. Starting with NUnit 3.0, there is a 'Retry' attribute that looks like it will do exactly what kumar wants.
Ignored tests are displayed by the runners as warnings in order to provide a reminder that the test needs to be corrected or otherwise changed and re-instated.
You don't need to wrap GetNumber() inside a try/catch. If GetNumber() throws, your test will fail.
If you need to fail it explicitly, use Assert.Fail();
If GetNumber()
returns a value, you shouldn't do what you're trying to do. Instead, you should assert the return value. Don't bother checking for exceptions if you don't expect one to arise. The NUnit framework will take care of that and fail your test for you.
If GetNumber()
doesn't return a value, you can do one of three things:
In this case, the first option is the most explicit. This is common if the only interesting side-effect you can validate is if an exception gets thrown. But if GetNumber()
doesn't return a value, you should really consider renaming your method :)
All test should pass, if you are expecting an exception you should use ExpectedException attribute. If your code throws the expected exception test will pass.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With