Is there a way to implement an AfterScenario
hook to re-run the current test in case of Fail?
Something like this:
[AfterScenario("retry")]
public void Retry()
{
if (ScenarioContext.Current.TestError != null)
{
// ?
}
}
Note: The tests in my project are combined in Ordered tests and executed via MsTest.
The purpose of Specflow scenarios is to assert that a system is behaving as expected.
If some temporal issue causes the test to fail, then getting the test to re-run and "hoping for the best" is not going to resolve the problem! Having a test fail occasionally should not be expected behaviour. A test should give a consistent result every time it is executed.
A great post on what makes a good test can be found here and that answer also states that a test should be:
Repeatable: Tests should produce the same results each time.. every time. Tests should not rely on uncontrollable params.
In this case it's quite right for the test to fail. You should now investigate why exactly the test occasionally fails.
Most often tests fail due to timing issues e.g. an element not being present during a page load. In this scenario given a consistent test environment (i.e. same test database, same test browsers, same network set-up), then again you will be able to write repeatable tests. Look at this answer on using WebDriverWait to wait for a predetermined amount of time to test for the existence of expected DOM elements.