Skip to content

Adding context to steps #360

@jlorgal

Description

@jlorgal

Is your feature request related to a problem? Please describe.
We want to generate a library with some common steps. For example, a step that sends a REST request, a step that checks the status code, a step that validates the response, and a step that checks a HTTP header from the response. In this way, we could reuse some common steps.
However, godog only passes the regex parameters as arguments. We could use a struct and create methods for that type. But we wouldn't get reusable steps in any scenario.

Describe the solution you'd like
I'd like that each scenario creates a context.Context instance (or custom context) and the context is passed as first argument in each step function. Instead of:

func thereAreGodogs(available int) error {

we would have:

func thereAreGodogs(context.Context ctx, available int) error {

Note that context.Context is immutable. So we would need to create a map to store all context changes related to godog, to return the context to chain the context for the next step, or to use a custom context type.

Describe alternatives you've considered
We could use InitializeScenario for this purpose but it would require a lot of repetitive code for each step:

func InitializeScenario(ctx *godog.ScenarioContext) {
	var stepCtx godog.Context
	ctx.Step(`^there are (\d+) godogs$`, func(available int) {
		thereAreGodogs(stepCtx, available)
	})
	...
}

Additional context
See GoBDD that follows this approach. See also the motivations to write an alternative to Godog which I consider to be very good feedback for possible improvements in Godog.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions