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

docs: add duplicate steps example and "Motivation" README #642

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ Gopkg.toml
_artifacts

vendor
godog.iml
34 changes: 34 additions & 0 deletions _examples/dupsteps/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Duplicate Steps

This example reproduces the problem wherein duplicate steps are silently overridden.

## Motivation

As a relatively new user of Cucumber & its [Gherkin syntax](https://cucumber.io/docs/gherkin/), and
as an implementer of steps for a scenario using `godog`, I'd like to have the ability to encapsulate
implementation of steps for a scenario without concern that a step function from a different scenario
will be called instead of mine. At least, I'd like to have more control over automatic "re-use" of
step functions; either: (1) choose to limit the assignment of a step function to step text (or regex)
to be matched only within the scope of its enclosing scenario (preferred), (2) have `godog` throw an
error when an ambiguous match to a step function is defined or detected for a step's implementation.

Though I've begun to understand that re-use of step implementations is somewhat fundamental to the Gherkin design
(e.g., I've read about _[feature coupled step definitions](https://cucumber.io/docs/guides/anti-patterns/?lang=java#feature-coupled-step-definitions)_
and how Cucumber _"effectively flattens the features/ directory tree"_), it's still annoying that
the `godog` scaffold seems to _require us to conform_ to the Gherkin recommendation to _"organise
your steps by domain concept"_, and not by Feature or even Scenario, as would better suit our project.

What's ended up happening to several of our developers in our distributed BDD testing initiative is
they end up force-tweaking the text of their steps in order to avoid duplicates, as they don't have agency
over other scenario implementations which they need to run with in our Jenkins pipeline. Later, they're
annoyed when their scenarios suddenly start failing after new scenarios are added having step
implementations with regex which _happens_ to match (thereby overriding) theirs.

_NOTE:_ due to a limitation in our Jenkins pipeline, all of our features & scenarios _must_ be executed
within the same `godog.Suite`, else (I realize) we could just "solve" this problem by running each scenario
in its own invocation of `godog`.

## Summary

In light of the specifics of the "Motivation" above, the stated "problem" here might then be more
effectively re-characterized as a "request" to give more control to the end-user, as suggested above.
113 changes: 113 additions & 0 deletions _examples/dupsteps/dupsteps_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package dupsteps

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"

"github.com/cucumber/godog"
)

func TestDuplicateSteps(t *testing.T) {

featureContents := `
Feature: check this out

Scenario: Flat Tire
Given I ran over a nail and got a flat tire
Then I fixed it
Then I can continue on my way

Scenario: Clogged Drain
Given I accidentally poured concrete down my drain and clogged the sewer line
Then I fixed it
Then I can once again use my sink
`

suite := godog.TestSuite{
Name: t.Name(),
ScenarioInitializer: func(context *godog.ScenarioContext) {
// NOTE: loading implementations of steps for different scenarios here
(&cloggedDrain{}).addCloggedDrainSteps(context)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "clogged drain" scenario (above) works fine until the "flat tire" scenario (below) is added (e.g., in a subsequent commit).

The "I fixed it" step from the newly added flatTire scenario silently overrides the matching step in the previously defined step from the cloggedDrain scenario. The writer of the clogged drain scenario will have to investigate why his/her scenario suddenly stops working.

We'd like the tool to support a distributed development workflow where developers can work on scenarios independently, without having to take into account (and reuse) all pre-existing, matching steps.

(&flatTire{}).addFlatTireSteps(context)
},
Options: &godog.Options{
Format: "pretty",
Strict: true,
FeatureContents: []godog.Feature{
{
Name: fmt.Sprintf("%s contents", t.Name()),
Contents: []byte(featureContents),
},
},
},
}

rc := suite.Run()
assert.Zero(t, rc)
}

// Implementation of the steps for the "Clogged Drain" scenario

type cloggedDrain struct {
drainIsClogged bool
}

func (cd *cloggedDrain) addCloggedDrainSteps(ctx *godog.ScenarioContext) {
ctx.Step(`^I accidentally poured concrete down my drain and clogged the sewer line$`, cd.clogSewerLine)
ctx.Step(`^I fixed it$`, cd.iFixedIt)
ctx.Step(`^I can once again use my sink$`, cd.useTheSink)
}

func (cd *cloggedDrain) clogSewerLine() error {
cd.drainIsClogged = true

return nil
}

func (cd *cloggedDrain) iFixedIt() error {
cd.drainIsClogged = false

return nil
}

