Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using variable with Background in Cucumber

Tags:

tdd

ruby

cucumber

I'm trying to run a feature file like this:

Feature: my feature

Background:
  When I do something
  And I choose from a <list>

Scenario Outline: choice A
  And I click on <something> after the choice A is clicked

Examples:
  | list | something |
  |  a   |   1  |
  |  b   |   2  |
  |  c   |   3  |

But what happens is when the second Background step runs, in the step definition, list is a String with the value <list>, and the first Scenario line something is 1, so can Background not use the variables from Examples? Putting a copy of Examples before Scenario Outline does not work.

like image 625
RenaissanceProgrammer Avatar asked Sep 08 '15 23:09

RenaissanceProgrammer


People also ask

Can I use background with examples in Cucumber?

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.

How do you use background feature in Cucumber?

The Background keyword is applied to replicate the same steps before all Scenarios within a Feature File. It should be used for defining simple steps unless we are forced to bring the application to a state which requires complicated steps to be carried out. As requested by the stakeholders of the project.

Can I use scenario outline with background?

There is nothing special you have to do to combine these all in a single feature file. Declare and use them like you do while declaring multiple scenarios in a feature file. Background will be common for all scenarios and scenario outline. Also, Scenarios and Scenario Outline will be independent of each other.

Can we use scenario outline in background in Cucumber?

Background in Cucumber is used to define a step or series of steps that are common to all the tests in the feature file. It allows you to add some context to the scenarios for a feature where it is defined. A Background is much like a scenario containing a number of steps.


1 Answers

The answer to your question is: no. Background is not a scenario outline. It does not take values from Examples, which is exclusively for the Scenario Outline where it is included. Let's suposse you have several Scenario Outlines. Each of them should have its own Examples sections and it is not shared between them. Consequently, it is not shared with Background either. That is why it does not work when you move Examples before Scenario Outline, as you mentioned in your question.

like image 58
lourdes Avatar answered Sep 29 '22 14:09

lourdes