Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BDD - Cucumber: Is possible to disable Background logic for only one scenario in feature?

Tags:

cucumber

bdd

In a feature file have a Background and several Scenarios, but now need a Scenario related to same feature that don't have to run background logic, is possible to disable for only a scenario?

UPDATE - Add Example:

Feature: Sign Up

  In order to access to protected parts of site
  A user
  Should sign up

  Background:
     Given I am on sign up page
     And I am not logged in

  Scenario: User sign up succesfully
    When I sign up with valid fields
    Then I should view dashboard page 

  Scenario: User altredy sign up
    When I sign up with altredy registred user e-mail
    Then I should view altredy sign up message and link to forgot password page

  Scenario: User try to sign up with missing/wrong data
    When I will try to sign up with missing/wrong data
    Then I should error message

  Scenario: User altredy sign in
    #here disable background
    Given I am logged in
    When I am on sign up page
    Then i should be redirect to dashboard page
like image 237
byterussian Avatar asked Mar 27 '12 20:03

byterussian


People also ask

Can a feature file have multiple backgrounds?

There can be only one Background in one Feature file and it allows us to set a precondition for all Scenarios in a Feature file. A Background is like a Scenario, containing a number of Steps. Background is run before each Scenario, but after the BeforeScenario Hooks.

How do I run background only once?

If you want your background to be run only once. You can add condition with an instance variable ex, i==0 then execute the logic and increment i at the end of the method. For the next scenario, i value is 1 which is not equal to 0,the it won't execute the logic.

Is background necessary for a feature file?

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.

What are the advantages of using background in Cucumber?

Using Background in CUCUMBER, we can make the feature file more readable and less complex in lieu of writing steps over and over again for each scenario. When we execute the feature, at run time, the steps in Background are executed in the beginning of each scenario.


1 Answers

Solution 1.

You could put that only Scenario in other Feature file where there's no background or it has it's own background

Solution 2.

Remove the background from the feature file and then put its logic on the step definitions, something like

Given 'I am on sign up page' do
    some code here
end

Given 'I am not logged in' do
    some code here
end

then on each first step

Given 'I sign up with valid fields' do
    step 'I am on sign up page'
    step 'I am not logged in'
    the rest of your code for this step
end

Given 'I sign up with altredy registred user e-mail' do
    step 'I am on sign up page'
    step 'I am not logged in'
    the rest of your code for this step
end

Given 'I will try to sign up with missing/wrong data' do
    step 'I am on sign up page'
    step 'I am not logged in'
    the rest of your code for this step
end

This is not pretty though you would repeat those at least 3 times.

Solution 3.

You could get rid of that Scenario and paste it's steps in the first Scenario, something like

Scenario: User sign up succesfully
    When I sign up with valid fields
    Then I should view dashboard page    #this is your Given I am logged in step
    When I am on sign up page
    Then i should be redirect to dashboard page
like image 70
Gus Avatar answered Sep 21 '22 22:09

Gus