|
4 | 4 |
|
5 | 5 | The Go linter `paralleltest` checks that the t.Parallel gets called for the test method and for the range of test cases within the test.
|
6 | 6 |
|
7 |
| - |
8 | 7 | ## Usage
|
9 | 8 |
|
10 |
| -``` |
| 9 | +```sh |
11 | 10 | paralleltest ./...
|
12 | 11 | ```
|
13 | 12 |
|
14 | 13 | ## Examples
|
15 | 14 |
|
16 |
| -### Missing t.Parallel() in the test method |
| 15 | +### Missing `t.Parallel()` in the test method |
17 | 16 |
|
18 | 17 | ```go
|
19 | 18 | // bad
|
20 | 19 | func TestFunctionMissingCallToParallel(t *testing.T) {
|
21 |
| -} |
| 20 | +} |
22 | 21 |
|
23 | 22 | // good
|
24 | 23 | func TestFunctionMissingCallToParallel(t *testing.T) {
|
25 |
| - t.Parallel() |
26 |
| - // ^ call to t.Parallel() |
27 |
| -} |
| 24 | + t.Parallel() |
| 25 | + // ^ call to t.Parallel() |
| 26 | +} |
28 | 27 | // Error displayed
|
29 | 28 | // Function TestFunctionMissingCallToParallel missing the call to method parallel
|
30 | 29 | ```
|
31 | 30 |
|
32 |
| -### Missing t.Parallel() in the range method |
| 31 | +### Missing `t.Parallel()` in the range method |
33 | 32 |
|
34 | 33 | ```go
|
35 | 34 | // bad
|
36 | 35 | func TestFunctionRangeMissingCallToParallel(t *testing.T) {
|
37 |
| - t.Parallel() |
| 36 | + t.Parallel() |
38 | 37 |
|
39 |
| - testCases := []struct { |
40 |
| - name string |
41 |
| - }{{name: "foo"}} |
| 38 | + testCases := []struct { |
| 39 | + name string |
| 40 | + }{{name: "foo"}} |
42 | 41 |
|
43 |
| - for _, tc := range testCases { |
44 |
| - t.Run(tc.name, func(t *testing.T) { |
45 |
| - fmt.Println(tc.name) |
46 |
| - }) |
47 |
| - } |
| 42 | + for _, tc := range testCases { |
| 43 | + t.Run(tc.name, func(t *testing.T) { |
| 44 | + fmt.Println(tc.name) |
| 45 | + }) |
| 46 | + } |
48 | 47 | }
|
49 | 48 |
|
50 | 49 | // good
|
51 | 50 | func TestFunctionRangeMissingCallToParallel(t *testing.T) {
|
52 |
| - t.Parallel() |
53 |
| - |
54 |
| - testCases := []struct { |
55 |
| - name string |
56 |
| - }{{name: "foo"}} |
57 |
| - |
58 |
| - for _, tc := range testCases { |
59 |
| - t.Run(tc.name, func(t *testing.T) { |
60 |
| - t.Parallel() |
61 |
| - // ^ call to t.Parallel() |
62 |
| - fmt.Println(tc.name) |
63 |
| - }) |
64 |
| - } |
65 |
| -} |
| 51 | + t.Parallel() |
| 52 | + |
| 53 | + testCases := []struct { |
| 54 | + name string |
| 55 | + }{{name: "foo"}} |
| 56 | + |
| 57 | + for _, tc := range testCases { |
| 58 | + t.Run(tc.name, func(t *testing.T) { |
| 59 | + t.Parallel() |
| 60 | + // ^ call to t.Parallel() |
| 61 | + fmt.Println(tc.name) |
| 62 | + }) |
| 63 | + } |
| 64 | +} |
66 | 65 | // Error displayed
|
67 | 66 | // Range statement for test TestFunctionRangeMissingCallToParallel missing the call to method parallel in t.Run
|
68 | 67 | ```
|
69 | 68 |
|
70 |
| - |
71 |
| - |
72 |
| -### t.Parallel() is called in the range method but testcase variable not being used |
| 69 | +### `t.Parallel()` is called in the range method but testcase variable not being used |
73 | 70 |
|
74 | 71 | ```go
|
75 | 72 | // bad
|
76 | 73 | func TestFunctionRangeNotUsingRangeValueInTDotRun(t *testing.T) {
|
77 |
| - t.Parallel() |
78 |
| - |
79 |
| - testCases := []struct { |
80 |
| - name string |
81 |
| - }{{name: "foo"}} |
82 |
| - for _, tc := range testCases { |
83 |
| - t.Run("this is a test name", func(t *testing.T) { |
84 |
| - // ^ call to tc.name missing |
85 |
| - t.Parallel() |
86 |
| - fmt.Println(tc.name) |
87 |
| - }) |
88 |
| - } |
| 74 | + t.Parallel() |
| 75 | + |
| 76 | + testCases := []struct { |
| 77 | + name string |
| 78 | + }{{name: "foo"}} |
| 79 | + for _, tc := range testCases { |
| 80 | + t.Run("this is a test name", func(t *testing.T) { |
| 81 | + // ^ call to tc.name missing |
| 82 | + t.Parallel() |
| 83 | + fmt.Println(tc.name) |
| 84 | + }) |
| 85 | + } |
89 | 86 | }
|
90 | 87 |
|
91 | 88 | // good
|
92 | 89 | func TestFunctionRangeNotUsingRangeValueInTDotRun(t *testing.T) {
|
93 |
| - t.Parallel() |
94 |
| - |
95 |
| - testCases := []struct { |
96 |
| - name string |
97 |
| - }{{name: "foo"}} |
98 |
| - for _, tc := range testCases { |
99 |
| - t.Run(tc.name, func(t *testing.T) { |
100 |
| - // ^ call to tc.name |
101 |
| - t.Parallel() |
102 |
| - fmt.Println(tc.name) |
103 |
| - }) |
104 |
| - } |
| 90 | + t.Parallel() |
| 91 | + |
| 92 | + testCases := []struct { |
| 93 | + name string |
| 94 | + }{{name: "foo"}} |
| 95 | + for _, tc := range testCases { |
| 96 | + t.Run(tc.name, func(t *testing.T) { |
| 97 | + // ^ call to tc.name |
| 98 | + t.Parallel() |
| 99 | + fmt.Println(tc.name) |
| 100 | + }) |
| 101 | + } |
105 | 102 | }
|
106 | 103 | // Error displayed
|
107 | 104 | // Range statement for test TestFunctionRangeNotUsingRangeValueInTDotRun does not use range value in t.Run
|
108 | 105 | ```
|
109 | 106 |
|
110 |
| -### t.Parallel() is called in the range method and test case variable tc being used, but is not reinitialised (<a href="https://gist.github.com/kunwardeep/80c2e9f3d3256c894898bae82d9f75d0" target="_blank">More Info</a>) |
| 107 | +### `t.Parallel()` is called in the range method and test case variable tc being used, but is not reinitialised (<a href="https://gist.github.com/kunwardeep/80c2e9f3d3256c894898bae82d9f75d0" target="_blank">More Info</a>) |
111 | 108 | ```go
|
112 | 109 | // bad
|
113 | 110 | func TestFunctionRangeNotReInitialisingVariable(t *testing.T) {
|
114 |
| - t.Parallel() |
115 |
| - |
116 |
| - testCases := []struct { |
117 |
| - name string |
118 |
| - }{{name: "foo"}} |
119 |
| - for _, tc := range testCases { |
120 |
| - t.Run(tc.name, func(t *testing.T) { |
121 |
| - t.Parallel() |
122 |
| - fmt.Println(tc.name) |
123 |
| - }) |
124 |
| - } |
| 111 | + t.Parallel() |
| 112 | + |
| 113 | + testCases := []struct { |
| 114 | + name string |
| 115 | + }{{name: "foo"}} |
| 116 | + for _, tc := range testCases { |
| 117 | + t.Run(tc.name, func(t *testing.T) { |
| 118 | + t.Parallel() |
| 119 | + fmt.Println(tc.name) |
| 120 | + }) |
| 121 | + } |
125 | 122 | }
|
126 | 123 |
|
127 | 124 | // good
|
128 | 125 | func TestFunctionRangeNotReInitialisingVariable(t *testing.T) {
|
129 |
| - t.Parallel() |
130 |
| - |
131 |
| - testCases := []struct { |
132 |
| - name string |
133 |
| - }{{name: "foo"}} |
134 |
| - for _, tc := range testCases { |
135 |
| - tc:=tc |
136 |
| - // ^ tc variable reinitialised |
137 |
| - t.Run(tc.name, func(t *testing.T) { |
138 |
| - t.Parallel() |
139 |
| - fmt.Println(tc.name) |
140 |
| - }) |
141 |
| - } |
| 126 | + t.Parallel() |
| 127 | + |
| 128 | + testCases := []struct { |
| 129 | + name string |
| 130 | + }{{name: "foo"}} |
| 131 | + for _, tc := range testCases { |
| 132 | + tc:=tc |
| 133 | + // ^ tc variable reinitialised |
| 134 | + t.Run(tc.name, func(t *testing.T) { |
| 135 | + t.Parallel() |
| 136 | + fmt.Println(tc.name) |
| 137 | + }) |
| 138 | + } |
142 | 139 | }
|
143 | 140 | // Error displayed
|
144 | 141 | // Range statement for test TestFunctionRangeNotReInitialisingVariable does not reinitialise the variable tc
|
|
0 commit comments