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