1

As I understand it based on this Given When Then wiki page, Given steps interact with the app to set the precondition state, When steps interact with the app to attempt to set the desired new state being tested, and Then statements read the state of the app without modifying it.

Is reusing When steps as Given steps for subsequent states a good idea?

For instance, in a simple shopping cart application, I might write:

Given the user is interested in some item
When the user adds the item to their cart
Then the cart will include the item

Given the user adds the item to their cart
When the user checks out
Then the user will see a summary of their purchase including the item

Given the user checks out
When the user cancels an item
Then the item should be canceled
And the user should be refunded

There is a similar question here reusing the user's previous interaction in a wizard, but it seems to disagree with Uncle Bob's finite-state-machine interpretation in that the answer suggests making the steps much less rigorous, while Uncle Bob implies that the steps ought to be rigorous enough to make an intelligible state transition diagram out of them. I completely accept the advice to remove all the user-interface jargon from them and focus only on business terms, but there does seem to be a difference between logically connecting business terms as I have tried to do here, and just making logically unconnectable steps that are only connected by "unseen" glue code.

1

4 Answers 4

2

So this is a bit of a weird territory because its one of those things where you CAN but you really shouldn't. Let me explain.

As you mentioned:

Given steps interact with the app to set the precondition state

When steps interact with the app to attempt to set the desired new state being tested

Lets keep that in mind as we look at your example

When the user adds the item to their cart

This step is actually pretty good, it lacks implemntation language and discribes a user behavior. 10/10

Given the user adds the item to their cart

This step, while it works grammatically, violates the rules we set up about what Given and When steps do. We have already established When is for interaction, and Given is for precondition. However this Given is describing an action being taken, not a state or precondition. A better way to write this may be:

Given the user has an item in their cart

This specifies a precondition and not an action, so its proper gherkin.

So now you may be thinking "But what about code reuse!?!?!?", this is where flows and libs come in handy. If you find yourself reusing the code often, move it in to a lib that you can call to reference that action in your stepdefs, this way you can keep both your feature file in proper gherkin and your stepdefs with as little code reuse as possible

4
  • The point about language is well taken. The tenses aren't correct and don't read well as past-tense preconditions. However, I'm also concerned with "test collapsing" as I'll call it. In my example, I would expect the naïve implementation of cucumber to actually execute "add an item" three times because of the dependency tree I have constructed between the behaviors. A better implementation would execute only the last behavior and recognize that all other behaviors are executed during that last behavior because they are preconditions. Oct 23, 2017 at 21:09
  • But thats putting the cart before the horse. Cucumber (and other frameworks like it) are made to leverage pre-existing gherkin first, and be testing tools second. It may not make 100% sense from a testing standpoint 100% of the time. But the point is to cover Acceptance Criteria with testing. While as code the step might be identical, as Acceptance Criteria they are distinct, and should be seen as seperate. At the end of the day what @alayor said is valid, this is flexible. You can do it in a way that favors code reuse, but that doesn't change the fact that you are modifying ACs for testing.
    – Mo H.
    Oct 23, 2017 at 21:21
  • By dependency tree, I mean that for each behavior, the process of putting the system into the precondition state will actually exercise other behaviors. For instance, to cancel an item, you must have checked out, and to check out, you must have added an item. These are state transitions / behavior dependencies. I have modeled discrete behaviors that I am interested in, but the behaviors exist in a business process, steps of which can only be reached by first executing previous dependent steps. Oct 23, 2017 at 21:36
  • While I don't necessarily agree with this approach, as it couples your steps to features by restricting where they can be used. I don't see why this can't still be done with libraries and context variables.
    – Mo H.
    Oct 23, 2017 at 21:39
1

You're correct in that Gherkin scenarios describe steps that Arrange, Act, and Assert within the application under test.

Uncle Bob's finite state machine can be described in Gherkin, if you have enough Gherkin statements. It's important to remember, though, that each scenario in Gherkin starts from the initial state, not from the state at the end of the scenario located above it in a feature file.

To rephrase, each scenario MUST stand on its own.

The potential duplication of test steps is of less importance than describing a complete scenario. One reason is that test runners may selectively run scenarios, which would cause test failures if the precursor scenario was not executed. Other test runners may run scenarios in parallel, which would also play havoc if such dependencies were allowed.

It's perfectly acceptable, and common practice, to have a Given step repeated in several scenarios.

Your example would be more properly stated something like:

Given the user is interested in some item
When the user adds the item to their cart
Then the cart will include the item

Given the user has added an item to their cart
When the user checks out
Then the user will see a summary of their purchase including the item

Given the user purchases an item
When the user cancels the item
Then the item should be canceled

The third Given statement could include steps in its definition to have the user put the item in the cart and check out, although it could also simply create an invoice from scratch. That's the beauty of Gherkin: it doesn't matter how the precondition is implemented, the verification is that the action causes the expected outcome.

1
  • 1
    I'm contemplating accepting this as the answer because you have focused on the scenario. At the time I didn't appreciate that because I was focused on the problem of how to manage application state during test, but three years later I think you might have hit the nail on the head. Oct 19, 2020 at 21:58
0

I don't think you should re-use When steps as Givens. Instead you should build on the functionality the When step has interacted with to create your Givens. In general each When step has the potential to have several Given steps built on its functionality.

In some cases you will do exactly the same thing in each step. For example logging in

When "I login" do
  visit login_path
  fill_in id
  fill_in password
  ...
end

Given "I have logged in" do
  visit login_path
  fill_in
  ...
end

To make these steps work well lets introduce a helper method

module LoginStepHelper
  def login(as: )
     visit ...
  end
end
World LoginStepHelper

When "I login" do
  login as: @i
end

Given "I have logged in" do
  login as: @i
end

Now we get the benefit of better grammar for a Given at almost no cost, and we can build on this to make other useful steps

e.g.

Given Fred is logged in
Given I am logged in as an admin
...

In most cases you have an opportunity to do something different for your Given. Lets say we want to register a user. For the When we will interact with a form through some sort of UI. However for the Given we can bypass the UI. In our implementation we would have

When I register
  visit registration_path
  fill_in ...
  ...
  submit
end

whilst our Given could be

Given I am registered
  register as: @i
end

module RegistrationStepHelper
  def register(as:)
    CreateRegistration.call( ...
  end
end

Our Given has a couple of ways of creating the registration

  1. Call directly the service used to create registrations with an appropriate params hash
  2. Call a factory/fixture creator to directly write a registration record to the database.

Of these two the first is far superior.

Now you are getting better grammar and a vast decrease in runtime cost. This becomes particularly important when you want to do complex interactions involving a great deal of existing functionality e.g. a registered customer doing a re-order on an ecommerce site.

0

In case you are using Javascript, I've created a package named reuse-cucumber-scenarios for calling a scenario by doing:

Given the scenario "@scenario_tag"

.

Given the scenario "@scenario_tag" with parameters
"""
{
  "1": ["step1_param1", "step1_param2"],
  "2": ["step2_param1", "step2_param2", "step2_param3"],
  "3": ["step3_param1", "step3_param2", "step3_param3"],
}
"""

or creating gherkin variables...

Given the variable "$variable_name" is equal to
"""
#JSON object
"""

or creating scenario functions and calling them by doing...

Given the scenario "@$scenario_function_tag" where variable "$variable_name" is "value_to_replace"

and more...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.