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

Time:07-31

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?

CodePudding user response:

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.

  • Related