cucumber / godog

Cucumber for golang
MIT License
2.32k stars 254 forks source link

Support nested steps #639

Open vec715 opened 3 months ago

vec715 commented 3 months ago

🤔 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:

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

nhv96 commented 1 month ago

Hi, your approach is possible, but I think it will come with many limitations:

Even thought it is possible and also already be implemented in Goconvey, I think Godog should not follow the same, as we're using BDD, it leverages the collaboration of the whole team, the step definitions of .feature file plays a crucial role as well as it help us have a clear view on what the behavior should look like, we can break it down to smaller parts with ease and we don't need to worry about how underlying code is implemented.

vearutop commented 1 month ago

I agree, godog is all about running gherkin files given step definitions. Replacing gherkin with go code moves it into a different problem space.