Reputation: 2415
I am using NUnit to test a large web application with Selenium.
We are depending on a number of exceptions, either custom or provided by Selenium, to fail the tests. NUnit features a Retry
attribute, which repeats a failed test, but only on an explicit assertion error.
Is there any way to handle exceptions as failed assertions by default? Wrapping every relevant statement, or every test as a whole in
Assert.That(TestMethodDelegate, Throws.Nothing)
as suggested here would involve a lot of work and additional boilerplate.
I understand this goes against intended behavior, but I need to bend some rules to tame the misguided nightmare that is Selenium testing.
Upvotes: 5
Views: 6438
Reputation: 57
The easy way to do this is the following:
if (TestContext.CurrentContext.Result.Outcome.Status != TestStatus.Passed ||
TestContext.CurrentContext.Result.Outcome.Status != TestStatus.Skipped)
Assert.Fail("Test failed due to exception");
Now every exception becomes a failed assertion and NUnit's Retry will do what it should.
Note: there is a very good reason NUnit does not do this by default. It's designed for unit tests and there, assertions and errors should be handled differently.
However when dealing with Selenium, that falls apart due to server timeouts, etc. so having retry on error functionality is very useful.
Upvotes: 1
Reputation: 22657
You could take the code from the NUnit RetryAttribute and write your own version for your tests. I believe you would just need to modify the if statement in the Execute
method to check for failures and errors.
Upvotes: 3