8
votes

I'm new to SpecFlow, and am setting up a number of test features/scenarios that are for authenticated users of different role types. I'm doing this via browser automation with Coypu.

So I have a Background step in the feature to set up the log in of a user in that role type.

Background:
    Given I am logged in as a ...some role I'm interested in...

After each scenario in the feature, I want to log the user out again (otherwise the log in step in the Background won't work for the next scenario -- I'm keeping the same Coypu browser instance open in between tests).

I found the [AfterScenario] annotation that I could use, but as this is scoped across all scenarios in all features (as far as I understand...) it would affect scenarios say for unauthenticated users.

I could scope the [AfterScenario] with [Scope(Feature="Some Feature")] I believe, but I'm anticipating having to log in/out before and after a whole number of features that I'm testing, and I'm not keen on specifying all of these with a bunch of magic strings.

So I'm wondering if there's something I can put in the Feature file, kind of the equivalent of Background but to run after each scenario in that feature. (Or alternatively, maybe the way I'm logging in/out for each scenario is not the best way to go about things?)

1

1 Answers

13
votes

There is no "Postground" feature in specflow however you can achieve something similar with tags filtering.

In most of our projects we are using tags to mark scenarios which have specific setup/teardown logic. Then we are using the BeforeScenario/AfterScenario hooks to execute the logic:

[BeforeScenario("authentication")]
public void BeforeAuthenticationScenario()
{
    //...
}    

[AfterScenario("authentication")]
public void AfterAuthenticationScenario()
{
    //...
}

And you can tag individual scenarios or whole features:

@authentication
Feature: Some feature requires authentication

@authentication
Scenario: Some scenario requires authentication

So in your code you will only have one magic string "authentication" and in your features you can apply the custom logic declaratively with the tag.