Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Have step expressions and implementations closer to each other #398

Open
noel-yap opened this issue Jun 17, 2021 · 8 comments
Open

Have step expressions and implementations closer to each other #398

noel-yap opened this issue Jun 17, 2021 · 8 comments

Comments

@noel-yap
Copy link

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

When step expressions and implementations are separated I feel annoyed because I would like efficiency.

Describe the solution you'd like
A clear and concise description of what you want to happen.

Java has annotations (ie @Given, @When, @Then) which allows the expression to be right next to the implementation.

In, go, the following can be done:

var steps := map[string]interface{}{
    `^I eat (\d+)$`: func(arg1 int) error {
      return godog.ErrPending
    },
    `^there are (\d+) godogs$`: func(arg1 int) error {
      return godog.ErrPending
    },
    `^there should be (\d+) remaining$`: func(arg1 int) error {
      return godog.ErrPending
    },
}

func InitializeScenario(ctx *godog.ScenarioContext) {
	for expr, fn := range steps {
		ctx.Step(expr, fn)
	}
}

Additional context
Add any other context or screenshots about the feature request here.

If the above were done, the suggested implementation upon missing steps would need to be changed as well.

@vearutop
Copy link
Member

You can also init steps as

func InitializeScenario(ctx *godog.ScenarioContext) {
	ctx.Step(`^I eat (\d+)$`, func(arg1 int) error {
		return godog.ErrPending
	})
	ctx.Step(`^there are (\d+) godogs$`, func(arg1 int) error {
		return godog.ErrPending
	})
	ctx.Step(`^there should be (\d+) remaining$`, func(arg1 int) error {
		return godog.ErrPending
	})
}

having expressions and implementations next to each other.

But I'm not sure I follow the suggestion or the idea of the issue.
@noel-yap what exactly is the problem and how would you like to have it fixed?

@noel-yap
Copy link
Author

@vearutop , I had initially started doing just that and learned that if the implementations are long, that becomes unwieldy (not to mention it not lending itself well to sharing scenario context).

I was hoping there'd be a way to do it similarly to using annotations in Java:

@Given("^I eat (\d+)$")
public void iEat(n int) {
}

@vearutop
Copy link
Member

vearutop commented Jul 21, 2021

Maybe it would help to arrange lengthy step definitions as functions that operate on *godog.ScenarioContext, please check an example:

func InitializeScenario(ctx *godog.ScenarioContext) {
	longStep1(ctx)
	longStep2(ctx)
	ctx.Step(`^there should be (\d+) remaining$`, func(arg1 int) error {
		return godog.ErrPending
	})
}

func longStep1(ctx *godog.ScenarioContext) {
	ctx.Step(`^I eat (\d+)$`, func(arg1 int) error {
		// Long body follows...
		return godog.ErrPending
	})
}

func longStep2(ctx *godog.ScenarioContext) {
	ctx.Step(`^there are (\d+) godogs$`, func(arg1 int) error {
		// Long body follows...
		return godog.ErrPending
	})
}

They can also be grouped together to simplify InitializeScenario.

func longSteps(ctx *godog.ScenarioContext) {
	longStep1(ctx)
	longStep2(ctx)
}

@noel-yap
Copy link
Author

Sorry, I was unclear. By scenario context, I had meant context specific to the scenarios being tested, not godog.ScenarioContext. One way to resolve that would be to allow godog.ScenarioContext to hold custom context.

Regardless, what you have above looks fruitful. Thanks!

@noel-yap
Copy link
Author

As I was trying that out, I realized there's still a bunch of jumping around in the code since longStep1, etc would still need to be called explicitly.

@inluxc
Copy link

inluxc commented Aug 10, 2021

Sorry, I was unclear. By scenario context, I had meant context specific to the scenarios being tested, not godog.ScenarioContext. One way to resolve that would be to allow godog.ScenarioContext to hold custom context.

Regardless, what you have above looks fruitful. Thanks!

In the next version 0.12.0 we will have context support. 409

@waytocypress
Copy link

waytocypress commented Jul 20, 2022

you can do step binding (IDE level not saying test_main) in vscode. use cucumber full support plugin and in setting.json file add these

  1. location of step definition across whole project (_test.go file)
    "cucumberautocomplete.steps": [
    "**/*.go"
    ],
  2. location of feature files across project
    "cucumberautocomplete.syncfeatures": "**/*.features",

@tdeheurles
Copy link

Just an addition to @waytocypress comment, for the one who are not used to vscode:

  • install extensions: Cucumber (Gherkin) Full Support and Cucumber (Gherkin) Support enhanced for Behat
  • create a .vscode/settings.json file with this content:
{
    "cucumberautocomplete.steps": [
        "**/*.go"
    ],
    "cucumberautocomplete.syncfeatures": "**/*.features"
}

Note: the configuration is detailed in the extensions descriptions.

I confirm it works great. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants