API for stateless step definitions #214

aslakhellesoy opened this Issue May 28, 2017 · 2 comments


None yet
2 participants

aslakhellesoy commented May 28, 2017

Currently, the only way to pass state between step definitions is by storing state as instance variables.

This is cumbersome in some languages, in particular for Java where it becomes tricky to share state between classes. DI is the only way to address this.

For other languages (such as Ruby and JavaScript) the World often becomes a huge object holding all kinds of different state used by various step definitions.

I would like to propose an alternative API for Step Definitions where:

  • Each step definition (or hook) can return a value
  • This value is passed to the next step definition
  • Statically typed languages can use custom types

This would remove the need for a World or DI. It would reduce coupling and make it more explicit what is passed between step definitions.

We'll use this issue to discuss the implications of this in various implementations, and to come up with an API for various languages such as Java, Ruby and JavaScript (at least).

Once we're happy with the API we can create new issues to implement this in various Cucumbers.

(I've been thinking about this for a while, and I'm glad I am not the only one! See cucumber/cucumber-jvm#1136)

I think this solution trades a coupling a between step definitions and the World/DI modules for a coupling to the order in which steps in gherkin must be written. So I don't think this can be used to completely replace the World or DI modules. In the worst case you end up passing the World between steps.

For example take a website like Gmail that personalizes your account even when you are not logged in because it recognizes that nobody else ever uses that machine.

Given I am a specific kind of customer
And I have a login account that was previously used to login
When use the website
Then I am greeted with my name

In this scenario I'd have to pass the information about the customer (his name) from the first step all the way to last step through one or more steps that don't use the information.

But other then that yeah this can useful.

mpkorstanje commented Jun 10, 2017 edited

I found some past discussion about the same issue in Scala.


Edit: Wrong button. Sorry.

mpkorstanje reopened this Jun 10, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment