cweill / gotests

Automatically generate Go test boilerplate from your source code.
Apache License 2.0
4.92k stars 346 forks source link

Tests generated with -parallel should use t.Parallel() at the top level of a Test function as well as in the subtest functions #188

Open edgio-mwodrich opened 6 months ago

edgio-mwodrich commented 6 months ago

Huge thanks for the addition of -parallel in the first place, it's really handy; just wanted to point out a potential improvement for it here.

Tests generated with -parallel currently only use t.Parallel() in subtest functions, so they run afoul of the golangci-lint linter tparallel: https://github.com/moricho/tparallel which looks for certain inappropriate uses of t.Parallel().

Tests should use t.Parallel() both at the beginning of each Test_ function and at the beginning of each subtest function for optimal parallelism.

e.g.:

func Test_Table2(t *testing.T) {
    t.Parallel()

    tests := []struct {
        name string
    }{
        {
            name: "Table2_Sub1",
        },
        {
            name: "Table2_Sub2",
        },
    }

    for _, tt := range tests {
        tt := tt
        t.Run(tt.name, func(t *testing.T) {
            t.Parallel()
            call(tt.name)
        })
    }
}