Go testing customization with testing.TB

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

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

  1. type CustomTester struct {
  2. testing.TB
  3. }
  4. func (t *CustomTester) assert(exp interface{}, act interface{}) {
  5. if exp != act {
  6. t.Errorf("expected: %v. got: %v\n", exp, act)
  7. }
  8. }
  9. // 我想要在这里使用testing包注入testing.T
  10. // 但是,通过使用我的自定义包装器:CustomTester结构体,
  11. // 使用我的自定义assert方法来摆脱在每个assert中使用t作为参数,
  12. // 例如:assert(t, exp, act)
  13. func TestCustom(t *testing.TB) {
  14. t.assert(3, len(foo))
  15. }

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

working_not_wanted.go

  1. func assert(t *testing.TB, exp interface{}, act interface{}) {
  2. if exp != act {
  3. t.Errorf("expected: %v. got: %v\n", exp, act)
  4. }
  5. }
  6. func TestCustom(t *testing.T) {
  7. assert(t, 3, len(foo))
  8. }
英文:

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

  1. type CustomTester struct {
  2. testing.TB
  3. }
  4. func (t *CustomTester) assert(exp interface{}, act interface{}) {
  5. if exp != act {
  6. t.Errorf("expected: %v. got: %v\n", exp, act)
  7. }
  8. }
  9. // I want testing package inject testing.T here
  10. // But, by using my own wrapper: CustomTester struct with,
  11. // my own assert method to get rid of using t as an argument,
  12. // in each assert like: assert(t, exp, act)
  13. func TestCustom(t *testing.TB) {
  14. t.assert(3, len(foo))
  15. }

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

  1. func assert(t *testing.TB, exp interface{}, act interface{}) {
  2. if exp != act {
  3. t.Errorf("expected: %v. got: %v\n", exp, act)
  4. }
  5. }
  6. func TestCustom(t *testing.T) {
  7. assert(t, 3, len(foo))
  8. }

答案1

得分: 3

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

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

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

还有其他方法可以实现,但是无法通过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:

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

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:

确定