首页 > 解决方案 > 存在步骤时的Godog未定义步骤

问题描述

我一直在使用 Godog 为 Golang 中的微服务实现功能文件测试。

我的功能文件中有 54 个步骤,我为所有这些步骤生成了步骤定义。

当我使用go test命令运行测试时,前 22 个场景通过并且 23 个被声明为Undefined即使它的定义存在。

任何测试后我的控制台输出:

......................U------------------------U-----U 54


1 scenarios (1 undefined)
54 steps (22 passed, 3 undefined, 29 skipped)
44.0003ms

Randomized with seed: 1621922954984294500

You can implement step definitions for undefined steps with these snippets:

func readingResourceOfId(arg1 string) error {
        return godog.ErrPending
}

func resourceWithIdIsFound(arg1 string) error {
        return godog.ErrPending
}

func resourceWithIdIsNotFound(arg1 string) error {
        return godog.ErrPending
}

func InitializeScenario(ctx *godog.ScenarioContext) {
        ctx.Step(`^reading resource of id "([^"]*)"$`, readingResourceOfId)
        ctx.Step(`^resource with id "([^"]*)" is found$`, resourceWithIdIsFound)
        ctx.Step(`^resource with id "([^"]*)" is not found$`, resourceWithIdIsNotFound)
}

testing: warning: no tests to run
PASS
ok      gitlab.com/xxxxxx/go-micro-service      0.595s

这是我main_test.go文件中的片段

func InitializeScenario(ctx *godog.ScenarioContext) {

api := &apiFeature{
    Set: make(map[string]interface{}),
}

ctx.Step(`^API response code is (\d+)$`, api.aPIResponseCodeIs)
ctx.Step(`^API response Location header is "([^"]*)" with "([^"]*)" autogenerated$`, api.aPIResponseLocationHeaderIsWithAutogenerated)
ctx.Step(`^creating a resource with name "([^"]*)" and type "([^"]*)"$`, api.creatingAResourceWithNameAndType)
ctx.Step(`^deleting the resource of id "([^"]*)"$`, api.deletingTheResourceOfId)
ctx.Step(`^external resource id (\d+) is "([^"]*)"$`, api.externalResourceIdIs)
ctx.Step(`^finally setting reference to external resource (\d+) on id "([^"]*)"$`, api.finallySettingReferenceToExternalResourceOnId)
ctx.Step(`^id is "([^"]*)"$`, api.idIs)
ctx.Step(`^modifying resource of id "([^"]*)" setting status to "([^"]*)"$`, api.modifyingResourceOfIdSettingStatusTo)
ctx.Step(`^name is "([^"]*)"$`, api.nameIs)
ctx.Step(`^searching for resource instances$`, api.searchingForResourceInstances)
ctx.Step(`^setting reference to external resource (\d+) on id "([^"]*)"$`, api.settingReferenceToExternalResourceOnId)
ctx.Step(`^status end date is null$`, api.statusEndDateIsNull)
ctx.Step(`^status is "([^"]*)"$`, api.statusIs)
ctx.Step(`^status "([^"]*)" is found in history$`, api.statusIsFoundInHistory)
ctx.Step(`^status start date is not null$`, api.statusStartDateIsNotNull)
ctx.Step(`^the micro-service is started$`, api.theMicroserviceIsStarted)
ctx.Step(`^type is "([^"]*)"$`, api.typeIs)


//Steps are defined
ctx.Step(`^reading resource of id "([^"]*)"$`, api.readingResourceOfId)
ctx.Step(`^resource with id "([^"]*)" is found$`, api.resourceWithIdIsFound)
ctx.Step(`^resource with id "([^"]*)" is not found$`, api.resourceWithIdIsNotFound)
}

谁能指出这背后的问题是什么,还是一个错误?

标签: gotestingvisual-studio-codecucumberbdd

解决方案


事实证明,Godog 没有从feature文件中删除 Steps。

因此,在我的功能文件中,上述行的步骤包括:

读取ID为“ID1”的资源时_____(空格)

因此,由于某种原因,正则表达式模式无法将步骤定义映射到这些行。

一旦我删除了空白空间,它就很好用。


推荐阅读