When I'm doing an end-to-end test by using cucumber, where would the testing flow stop? For example, we have several steps here:

  • I walk into a shop
  • I select some items
  • I pay for them
  • I get my receipt
  • I walk out of the shop
  • I walk to my car

If the first part of cucumber tests like this:

Given I walk into a shop
when I select some items
And I pay for them

Then, what assertion should I use? I expect the result should only to be I get my receipt, but it seems I walk out of the shop will also work since it will also be the result after I pay for the items as long as it will happen after the payment, and so does I walk to my car. Then it becomes unclear. How does the cucumber Then work? Why would it assert the two different assertions/stages to be right? Thanks!


5 Answers 5


Think about what you are testing.

Are you testing that a user can do shopping in your supermarket and receive proof of purchase or are you testing that a user can still walk after a visit to your supermarket?

What's the end goal - the outcome that you are looking for to be like "That process works".

In my opinion:

# BEFORE - I walk to the shop
Given I have entered the shop
When I pick up some items
And I pay for my items
Then I should receive my receipt
# AFTER - I leave the shop and walk to my car

With cucumbers hook functionality, you can ensure that these steps are carried out (setup and cleanup). For instance, I would register my user before my test, and then in my test I would log in, do some things, and after the test, I would log out or remove the user to reset the environment (and essentially complete the end to end). The act of registering and logging out / removing the user are not what we are testing, which is why I don't include them in the test script.


It depends on what you are testing.

I could imagine one test focused on the receipt and it being correct.
Another test might be focused on the shop being occupied.
Another test might be focused on the location of the shopper.

They could also be three expectations in one test depending on the specifics.
Generally you aim for one assertation per case if you can.


Foremost thing to look at it is what are you going to verify in your E2E tests. E2E test here seems to be Given I have walked into the shop And I have selected some items When I pay the bill Then I should get my successful payment receipt

Your main goal of the test on the above scenario would be to verify the user can purchase items and gets the proof of it. If you also give "And I walk out of the shop" this step may not be mandatory in this particular case Because user can still be able to shop more items as long as he is there. If user wants to walk out of the shop that is completely another scenario. Out of Scope for this test. Walking out of shop involves many other steps too.

Hence it would be ideal to get more focused on the area that needs to verified and assert there.

  • Thanks! Yes from the point of testing, we should assert that I get the receipt, but it seems that if we assert that I walk out of the shop or I walk to my car will also make the test passed. So why would this happen? Thanks!
    – laahaa
    Jul 8, 2018 at 16:38

Then's in Cucumber assert something that confirms or rejects the behaviour that has been exercised by the When. So examine the When to determine the Then. In your example your when isn't very clear and comprises of many steps which is perhaps why you are having some issues. Lets simplify your When and then look at the Then.

When I buy some products
Then I have bought some products

When I add some products to my basket
Then I should have some products in my basket

When I pay for my items
Then I should have paid for my items

When I foo
Then I should have foo'd

All of these follow the same pattern and have some common characteristics

  1. A single When matching a single Then
  2. All are happy path we are not dealing with errors
  3. All define a name for something that is important to your business context
  4. None of them have anything to do with HOW you do something

The art of Cucumber is to use your control of natural language and your understanding of your business context to create scenarios that fit this pattern and communicate effectively. If you do this then you get a great deal of help in driving the development of each piece of behaviour that you have define in you When.

After you have your basic behaviour defined then you can use additional scenarios to explore sad paths.

The scope of your scenario ends when you have proof that your When is complete. In your example things like 'leaving the shop' and 'walking to the car' are irrelevant. They don't confirm anything to do with buying products.

Perhaps you are thinking that because your Givens mention them then your Thens should too. This is wrong Thens are only interested in Whens.


There are two things:

First, why would you verify something external to the application like 'walking out of shop' and should verify the receipt as single assertion in the end of the test.

Secondly, all gherkin keywords like 'Given' ,'when' and 'then' are sugar coating, under the hood , 'technically' it does not matter what code you bind to them and how many times.

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.