猿问

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

为了符合并发要求,我想知道如何在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可以传递这些信息?


慕哥6287543
浏览 139回答 3
3回答

繁星点点滴滴

我发现在步骤中使用方法而不是函数来获得好运。然后,将状态放入结构中。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    ...}

Smart猫小萌

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

守着星空守着你

最新版本 (v0.12.0+)godog允许context.Context挂钩和步骤之间的链接。您可以将其context.Context作为步骤定义参数并返回,测试运行器将提供上一步的上下文作为输入,并使用返回的上下文传递给下一个钩子和步骤。func iEat(ctx context.Context, arg1 int) context.Context {    if v, ok := ctx.Value(eatKey{}).int; ok {        // Eat v from context.    }    // Eat arg1.        return context.WithValue(ctx, eatKey{}, 0)}其他信息和示例:https ://github.com/cucumber/godog/blob/main/release-notes/v0.12.0.md#contextualized-hooks 。
随时随地看视频慕课网APP

相关分类

Go
我要回答