Question

I have a feature file like so:

Feature: Viewing revenue

Background:
Given invoices
| Date     | Amount |
| 1/1/2010 | 200    |
| 1/1/2011 | 300    |

Scenario: Total revenue
Then the total revenue is
| Label | Revenue |
|       | $500    |

Scenario: Total revenue by year
When I choose to view it by "Year"
Then the total revenue is
| Label | Revenue |
| 2010  | $200    |
| 2011  | $300    |

I want to run some cleanup code after the execution of each scenario. I have a function decorated with the AfterScenario attributes as shown:

let [<AfterScenario>] ``Drop Invoices`` () =
  Invoices.removeAll

The function does get called, but only once, and only when I reload the project through NUnit's gui runner. Subsequent runs of the tests, without reloading the project, do not invoke the function.

Was it helpful?

Solution

The call to Invoices.removeAll seems to be missing a parameter, you may need to add ().

TickSpec expects attributed methods to return unit, so you may need to pipe the result to ignore. You can check the type of the Drop Invoices function by hovering over it, it should say unit -> unit, if it doesn't try this:

let [<AfterScenario>] ``Drop Invoices`` () =
  Invoices.removeAll () |> ignore

There was an issue prior to the November 2012 release of TickSpec where AfterStep and AfterScenario functions were not called if an exception was thrown when executing a step in a scenario. This is fixed in the latest source and the assembly is available via CodePlex or Nuget.

If you have more than one function marked as AfterScenario and any of them raise an exception then subsequent AfterScenario methods will not be called. Ideally your BeforeSceanrio and AfterScenario functions should not throw.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top