randombits
randombits

Reputation: 48490

Cucumber: Before hook run only once for all scenarios

I have a scenario outline with multiple scenarios. I'd like my Before hook to run only once so I can bootstrap the ActiveRecord objects I need to run against all of the scenarios. The problem is if I use

Before do
    # my code here
end

This will execute before each Scenario. Is there anyway to run it once for the entire Outline?

Upvotes: 9

Views: 6955

Answers (2)

Roger Garza
Roger Garza

Reputation: 1553

I had the same problem, where I needed to create a subscriber manager once for all of my event logging tests. If I just used a before hook or a regular step (e.g. a Given), the manager would be created before each scenario.

My solution was ultimately to use a tagged before hook on my first scenario.

Before('@first_logging_scenario') do
  # do something useful
end

To shutdown my manager, I used a tagged After hook with my last scenario

After('@last_logging_scenario') do
  # do something useful
end

Upvotes: 0

Ryan Bigg
Ryan Bigg

Reputation: 107728

I think if you simply create the objects in a file in features/support they will be persisted:

ImportantThing.create(:name => "USEFUL THING")

This is because before every Scenario Cucumber will start a database transaction and then rollback to its prior status, which should contain the objects you've loaded.

Upvotes: 4

Related Questions