moricho/tparallel

tparallel finds inappropriate usage of `t.Parallel()` method in your Go test codes

Go

28

69 commits

updated Jun 26, 2024

See the code

README

tparallel

tparallel Go Report Card MIT License

tparallel finds inappropriate usage of t.Parallel() method in your Go test codes.
It detects the following:

  • t.Parallel() is called in either a top-level test function or a sub-test function only
  • Although t.Parallel() is called in the sub-test function, it is post-processed by defer instead of t.Cleanup()

This tool was inspired by this blog: Test parallelization in Go: Understanding the t.Parallel() method

Installation

From GitHub Releases

Please see GitHub Releases.
Available binaries are:

  • macOS
  • Linux
  • Windows

macOS

$ brew tap moricho/tparallel
$ brew install tparallel

go get

$ go get -u github.com/moricho/tparallel/cmd/tparallel

Usage

golangci-lint

golangci-lint now supports tparallel, so you can enable this linter and use in it.

shell

$ go vet -vettool=`which tparallel` <pkgname>

Example

package sample

import (
	"testing"
)

func Test_Table1(t *testing.T) {
	teardown := setup("Test_Table1")
	defer teardown()

	tests := []struct {
		name string
	}{
		{
			name: "Table1_Sub1",
		},
		{
			name: "Table1_Sub2",
		},
	}

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

func Test_Table2(t *testing.T) {
	teardown := setup("Test_Table2")
	t.Cleanup(teardown)
	t.Parallel()

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

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			call(tt.name)
		})
	}
}
# github.com/moricho/tparallel/testdata/src/sample
testdata/src/sample/table_test.go:7:6: Test_Table1 should use t.Cleanup
testdata/src/sample/table_test.go:7:6: Test_Table1 should call t.Parallel on the top level as well as its subtests
testdata/src/sample/table_test.go:30:6: Test_Table2's subtests should call t.Parallel

Significant stargazers

Marat Reimers

38 followers · starred Jun 2022

Karim ElDeeb

20 followers · starred Apr 2021

moricho/tparallel

tparallel finds inappropriate usage of `t.Parallel()` method in your Go test codes

Go

28

69 commits

updated Jun 26, 2024

See the code

README

tparallel

tparallel Go Report Card MIT License

tparallel finds inappropriate usage of t.Parallel() method in your Go test codes.
It detects the following:

  • t.Parallel() is called in either a top-level test function or a sub-test function only
  • Although t.Parallel() is called in the sub-test function, it is post-processed by defer instead of t.Cleanup()

This tool was inspired by this blog: Test parallelization in Go: Understanding the t.Parallel() method

Installation

From GitHub Releases

Please see GitHub Releases.
Available binaries are:

  • macOS
  • Linux
  • Windows

macOS

$ brew tap moricho/tparallel
$ brew install tparallel

go get

$ go get -u github.com/moricho/tparallel/cmd/tparallel

Usage

golangci-lint

golangci-lint now supports tparallel, so you can enable this linter and use in it.

shell

$ go vet -vettool=`which tparallel` <pkgname>

Example

package sample

import (
	"testing"
)

func Test_Table1(t *testing.T) {
	teardown := setup("Test_Table1")
	defer teardown()

	tests := []struct {
		name string
	}{
		{
			name: "Table1_Sub1",
		},
		{
			name: "Table1_Sub2",
		},
	}

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

func Test_Table2(t *testing.T) {
	teardown := setup("Test_Table2")
	t.Cleanup(teardown)
	t.Parallel()

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

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			call(tt.name)
		})
	}
}
# github.com/moricho/tparallel/testdata/src/sample
testdata/src/sample/table_test.go:7:6: Test_Table1 should use t.Cleanup
testdata/src/sample/table_test.go:7:6: Test_Table1 should call t.Parallel on the top level as well as its subtests
testdata/src/sample/table_test.go:30:6: Test_Table2's subtests should call t.Parallel

Significant stargazers

Marat Reimers

38 followers · starred Jun 2022

Karim ElDeeb

20 followers · starred Apr 2021

Languages

Go

97.3%

Makefile

2.7%