Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to share state between scenarios using cucumber

I have a feature "Importing articles from external website". In my first scenario I test importing a list of links from the external website.

Feature: Importing articles from external website
  Scenario: Searching articles on example.com and return the links
    Given there is an Importer
    And its URL is "http://example.com"
    When we search for "demo"
    Then the Importer should return 25 links
    And one of the links should be "http://example.com/demo.html"

In my steps I have the 25 links in a @result array.

In my second scenario I want to take one of the links and test the fact that I parse the article correctly.

Now obviously I do not want to go to the external website every time, especially now that the first scenario passes.

How do I proceed here so I can keep testing without making the HTTP requests for the first scenario? Or should I run it once and persist the @result array across the rest of the scenarios so I can keep working with the actual result set?

like image 380
Fred Fickleberry III Avatar asked Jun 22 '12 21:06

Fred Fickleberry III


1 Answers

This is intentionally very difficult to do! Sharing state between tests is generally a Very Bad Thing, not least because it forces your tests to run in sequence (your first scenario MUST run before the subsequent ones, not something Cucumber supports explicitly).

My suggestion would be to re-think your testing strategy. Hitting external services in tests is a great way to make them run slowly and be unreliable (what happens when the external service goes down?). In this case I'd suggest using something like webmock or vcr to create a fake version of the external site, that returns the same response as you'd expect from the real site, but you can hit as many times as you like without the worry of performance or unavailability.

like image 191
Jon M Avatar answered Sep 21 '22 15:09

Jon M