go - 使用带有 GoDog 测试框架的断言库
问题描述
我使用Cucumber GoDog作为 gRPC 微服务测试的 BDD 测试框架。GoDog 不附带任何断言助手或实用程序。
这里有没有人有采用任何现有断言库(如Testify / GoMega with GoDog)的经验?
据我所知,GoDog 不能在此基础上工作,go test
这就是为什么我猜想采用go test
我提到的任何基于断言库都具有挑战性。但我仍然想在这里检查是否有人有这样做的经验。
解决方案
这是使用 Testify 的基本概念验证:
package bdd
import (
"fmt"
"github.com/cucumber/godog"
"github.com/stretchr/testify/assert"
)
type scenario struct{}
func (_ *scenario) assert(a assertion, expected, actual interface{}, msgAndArgs ...interface{}) error {
var t asserter
a(&t, expected, actual, msgAndArgs...)
return t.err
}
func (sc *scenario) forcedFailure() error {
return sc.assert(assert.Equal, 1, 2)
}
type assertion func(t assert.TestingT, expected, actual interface{}, msgAndArgs ...interface{}) bool
type asserter struct {
err error
}
func (a *asserter) Errorf(format string, args ...interface{}) {
a.err = fmt.Errorf(format, args...)
}
func FeatureContext(s *godog.Suite) {
var sc scenario
s.Step("^forced failure$", sc.forcedFailure)
}
Feature: forced failure
Scenario: fail
Then forced failure
这里的关键是实现 Testify 的assert.TestingT
接口。
推荐阅读
- javascript - 数组中的展开操作出错。TS1005:“,”预期。打字稿
- api - 唤醒时 Azure 应用服务的瓶颈
- testing - Is there a way to fake DateTime.now() in a Flutter test?
- reactjs - redux 状态改变但连接的组件没有更新,无法理解突变
- git - Git Remote Repo 显示冲突,但本地没有显示冲突
- python - Nested ndarray reshape
- ruby - 无法让 ruby 使用 Nokogiri 解析我的 XML
- python - 从连接对象的条目属性中提取的数据类型单个元素在python中代表什么?
- datagridview - 拖放和光标图像更改代码中的错误需要 C# 帮助
- python - TypeError:“numpy.ndarray”对象不可调用我该怎么办?