0

I am looking for a solution for the following scenario. I am writing the cucumber-capybara tests for the Ruby on Rails application.

I have multiple cucumber feature file with several scenarios (say Scenario B...Z) which are dependent on one particular scenario (say Scenario A). I want to run the scenario only once for all the scenarios which are dependent on it. So if i run the scenarios (B....Z) I wanted to run the dependent scenario (A) only once. I came across Before hook in cucumber but it will run once for every scenario.

I have one feature file and in that there is one scenario which provisions a server. (Scenario A)

I have multiple other feature files and multiple scenarios (Scenario B..Z) which will run the tests assuming that the server is actually provisioned (Scenario A)

So whenever someone runs the dependent scenarios (Scenario B..Z ), it should check if server is provisioned by some other scenario then it should not try to provision the server. As it will increase the no.of servers.

1
  • So, based on my comment bellow, You can add a check to see if server is provisioned in your Given block. If yes, do nothing, else: provision a server
    – x6iae
    Jan 28, 2016 at 10:12

1 Answer 1

0

You could write a feature with scenario A, and then write a Given block which will set the initial expectation for what scenario A is about, Then you can call that before scenario B..Z

So, assuming scenario A is to sign in, then write a single feature for signing in, and then for scenario B to Z, you write a given block that set a user as signed-in, and use that as:

Given user is signed in
...

where Given user is signed in is defined like:

Given /^user is signed in$/ do
  # code to sign a user in
end
5
  • Added more information to the question. By referring to your example , if I wanted to check that user should log in only once even if the "Given user is signed in" is called from multiple feature files , how can I do that ?
    – Anupam K
    Jan 28, 2016 at 9:04
  • You can add a check in the Given user is signed in block, to see if there is a user signed in. If yes, do nothing, else, sign in user
    – x6iae
    Jan 28, 2016 at 10:11
  • I have moved the step definition file inside the support directory and the following if block gets executed always $dunit ||= true value if $dunit then puts "Hey I am runnig now " $dunit = false end For each scenario in different files I always see "Hey I am runnig now " printed on console
    – Anupam K
    Jan 28, 2016 at 12:44
  • This is the complete code I am trying Before('@dependent') do $dunit ||= true if $dunit then puts "Hey I am runnig now " $dunit = false end end
    – Anupam K
    Jan 28, 2016 at 12:59
  • Yes now, you are doing $dunit ||= true. this will always make sure $dunit is true
    – x6iae
    Jan 28, 2016 at 13:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.