5

So im trying to get better at writing Declarative style cucumber tests that make sense, I think the hardest "Keyword" for me to properly form into a feature file is the "Given" keyword.

I know "Given" is meant to set up a stage....but I feel like a lot of things that could be put into the "Given" portion of a feature file, really belong in the "When" portion.

For example lets take a simple "Login" feature file:

What makes more sense.

Given I have entered a valid username and password
When I login
Then I should see X content

OR

Given I have an account for the site
When I login to the site with a valid username and password
Then I should see X content

I know it's just wording, but still im trying to improve my writing style. But im not sure which one actually makes the most sense? (Obviously this is relevant to many examples, login was just one easy one I could thing of)

3 Answers 3

3

As it is described in "The Cucumber For Java Book":

We use Given to set up the context where the scenario happens, When to interact with the system somehow, and Then to check that the outcome of that interaction was what we expected.

That's the basic explanation. But don't forget that Cucumber doesn't really care how you use these keywords. For example you might end up with a scenario that looks like this:

Scenario: Attempt withdrawal using stolen card
  Given I have $100 in my account
  Given my card is invalid
  When I request $50
  Then my card should not be returned
  Then I should be told to contact the bank

And don't forget the And and But keywords that you can use to add more steps to each Given, When and Then section. So you could rewrite the previous example like this:

Scenario: Attempt withdrawal using stolen card
  Given I have $100 in my account
  But my card is invalid
  When I request $50
  Then my card should not be returned
  And I should be told to contact the bank

If you still find Given, When, Then, And, and But a little verbose. There is an additional keyword you can use to start a step: * (an asterisk). So you could have written the previous scenario like this:

Scenario: Attempt withdrawal using stolen card
  * I have $100 in my account
  * my card is invalid
  * I request $50
  * my card should not be returned
  * I should be told to contact the bank

To Cucumber, this is exactly the same scenario.

1
  • Yeah I know it's just wording......but im trying to keep consistency across test cases. All helpful info though!
    – Mercfh
    Commented Dec 16, 2015 at 7:19
4

Given is a precondition.

When is a user action.

Then is testable outcome.

So when we keep this in mind you should choose your second option:

  • Given I have an account for the site (precondition).

  • When I login to the site with a valid username and password (user action).

  • Then I should see X content (testable outcome).

Hope this helps.

-1

Do you need a Given step? I'd argue that you don't.

When I log in
Then I see X content

That's a valid test to me. The fact that the username and password are "valid" is inherent to the passing test. You don't need "Given I have an account for the site" because that's obvious.

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.