How To Automatically Retry Failed Tests in Nunit
Everyone wants to have reliable tests, but no matter how hard you the flaky tests, they keep hunting you and you need to find a way to retry failed tests in Nunit.
A flaky test is a test which could fail or pass for the same configuration. Flaky tests could be harmful for developers because their failures do not always indicate bugs in the code
On one of the c# selenium projects that I’ve worked I had the same issue that you have now , and I tried to use the Retry attribute from Nunit.
But guess what? It re-runs the failed tests but only the one that failed because of an assertion failed.
It didn’t really helped me because I had a lot of tests failing because of the environment that was going down and up.
The solution for me was to rewrite the Nunit retry attribute in a way that was going to help me.
If you have a look at the Nunit RetryAttribute class you’ll see they don’t handle all the possible of the results states.
So what you can do , inside your helper folder you can create a new class named “CustomRetry.cs” and it will look something like this :
You are almost done , the next step will be to tag you test with [CustomRetry(int count)]
If you have any questions please do not hesitate to add a comment or [at] email@example.com.
P.S If you want to share your story with us you can use the ADD POST button on the top :).