5

I'm looking for suggestions or best practices here.

Sometimes I would like to give the same regex/phrase for 2 different steps using different prepositions. The case I see often is a phrase that makes sense in both Given and in Then steps.

Given being used for a step that performs and action (to setup the test state). And Then being used for a step that verifies the state, but doesn't perform an action. Quite often I find the same phrasing works right for both. An example such phrase is "I am logged in"

Here is a contrived scenario

Scenario: Login is saved between sessions.
    Given I am logged in
    When I exit the application
    And I start the application
    Then I am logged in

Of course this doesn't work in the step definitions because cucumber doesn't differentiate between step predicates.

I've toyed with adding 'verify' to all colliding Then steps, or adding a 'that' to all duplicated Given steps. But nothing sits right.

Given that I am logged in.
...
Then verify I am logged in.

It works of course, but I feel I'm missing something that doesn't require having the same "extended predicate" for most of my steps.

DragonFax
  • 4,625
  • 2
  • 32
  • 35
  • 3
    You can use `Then I should be logged in` – Andrei Botalov Feb 28 '13 at 08:09
  • Yeah, thats what I'm doing. Just choosing a slightly different form of the same sentence. I guess standardizing on that in your tests is the only way. I really wish you could define When and Then steps with the same names. – DragonFax Mar 01 '13 at 04:23
  • Possible duplicate of [Two Step Definitions that are the same but one is for Given and one is for Then](https://stackoverflow.com/questions/47107183/two-step-definitions-that-are-the-same-but-one-is-for-given-and-one-is-for-then) – Marit Dec 30 '17 at 10:57

1 Answers1

1

I've had the same question, this is the answer I got:

Step Definitions have to be unique for Cucumber to know what to execute. In addition, the Given/When/Then keywords are technically interchangeable. They're for the readability of the feature file, but not linked to the implementation. So if both steps (Given and Then) do the same thing, there is technically no problem; you should be able to use the same step definition from your feature file, preceded by either 'Given' or 'Then' keyword. That said, you might want to consider rewriting your step definitions to describe the intended behavior instead of the implementation, e.g. "Given an element with id xxx"

snikt
  • 581
  • 4
  • 11
  • 27