aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/moricho/tparallel/README.md
blob: c4f1efd01a493e14b9c2e853ebb50e96d1e32e75 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# tparallel

[![tparallel](https://github.com/moricho/tparallel/workflows/tparallel/badge.svg?branch=master)](https://github.com/moricho/tparallel/actions)
[![Go Report Card](https://goreportcard.com/badge/github.com/moricho/tparallel)](https://goreportcard.com/report/github.com/moricho/tparallel)
[![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](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](https://engineering.mercari.com/en/blog/entry/20220408-how_to_use_t_parallel/)

## Installation

### From GitHub Releases

Please see [GitHub Releases](https://github.com/moricho/tparallel/releases).  
Available binaries are:

- macOS
- Linux
- Windows

### macOS

```sh
$ brew tap moricho/tparallel
$ brew install tparallel
```

### go get

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

## Usage

### golangci-lint

[golangci-lint](https://github.com/golangci/golangci-lint) now supports `tparallel`, so you can enable this linter and use in it.

### shell

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

## Example

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

```console
# 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
```