Go testing customization with testing.TB

huangapple go评论150阅读模式
英文:

Go testing customization with testing.TB

问题

我正在尝试使用自己的assert方法来自定义testing.T,以减少我编写的代码行数。我尝试了以下方法,但出现了错误:"wrong signature for TestCustom, must be: func TestCustom(t *testing.T)"

我该如何使TestCustom使用CustomTester接口,并添加一个新的方法assert

我不想使用第三方框架。

custom_testing.go

type CustomTester struct {
    testing.TB
}

func (t *CustomTester) assert(exp interface{}, act interface{}) {
    if exp != act {
        t.Errorf("expected: %v. got: %v\n", exp, act)
    }
}

// 我想要在这里使用testing包注入testing.T
// 但是,通过使用我的自定义包装器:CustomTester结构体,
// 使用我的自定义assert方法来摆脱在每个assert中使用t作为参数,
// 例如:assert(t, exp, act)
func TestCustom(t *testing.TB) {
    t.assert(3, len(foo))
}

注意: 我也尝试过这个方法,它可以工作,但是我不想在每次测试时都传递t参数:

working_not_wanted.go

func assert(t *testing.TB, exp interface{}, act interface{}) {
    if exp != act {
        t.Errorf("expected: %v. got: %v\n", exp, act)
    }
}

func TestCustom(t *testing.T) {
    assert(t, 3, len(foo))
}
英文:

I'm trying to customize the testing.T with my own assert method to lower the number of lines I'm writing. I tried the following, ended with an error: "wrong signature for TestCustom, must be: func TestCustom(t *testing.T)".

How can I make TestCustom use CustomTester interface with a new method, assert?

I don't want to use a 3rd-party framework.

custom_testing.go

type CustomTester struct {
        testing.TB
}

func (t *CustomTester) assert(exp interface{}, act interface{}) {
        if exp != act {
                t.Errorf("expected: %v. got: %v\n", exp, act)
        }
}

// I want testing package inject testing.T here
// But, by using my own wrapper: CustomTester struct with,
// my own assert method to get rid of using t as an argument,
// in each assert like: assert(t, exp, act)
func TestCustom(t *testing.TB) {
        t.assert(3, len(foo))
}

NOTE: I also tried this, it works but, I don't want to pass t each time when I'm testing:

working_not_wanted.go

func assert(t *testing.TB, exp interface{}, act interface{}) {
        if exp != act {
                t.Errorf("expected: %v. got: %v\n", exp, act)
        }
}

func TestCustom(t *testing.T) {
        assert(t, 3, len(foo))
}

答案1

得分: 3

Go测试框架执行具有特定签名的测试函数,该签名接受一个*testing.T。如果您想使用stdlib测试系统,您的测试函数必须具有所需的签名。

您可以在每个测试函数中用一行代码包装它:

func MyTest(stdt *testing.T) {
    // 这一行:
    t := &CustomTester{stdt}
    t.assert(true)
    t.Error("An error done happened")
}

还有其他方法可以实现,但是无法通过go test运行使用stdlib testing包的测试函数,除了将*testing.T作为其唯一参数之外。

英文:

The Go testing framework executes test functions of a specific signature, and that signature takes a *testing.T. If you want to use the stdlib testing system, your test functions have to have the required signature.

You could wrap it with one line in every test function:

func MyTest(stdt *testing.T) {
    // This line:
    t := &CustomTester{stdt}
    t.assert(true)
    t.Error("An error done happened")
}

There are other ways to do it, but there is no way to have a testing function, run by go test, using the stdlib testing package, that takes anything other than *testing.T as its sole parameter.

huangapple
  • 本文由 发表于 2017年5月22日 22:12:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/44115117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定