pfernandez
pfernandez

Reputation: 303

Webdriver, use "AfterScenario" to rerun failed test before report is generated

Is it possible to rerun the current test that has just failed, during the "AfterScenario" phase? As far as I know, a Console.WriteLine during the AfterScenario appears in the report, so the report is generated after the "AfterScenario".

The ScenarioContext class can detect if the Scenario has failed. Maybe it can also launch a scenario? Any other mean to do this is also acceptable.

Edit: To go back to the "BeforeScenario" would probably also work.

Upvotes: 1

Views: 2208

Answers (3)

Andy Smith
Andy Smith

Reputation: 56

If you are using SpecFlow+ Runner (AKA SpecRun) you can set a retry on failure for your specflow tests and then the runner will rerun any failed tests. SpecFlow+ Runner is a commercial product although there is a evaluation version available.

Upvotes: 1

Sam Holder
Sam Holder

Reputation: 32946

I 100% agree with everything that Alski said, in that I feel you are going to be swimming against the tide trying to bend specflow to your will in this way.

However I also want to offer an alternative solution that I would try if I was faced with this problem. I would probably check for test failures in the AfterScenario and then write out a file with the name of the test that failed. Then after the test run, if the file existed then I would use it to rerun the individual failed tests again.

But TeamCity would still be my first choice for doing this.

Upvotes: 0

AlSki
AlSki

Reputation: 6961

I want to say that it isn't possible, but like many things in IT you might be able force it work if you apply enough pressure.

So instead I'll say SpecFlow certainly isn't designed to support this. Consider all the pieces that are normally involved in running tests,

  • The host, such as Visual Studio, Build process (such as a TeamCity Agent) or just a Cmd line. These all kick off the tests by invoking;
  • A test runner. It could be the VS or Resharper test runner, TeamCity Nunit runner or even just nunit.console.exe (or msTest equivalent). This loads up your test dll and scans them for the nunit/mstest [Test] \ [Setup] \ [TearDown] attributes.

  • The test dll contains the code that the SpecFlow plugin generated and maps the nUnit attributes to SpecFlow Attributes.

I think you are only considering that last layer and not the ones on top. So even if you somehow manage to convince SpecFlow and nUnit/msTest to run those tests again, I think you will just end up with errors all the way through the process as VisualStudio, Resharper, TeamCity Agents or CmdLine fail to parse the same tests running twice.


Alternatively why don't you look at optimised test runners to solve your problem.

  • The TeamCity nUnit runner, can run tests that failed last time, first, so you know very quickly if you are going to fail again.
  • NCrunch allows you to choose engine modes, so that you might only run tests that failed previously, or tests that are affected by your code change or both (via a custom rule).

Upvotes: 3

Related Questions