Search code examples
goconcurrencycucumbergherkin

Godog pass arguments/state between steps


To comply with the concurrency requirements, I'm wondering how to pass arguments or a state between multiple steps in Godog.

func FeatureContext(s *godog.Suite) {
    // This step is called in background
    s.Step(`^I work with "([^"]*)" entities`, iWorkWithEntities)
    // This step should know about the type of entity
    s.Step(`^I run the "([^"]*)" mutation with the arguments:$`, iRunTheMutationWithTheArguments)

The only idea which comes to my mind is to inline the called function:

state := make(map[string]string, 0)
s.Step(`^I work with "([^"]*)" entities`, func(entityName string) error {
    return iWorkWithEntities(entityName, state)
})
s.Step(`^I run the "([^"]*)" mutation with the arguments:$`, func(mutationName string, args *messages.PickleStepArgument_PickleTable) error {
    return iRunTheMutationWithTheArguments(mutationName, args, state)
})

But this feels a bit like a workaround. Is there any feature in the Godog library itself to pass those information?


Solution

  • Godog doesn't currently have a feature like this, but what I've done in the past in general (would need to be tested for concurrency) would be to create a TestContext struct to store data in and create a fresh one before each Scenario.

    func FeatureContext(s *godog.Suite) {
        config := config.NewConfig()
        context := NewTestContext(config)
    
        t := &tester{
            TestContext: context,
        }
    
        s.BeforeScenario(func(interface{}) {
            // reset context between scenarios to avoid
            // cross contamination of data
            context = NewTestContext(config)
        })
    }
    

    I have a link to an old example here as well: https://github.com/jaysonesmith/godog-baseline-example