1

Is there any way to declare a variable in a feature file to then use in a cucumber test? Something like this:

myFile.feature

Given whenever a value is 50

myFile.java

@Given("^whenever a value is 50$")
public void testing(value) {
    assertEqual(value, 50);
}

Honestly, I don't even know what this would look like. But I would love to not have to declare a value in both the feature file AND the Cucumber test. Thanks!

kroe761
  • 3,296
  • 9
  • 52
  • 81
  • Why would you want to do this. Give a better example and maybe someone can give a better answer. – diabolist Feb 22 '16 at 16:17
  • kroe761 You should accept David Baak's answer, it is the right one. @diabolist Seriously? It is done **all the time**. – MikeJRamsey56 Mar 26 '16 at 05:12
  • @MikeJRamsey56 so you think David Baak's answer, answers the question! I suspected OP was after something else, that answer is just too obvious, and has nothing to do with 'declaring a variable in a feature file'. Anyhow we'll never know for sure unless OP comes back and clarifies – diabolist Mar 27 '16 at 14:33
  • @diabolist I just reread the question and all I can conclude is that kroe761's question **is** that obvious. Someone who is so new to cucumber that he is unaware of its parameter passing features (no pun intended). I can see now that you were looking for a deeper question. Pronouns like *this* are open to interpretation. I didn't understand what you meant. I do now. :-) – MikeJRamsey56 Mar 27 '16 at 14:49
  • Well, this was interesting. I have just accepted the answer, and yes, I was just that new/dumb in Cucumber. Thanks! – kroe761 Mar 28 '16 at 15:43

1 Answers1

3

Yes you can! Write the Given-step in the feature.

Feature: foobar

Scenario: something
    Given whenever a value is 50

Then run it as a JUnit test. You will see something like this in the console.

@Given("^whenever a value is (\\d+)$")
public void whenever_a_value_is(int arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();
}

Then you can copy+paste it and change it to:

@Given("^whenever a value is (\\d+)$")
public void whenever_a_value_is(int value) {
    assertEquals(value, 50);
}
David Baak
  • 944
  • 1
  • 14
  • 22