Question

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?

No correct solution

OTHER TIPS

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 "red@cross.com"
  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"

If you don't want your background to be executed for each scenario, then it's not a background, is it?

If you're calling remote services each time you run your Cucumber scenario, don't do that. Instead, use Webmock and VCR as described at http://marnen.github.io/webmock-presentation/webmock.html. Your tests will be faster and more accurate.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top