Specflow's [AfterScenario]

Vajda picture Vajda · Feb 14, 2011 · Viewed 16.2k times · Source

I have defined [AfterScenario] binding in my stepdefinition class and have method which will be executed after each scenario, but from some reason this isn't working. My method isn't called after each scenario, so excel remains open after some tests... Also there is no log for some unsuccessful tests. Why? Do you have any idea or suggestions?

Here is my method:

[AfterScenario]        
public void AfterScenario()
{
    if (TestContext.CurrentContext.Result.State != TestState.Success) ErrorLog();
    excelDriver.Stop();
}

Answer

Jon Archer picture Jon Archer · Feb 15, 2011

I was playing with the BeforeScenario and AfterScenario stuff myself just today for the first time.

I tagged my scenario with @sometag and then with the attribute on my AfterScenario() method used the same tag (without the '@' or at sign) as an argument, e.g.

Feature file:

@sometag
Feature: Some feature or other
Here is my feature description
...
Scenario: Some scenario
...

Events file:

[AfterScenario("sometag")]
public void AfterScenario()
{
    // stuff...
}

I don't know if tagging stuff like that is the key. Maybe without tags the AfterScenario() would always fire, I didn't try it.

The other thing you can try is attaching VS to the nunit-agent.exe process, setting a breakpoint inside your AfterScenario() method and seeing when it is hit.