func (cd *cloggedDrain) useTheSink() error {
if cd.drainIsClogged {
return fmt.Errorf("drain is clogged")
}

return nil
}

// Implementation of the steps for the "Flat Tire" scenario

type flatTire struct {
tireIsFlat bool
}

func (ft *flatTire) addFlatTireSteps(ctx *godog.ScenarioContext) {
ctx.Step(`^I ran over a nail and got a flat tire$`, ft.gotFlatTire)
ctx.Step(`^I fixed it$`, ft.iFixedIt)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NOTE: the text of this new (or the old, pre-existing) step could be something less obviously matching - e.g., ^(.+)\s+fixed\s+(.+)$, making it harder for the second developer to spot and re-use the step instead of writing their own.

ctx.Step(`^I can continue on my way$`, ft.continueOnMyWay)
}

func (ft *flatTire) gotFlatTire() error {
ft.tireIsFlat = true

return nil
}

func (ft *flatTire) iFixedIt() error {
ft.tireIsFlat = false

return nil
}

func (ft *flatTire) continueOnMyWay() error {
if ft.tireIsFlat {
return fmt.Errorf("tire was not fixed")
}

return nil
}
21 changes: 21 additions & 0 deletions _examples/dupsteps/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module github.com/cucumber/godog/_examples/dupsteps

go 1.23.1

require (
github.com/cucumber/godog v0.14.1
github.com/stretchr/testify v1.8.2
)

require (
github.com/cucumber/gherkin/go/v26 v26.2.0 // indirect
github.com/cucumber/messages/go/v21 v21.0.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gofrs/uuid v4.3.1+incompatible // indirect
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
github.com/hashicorp/go-memdb v1.3.4 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
51 changes: 51 additions & 0 deletions _examples/dupsteps/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cucumber/gherkin/go/v26 v26.2.0 h1:EgIjePLWiPeslwIWmNQ3XHcypPsWAHoMCz/YEBKP4GI=
github.com/cucumber/gherkin/go/v26 v26.2.0/go.mod h1:t2GAPnB8maCT4lkHL99BDCVNzCh1d7dBhCLt150Nr/0=
github.com/cucumber/godog v0.14.1 h1:HGZhcOyyfaKclHjJ+r/q93iaTJZLKYW6Tv3HkmUE6+M=
github.com/cucumber/godog v0.14.1/go.mod h1:FX3rzIDybWABU4kuIXLZ/qtqEe1Ac5RdXmqvACJOces=
github.com/cucumber/messages/go/v21 v21.0.1 h1:wzA0LxwjlWQYZd32VTlAVDTkW6inOFmSM+RuOwHZiMI=
github.com/cucumber/messages/go/v21 v21.0.1/go.mod h1:zheH/2HS9JLVFukdrsPWoPdmUtmYQAQPLk7w5vWsk5s=
github.com/cucumber/messages/go/v22 v22.0.0/go.mod h1:aZipXTKc0JnjCsXrJnuZpWhtay93k7Rn3Dee7iyPJjs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gofrs/uuid v4.2.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/gofrs/uuid v4.3.1+incompatible h1:0/KbAdpx3UXAx1kEOWHJeOkpbgRFGHVgv+CFIY7dBJI=
github.com/gofrs/uuid v4.3.1+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
github.com/hashicorp/go-immutable-radix v1.3.0/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
github.com/hashicorp/go-immutable-radix v1.3.1 h1:DKHmCUm2hRBK510BaiZlwvpD40f8bJFeZnpfm2KLowc=
github.com/hashicorp/go-immutable-radix v1.3.1/go.mod h1:0y9vanUI8NX6FsYoO3zeMjhV/C5i9g4Q3DwcSNZ4P60=
github.com/hashicorp/go-memdb v1.3.4 h1:XSL3NR682X/cVk2IeV0d70N4DZ9ljI885xAEU8IoK3c=
github.com/hashicorp/go-memdb v1.3.4/go.mod h1:uBTr1oQbtuMgd1SSGoR8YV27eT3sBHbYiNm53bMpgSg=
github.com/hashicorp/go-uuid v1.0.0/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/go-uuid v1.0.2 h1:cfejS+Tpcp13yd5nYHWDI6qVCny6wyX2Mt5SGur2IGE=
github.com/hashicorp/go-uuid v1.0.2/go.mod h1:6SBZvOh/SIDV7/2o3Jml5SYk/TvGqwFJ/bN7x4byOro=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=