Can you use both @Given and @And for a certain step?

Robert van der Spek :

I have several tests which share Step Definitions. For example:

Scenario: test1
  Given that the sky is blue
  And that the sun is up
  When I go outside
  Then I might get a sunburn

Scenario: test2
  Given that the sun is up
  When I go outside
  Then it will be light

Both steps "And that the sun is up" and "Given that the sun is up" are equal in their implementation.

What I would like is this:

@And("that the sun is up")
@Given("that the sun is up")
public void thatTheSunIsUp() {
    // Do some fancy and sunny things.
}

Unfortunately this does not work. How can I achieve the same, without having duplicate methods for equal steps?

KyleFairns :

Every step in cucumber is defined as a Given, When or Then, but in reality it's more like:

// ENTER PSUEDOCODE
@Step("that the sun is up")
public void thatTheSunIsUp() {
    // Do some fancy and sunny things.
}

The keywords are interchangeable, which allows for context as to whether it's a prerequisite (Given) an action under test (When) or an outcome (Then).

Defining it as you have originally (without the duplicate @And section), you would be able to use Given, When, Then, And, But and * in your feature file as the keyword, and cucumber's backend should match your step, but what you use for your definition should match up with it's intended use (as described in the previous paragraph)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=329664&siteId=1