Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of Background to run after scenarios

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?)

like image 200
ngm Avatar asked Jun 11 '12 11:06

ngm


People also ask

Does background run before scenario?

A Background is run before each scenario, but after any Before hooks. In a feature file, we should add the Background before the first Scenario. Occasionally we'll find ourselves repeating the same Given steps in all of the scenarios in a feature.

What is the difference between background and scenario outline?

Use scenario outlines to group examples that share the same structure, and put common data in the scenario instead of the example tables. Use background sections to expose contextual information for examples that do not share the same structure.

What is background in BDD?

A Background allows you to add some context to the scenarios that follow it. It can contain one or more Given steps, which are run before each scenario, but after any Before hooks. A Background is placed before the first Scenario / Example , at the same level of indentation.

What is the difference between background and before in cucumber?

Use Background when you provide customer-readable pre-conditions to your scenarios. Use Before when you have to do some technical setup before your scenarios.


1 Answers

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.

like image 63
nemesv Avatar answered Oct 23 '22 17:10

nemesv