question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow multiple step patterns on the same method

See original GitHub issue

I’ve been working with Cucumber JVM for the past couple of months and have been loving it, but there’s one feature I would love to see and I think others might as well.

Basically, I’d like to do something like this:

@When(
        value = {
                "^I perform the calculation (\\d+) plus (\\d+)$",
                "^I add the numbers (\\d+) and (\\d+)$"
        }
)
void performXPlusY(int x, int y) {
    // do the step
}

I know it’s technically possible to do this via regex, but it’s very ugly, uses extra capture groups, and allows for more than the two desired patterns:

@When("^(I perform the calculation|I add the numbers) (\\d+) (plus|and) (\\d+)$")
void performXPlusY(String ignored1, int x, String ignored2, int y) {
    // do the step
}

What does everyone think about this? Is it something that would be easy enough to add?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:12
  • Comments:25 (8 by maintainers)

github_iconTop GitHub Comments

5reactions
mikehollercommented, Feb 12, 2016

Cucumber relies on the natural language capabilities of Gherkin to allow tests to be written that are supposedly easy for both managers and programmers to understand. However, it’s not always one person that writes specs (Gherkin .feature files) and even if there is one person it’s very easy to write two steps that mean the same thing in syntactically different ways.

Currently, the only ways to handle syntactically different but semantically the same steps are:

  • Modify the step to match a previously written (and running) step
  • Create a step handler for the syntactically new step and delegate it to the older step

Since Cucumber and Gherkin pride themselves on natural language and quality of the human-computer interface, I believe there should be an easier way than delegation to allow both syntactically different steps to exist.

2reactions
mikehollercommented, May 31, 2016

@dkowis, thanks for responding. I respectfully disagree with your position on the basis of code (glue) quality and readability, and beg you to reconsider.

It’s hard to argue that delegating to an older step is more concise and readable than my proposed solution.

It seems to me the point of “glue” functions is to link a step definition with instructions for step execution. That is, to map syntax to semantic meaning. The glue should be considered semantically (this is how to do X) rather than syntactically (do X).

It’s true that most other languages utilize a DSL, but even with a DSL the ability to have multiple syntactically different regexes defined for a single step is possible by allowing the DSL to accept an array of regexes in addition to a single regex. Perhaps this is a change that needs to happen on the Cucumber platform level, rather than the Cucumber-JVM level.

Thank you for reconsidering.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Multiple steps for same cucumber step definition
My problem is that we use the Given at past tense and the When at present tense. So the question is: how can...
Read more >
Step Organization - Cucumber Documentation
As your project grows, you should split your step definitions into meaningful groups in different files.
Read more >
Java Cucumber Tutorial 13 (Multiple Step Definition Classes)
Java Cucumber Tutorial 13 ( Multiple Step Definition Classes) | QAShahinJava Cucumber video tutorial on managing multiple Step Definition ...
Read more >
How to run multiple step definition files in Cucumber, Allow multiple ...
Allow multiple step patterns on the same method. If that experience with Cucumber (-Ruby) and Ruby is valid also for Cucumber-JVM and the...
Read more >
A Practical Example of Cucumber's Step Definitions in Java
When writing Cucumber tests in Java, it's common to use the Step Definition class to keep the state within the multiple methods that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found