Alex Amorales
Alex Amorales

Reputation: 323

JBehave - best way to reuse/refer already existed story

Each story in my BDD tests starts with the same bunch of steps. Is there any way to refer this steps or maybe somehow to "refer" repeatable story. What is the best way to extract this common part? Currently, I am using @Composite annotation provided by JBehave.

Upvotes: 1

Views: 1566

Answers (2)

Ayman Alkhalil
Ayman Alkhalil

Reputation: 31

You can use GivenStories by setting all your steps in a story and call it in other stories like:

GivenStories: path/to/precondition2.story,
          ...
          path/to/preconditionN.story

Given ... // normal scenario steps

You can also send parameters to those steps:

Scenario:  A scenario in which the user can run other stories as pre-requisites
       parametrized using the rows of the Examples table

GivenStories: path/to/precondition.story#{0},
          path/to/precondition.story#{1}

Given ... // normal scenario steps

Examples:
|One|Two|
|uno|due|
|un|deux|

reference: http://jbehave.org/reference/stable/given-stories.html

Upvotes: 1

lauda
lauda

Reputation: 4173

The solution would be to use a Background scenario.

What this does is to execute the steps from the Background for each scenario. A disadvantage would be that if the Background fails then all the scenarios from that feature will be skipped and the feature will be marked as failed.

I guess is assuming that if the steps are common and they fail once then they fail every time. You can see an example in JBehave documentation.

Upvotes: 1

Related Questions