在使用testify的suite包时,go中可能存在未使用的参数。

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

Potentially unused parameter in go when using testify's suite package

问题

我想使用testify/suite包来执行子测试。

我将我的单元测试套件声明如下:

type UnitSuite struct {
	suite.Suite
}

func TestUnitSuite(t *testing.T) {
	suite.Run(t, &UnitSuite{})
}

这是我的子测试:

func (us *UnitSuite) ΤestSomething() {
	for i := range testVars {
		i := i
		us.T().Run(testVars[i].name, func(t *testing.T) {
        ...

对于func(t *testing.T),我得到了以下linting警告:

potentially unused parameter: 't' unusedparams

当我尝试用测试套件的函数T()替换它,以获取testing上下文时:

us.T().Run(testVars[i].name, func(us.T()) {

我在func(us.T())中遇到了这个错误<-- 错误退出

missing ',' in parameter list syntax

有什么方法可以避免产生错误和linting警告吗?

英文:

I want to execute subtests using the testify/suite package.

I am declaring my Unit suite as follows


type UnitSuite struct {
	suite.Suite
}

func TestUnitSuite(t *testing.T) {
	suite.Run(t, &amp;UnitSuite{})
}

and here is my subtest

func (us *UnitSuite) ΤestSomething() {
	for i := range testVars {
		i := i
		us.T().Run(testVars[i].name, func(t *testing.T) {
        ...

Ι am getting the following linting warning for func(t *testing.T)

potentially unused parameter: &#39;t&#39;unusedparams

When trying to substitute with the testing suite's function T() that is supposes to retrieve the testing context

us.T().Run(testVars[i].name, func(us.T()) {

I get this error in func(us.T()) <-- errors out

missing &#39;,&#39; in parameter listsyntax

What is the way to go about this that does not produce neither errors nor linting warnings?

答案1

得分: 2

如果你想声明一个未使用的参数(通常是为了满足接口或其他要求),你可以将其命名为一个下划线。例如:

Run(name, func(_ *testing.T) {})

请注意,在函数声明中不能放置一个,所以func(us.T()) {总是错误的。

英文:

If you want to declare an unused parameter (usually to satisfy an interface or other requirements), you can name it a single underscore. For example:

Run(name, func(_ *testing.T) {})

Note that you cannot put a value in function declaration, so func(us.T()) { is always wrong.

huangapple
  • 本文由 发表于 2022年7月30日 17:30:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/73174427.html
匿名

发表评论

匿名网友

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

确定