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

问题描述 投票:1回答:2

为了符合并发性要求,我想知道如何在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库本身是否具有传递这些信息的功能?

go concurrency cucumber gherkin
2个回答
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


0
投票

我发现使用方法而不是步骤来实现运气很好。然后,将状态放入结构中。

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
    ...
}
© www.soinside.com 2019 - 2024. All rights reserved.