Munro89
Munro89

Reputation: 13

Specflow: Can some step definitions be used feature wide?

When a feature requires some given steps, you need to specify for each scenario "Given I have done something And something else And the last thing...". Doing all those steps for every scenario can be tedious.

One solution could be to specify all this in one Given step (Given I am set up to test feature X). However, it's not something very precise while reading the feature steps.

This is why I'd like to know if step definitions can be used feature wide. I'd use something like this :

Feature: My feature

Obvious feature description here.

Given I have done something
And something else
And the last thing

Scenario: Y validation

When I type X
Then I should see Y

Scenario: Z validation

When I type X
Then I should see Z

Over this :

Feature: My feature

Obvious feature description here.

Scenario: Y validation

Given I have done something
And something else
And the last thing
When I type X
Then I should see Y

Scenario: Z validation

Given I have done something
And something else
And the last thing
When I type X
Then I should see Z

Any solution is welcome, but I'd like something that can be understood while reading the .feature file, instead of having to dig in the code.

Thanks

Upvotes: 1

Views: 689

Answers (2)

fancypi
fancypi

Reputation: 97

When using the Backgroung tag your scenarios and feature will look more or less like this:

Feature: My feature
     As a person
     I want to do something
     So that something can happen

Background: My Background
Given I have done something
And something else
And the last thing

Scenario: Y validation
      When I type X
      Then I should see Y

Scenario: Z validation
      When I type X
      Then I should see Z

Upvotes: 1

Marcus Hammarberg
Marcus Hammarberg

Reputation: 4822

There a great and easy solution to your problem in Gherkin, the language used to write feature files. It's called Background and is a couple of steps that is executed before each scenario in the file.

See this wiki page for more information; https://github.com/cucumber/cucumber/wiki/Background

Upvotes: 3

Related Questions