3

Is it a good practice to skip the When clause in Gherkin/Cucumber/SpecFlow? For instance:

  • Scenario: user has to open a website
    • Given User has access to website
    • Then User should be able to validate the page

I want to know if this is a good practice. I do not see any errors while running this scenario.

4
  • What's the question , what happened when you use thay
    – PDHide
    May 22, 2020 at 5:55
  • 1
    If its just about syntactic possibility the answer is yes. Cucumber is just an overblown pattern matcher so it's gherkin language doesn't really validate context. If it's about consistency with the spirit of gherkin tests every then should be preceded by a when statement even though it will create bloated, hard to read test case descriptions.
    – Daniel
    May 22, 2020 at 11:57
  • I couldn't understand your question ?
    – Emna Ayadi
    May 23, 2020 at 9:34
  • Is it a good practice to just use Scenario: user has to open a website Given User has access to website Then User should be able to validate the page Just wanted to know if its a good practice, I do see any errors while running this scenario.
    – Reddy
    May 25, 2020 at 5:19

2 Answers 2

4

It depends

The point of the Given/When/Then pattern is that it describes test preconditions (Given), actions (When), and expected results (Then) - very similar to the unit test arrange/act/assert pattern.

In the example you've given the actions are implied: the user navigates to the website. Depending on what's needed to prove the user's access, that could mean the user has already logged on, or it could mean the website lets everyone use it. There's nothing in your example to indicate either way, so a When statement could be helpful to clarify.

While I prefer to see the When statement so it's clear what has to be done to reach the Then condition, when it's the only action that can be taken to reach the Then condition, there may be no need to state it - if that's what the audience of the test prefers.

I'd write this test slightly differently:

Scenario: My website does not require special permissions or log on.

  • Given I have an open web browser
  • When I navigate to mywebsite.mybogushost.org
  • Then I see the full site

Your scenario could also be handled as:

  • When I go to my website
  • Then I see the full site

because there are no real preconditions.

2
  • +1 for your second suggestion, which is to forgo the Given. Each scenario should have a When and a Then. If your scenario legitimately has no prerequisites, then no Given steps are needed. It feels weird for a test to not have a When or Then. The action and expected result is the whole point of a test, even a BDD test. The prerequisites only exist to get you to the When so you can assert the Then. May 26, 2020 at 19:02
  • Thank you, will try your second suggestion.
    – Reddy
    May 30, 2020 at 3:44
2

No, the when is the action you are testing.

Why not write it as:

Scenario: User has to open a website
  Given Joey (a typical user) that has access to the website
  When Joey visits the page
  Then Joey is be able to validate the page content

You can technical skip step phases, you can also leave the implementation of a step empty. Sometimes I create steps and only write a comment in the implementation, e.g. // do this manual, like ...

I would describe the users action.

Suggest you read this article of the original Cucumber developer and understand that it is NOT a test tool:

The world's most misunderstood collaboration tool

...

This crowd has pretty much fallen into the same trap as the Rails developers - they use Cucumber uniquely as a testing tool. No collaboration. No outside-in.

https://cucumber.io/blog/collaboration/the-worlds-most-misunderstood-collaboration-tool/

Now if you think your steps help collaboration, then it is fine to skip the when or the given for that matter.

Your Answer

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

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