英文:
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, &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: 't'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 ',' 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论