moricho / tparallel

tparallel finds inappropriate usage of `t.Parallel()` method in your Go test codes
MIT License
24 stars 4 forks source link

Does not seem to work #10

Open kunwardeep opened 4 years ago

kunwardeep commented 4 years ago

How to reproduce

1> go get github.com/golangci/golangci-lint/cmd/golangci-lint@master (as the latest change is not in the release) 2> Set the linter to use tParallel 3> Run golangci-lint run ./...

Code to run the tests on

package main

func Add(i, j int) int {
    return i + j
}
package main

import (
    "fmt"
    "testing"

    "github.com/stretchr/testify/assert"
)

func Test_Add(t *testing.T) {

    testCases := []struct {
        name           string
        val1           int
        val2           int
        expectedResult int
    }{
        {
            name:           "add correct",
            val1:           2,
            val2:           4,
            expectedResult: 6,
        },
        {
            name:           "add negative",
            val1:           -2,
            val2:           -4,
            expectedResult: -6,
        },
    }

    for _, tc := range testCases {
        fmt.Println(tc.name)
    }

    for _, tc := range testCases {
        t.Run(tc.name, func(t *testing.T) {

            assert.Equal(t, tc.expectedResult, Add(tc.val1, tc.val2))
        })
    }
}
Screen Shot 2020-10-16 at 5 04 03 pm

I would've expected the linter to tell me that there is no t.Parallel being called.

moricho commented 4 years ago

Thank you for reporting and using tparallel in golangci-lint! However, that's the intended behavior. It's not compulsory to use t.Parallel(), so this linter doesn't warn when t.Parallel() is not called in both a top-level test and sub-tests.

tparallel alerts only when you intend to parallelize tests, but you are not using t.Parallel() efficiently.

kunwardeep commented 4 years ago

So how do I make the linter complain about the test above ?