为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

Go
慕哥6287543 2022-05-23 17:07:11
为了符合并发要求,我想知道如何在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可以传递这些信息?
查看完整描述

3 回答

?
繁星点点滴滴

TA贡献1803条经验 获得超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

    ...

}


查看完整回答
反对 回复 2022-05-23
?
Smart猫小萌

TA贡献1911条经验 获得超7个赞

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


查看完整回答
反对 回复 2022-05-23
?
守着星空守着你

TA贡献1799条经验 获得超8个赞

最新版本 (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 。


查看完整回答
反对 回复 2022-05-23
  • 3 回答
  • 0 关注
  • 138 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号