Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cucumber: how to only execute background once for multiple scenarios

I have a feature with a background section which takes multiple minutes; it's setting up state on a remote system via an API. I'd like to have it execute only once for all the scenarios which follow, instead of executing for every scenario. My scenarios don't change any state, they're read-only so there should be no side-effects from one scenario to another.

I'm not using rails, and don't have a local database, so can't do stuff with database transactions.

I'm currently thinking to do something with Before in env.rb and have some conditional code based on an implicit convention/meaning for tags, e.g. "@background-only-once", could make some custom code skip the steps, or it could execute different logic in the step-definitions – but it's a bit nasty to try to share this across executions.

Thoughts?

like image 618
Sune Kaae Avatar asked Nov 05 '22 09:11

Sune Kaae


1 Answers

This doesn't really solve it, but here's what I do in that situation.

I go to one scenario, and just label the different "scenarios" in comments. It's exactly what would happen if you just comment the additional scenario headers out.

Here's an example of commenting out the second scenario, and running of the state of the test at the end of the first feature

Feature: Admin can manage organizations
  In order to ...

Scenario: can add
  When I log into the admin
  When I follow "Organizations"
  When I follow "Add"

  When I fill in "Email" with "[email protected]"
  When I fill in "Name" with "Red Cross"
  When I press "Save"
  Then I should see "success"

# Scenario: can edit
  When I follow "Red Cross"
  When I fill in "Name" with "Green Cross"
  When I press "Update"

  Then I should see "success"
like image 67
Josh Crews Avatar answered Jan 01 '23 20:01

Josh Crews