gotests icon indicating copy to clipboard operation
gotests copied to clipboard

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

Open edgio-mwodrich opened this issue 1 year ago • 1 comments

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)
		})
	}
}

edgio-mwodrich avatar Mar 06 '24 18:03 edgio-mwodrich

To build on this - the tt := tt line is no longer needed in go >= 1.22.

bpm-alaska avatar Mar 31 '25 22:03 bpm-alaska

Fixed in v1.7.0! 🎉

Tests generated with the -parallel flag now properly include t.Parallel() at the top-level test function, not just in the subtest functions. This satisfies the tparallel linter requirements and ensures proper parallel test execution.

Example generated code:

func TestFoo(t *testing.T) {
	t.Parallel()  // ← Now added at top level!
	tests := []struct {
		name string
		want bool
	}{
		// TODO: Add test cases.
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			t.Parallel()  // ← And in subtests
			// test code...
		})
	}
}

Thanks for reporting this issue! Check out the release: https://github.com/cweill/gotests/releases/tag/v1.7.0

cweill avatar Oct 21 '25 01:10 cweill