Skip to content

Support nested steps #639

Open
Open
@vec715

Description

@vec715

🤔 What's the problem you're trying to solve?

The current flat design of Godog tests makes the structure difficult to read and navigate. Unlike Goconvey, Godog doesn't natively support nested steps, which would improve readability and allow for easier reuse of variables from parent functions. This limitation makes writing and maintaining tests more challenging and time-consuming

✨ What's your proposed solution?

Implement a feature in Godog that allows for nested steps, similar to Goconvey's approach. This would involve:

  • Creating a hierarchical structure for test steps.
  • Allowing variables to be shared between parent and child steps.

As an initial attempt, I created a custom ScenarioBuilder struct to simulate nested steps:

package features

import (
	"github.com/cucumber/godog"
	"github.com/cucumber/godog/colors"
	"os"
)

var opts = godog.Options{
	Output: colors.Colored(os.Stdout),
	Format: "progress", // can define default values
}

func init() {
	godog.BindCommandLineFlags("godog.", &opts) // godog v0.11.0 and later
}

type ScenarioBuilder struct {
	name string
	ctx  *godog.ScenarioContext
}

func NewScenario(name string, ctx *godog.ScenarioContext) *ScenarioBuilder {
	return &ScenarioBuilder{name: name, ctx: ctx}
}

func (s *ScenarioBuilder) Given(expr string, f interface{}) *ScenarioBuilder {
	s.ctx.Given(`^`+expr+`$`, f)
	return s
}

func (s *ScenarioBuilder) When(expr string, f interface{}) *ScenarioBuilder {
	s.ctx.When(`^`+expr+`$`, f)
	return s
}

func (s *ScenarioBuilder) Then(expr string, f interface{}) *ScenarioBuilder {
	s.ctx.Then(`^`+expr+`$`, f)
	return s
}

func (s *ScenarioBuilder) And(expr string, f interface{}) *ScenarioBuilder {
	s.ctx.Step(`^`+expr+`$`, f)
	return s
}

This approach initially worked
image

But (!) I encountered issues when more test cases were added.

⛏ Have you considered any alternatives or workarounds?

No response

📚 Any additional context?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions