go test报错 wrong signature for TestAdd, must be: func TestAdd(t *testing.T)

参考: https://www.cnblogs.com/mingbai/p/gotesterr.html

背景

学习 Go 过程中,在终端输入 go test 显示报错:

1
$ wrong signature for TestAdd, must be: func TestAdd(t *testing.T)

后经搜索该报错查明示由于 func TestAdd(t testing.T) 参数未带 *,需要改为 func TestAdd(t *testing.T)

问题复现

报错代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// calculation_util_test.go
package calculation_util

import "testing"

func TestAdd(t testing.T) {
tests := []struct{ a, b, c int }{
{1, 2, 3},
{1, 3, 4},
{1, 1, 2},
}
for _, test := range tests {
actualVal := Add(test.a, test.b)
expectVal := test.c
if actualVal != expectVal {
t.Errorf("Add(%d, %d) actualVal: %d, but expectVal: %d", test.a, test.b, actualVal, expectVal)
}
}
}

如何解决

修改后的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// calculation_util_test.go
package calculation_util

import "testing"

func TestAdd(t *testing.T) {
tests := []struct{ a, b, c int }{
{1, 2, 3},
{1, 3, 4},
{1, 1, 2},
}
for _, test := range tests {
actualVal := Add(test.a, test.b)
expectVal := test.c
if actualVal != expectVal {
t.Errorf("Add(%d, %d) actualVal: %d, but expectVal: %d", test.a, test.b, actualVal, expectVal)
}
}
}

测试通过!

go test报错 wrong signature for TestAdd, must be: func TestAdd(t *testing.T)

https://spencer17x.github.io/2021/09/21/go-test报错-wrong-signature-for-TestAdd-must-be-func-TestAdd-t-testing-T/

作者

spencer17x

发布于

2021-09-21

更新于

2026-02-28

许可协议

评论