godog icon indicating copy to clipboard operation
godog copied to clipboard

Support nested steps

Open vec715 opened this issue 6 months ago • 0 comments

🤔 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

vec715 avatar Aug 09 '24 02:08 vec715