Godog在步骤之间传递参数/状态

时间:2020-03-31 10:51:46

标签: go concurrency cucumber gherkin

为了符合并发性要求,我想知道如何在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)

我想到的唯一想法是内联被调用函数:

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)
})

但是,这有点像一种解决方法。 Godog库本身是否具有传递这些信息的功能?

2 个答案:

答案 0 :(得分:2)

Godog当前没有这样的功能,但是我过去通常所做的(需要测试并发性)是创建一个TestContext结构来存储数据并创建一个新的结构。在每种情况之前。

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)
    })
}

我在这里也有一个指向旧示例的链接:https://github.com/jaysonesmith/godog-baseline-example

答案 1 :(得分:2)

我发现使用方法而不是步骤的功能会带来好运。然后,将状态放入结构。

func FeatureContext(s *godog.Suite) {
    t := NewTestRunner()

    s.Step(`^I work with "([^"]*)" entities`, t.iWorkWithEntities)
}

type TestRunner struct {
    State map[string]interface{}
}

func (t *TestRunner) iWorkWithEntities(s string) error {
    t.State["entities"] = s
    ...
}