亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號