Skip to content

Commit 05443ce

Browse files
authored
tests: Convert executor tests to subtests (#315)
1 parent c253398 commit 05443ce

18 files changed

+2211
-1734
lines changed

executor/context_test.go

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,23 @@ func TestExecutor_FromContext(t *testing.T) {
5151

5252
// setup tests
5353
tests := []struct {
54+
name string
5455
context context.Context
5556
want Engine
5657
}{
5758
{
59+
name: "valid executor in context",
5860
// nolint: staticcheck,revive // ignore using string with context value
5961
context: context.WithValue(context.Background(), key, _engine),
6062
want: _engine,
6163
},
6264
{
65+
name: "executor not in context",
6366
context: context.Background(),
6467
want: nil,
6568
},
6669
{
70+
name: "invalid executor in context",
6771
// nolint: staticcheck,revive // ignore using string with context value
6872
context: context.WithValue(context.Background(), key, "foo"),
6973
want: nil,
@@ -72,11 +76,13 @@ func TestExecutor_FromContext(t *testing.T) {
7276

7377
// run tests
7478
for _, test := range tests {
75-
got := FromContext(test.context)
79+
t.Run(test.name, func(t *testing.T) {
80+
got := FromContext(test.context)
7681

77-
if !reflect.DeepEqual(got, test.want) {
78-
t.Errorf("FromContext is %v, want %v", got, test.want)
79-
}
82+
if !reflect.DeepEqual(got, test.want) {
83+
t.Errorf("FromContext is %v, want %v", got, test.want)
84+
}
85+
})
8086
}
8187
}
8288

@@ -110,21 +116,25 @@ func TestExecutor_FromGinContext(t *testing.T) {
110116

111117
// setup tests
112118
tests := []struct {
119+
name string
113120
context *gin.Context
114121
value interface{}
115122
want Engine
116123
}{
117124
{
125+
name: "valid executor in context",
118126
context: new(gin.Context),
119127
value: _engine,
120128
want: _engine,
121129
},
122130
{
131+
name: "executor not in context",
123132
context: new(gin.Context),
124133
value: nil,
125134
want: nil,
126135
},
127136
{
137+
name: "invalid executor in context",
128138
context: new(gin.Context),
129139
value: "foo",
130140
want: nil,
@@ -133,15 +143,17 @@ func TestExecutor_FromGinContext(t *testing.T) {
133143

134144
// run tests
135145
for _, test := range tests {
136-
if test.value != nil {
137-
test.context.Set(key, test.value)
138-
}
146+
t.Run(test.name, func(t *testing.T) {
147+
if test.value != nil {
148+
test.context.Set(key, test.value)
149+
}
139150

140-
got := FromGinContext(test.context)
151+
got := FromGinContext(test.context)
141152

142-
if !reflect.DeepEqual(got, test.want) {
143-
t.Errorf("FromGinContext is %v, want %v", got, test.want)
144-
}
153+
if !reflect.DeepEqual(got, test.want) {
154+
t.Errorf("FromGinContext is %v, want %v", got, test.want)
155+
}
156+
})
145157
}
146158
}
147159

executor/executor_test.go

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@ func TestExecutor_New(t *testing.T) {
7373

7474
// setup tests
7575
tests := []struct {
76+
name string
7677
failure bool
7778
setup *Setup
7879
want Engine
7980
}{
8081
{
82+
name: "driver-darwin",
8183
failure: true,
8284
setup: &Setup{
8385
Build: _build,
@@ -92,6 +94,7 @@ func TestExecutor_New(t *testing.T) {
9294
want: nil,
9395
},
9496
{
97+
name: "driver-linux",
9598
failure: false,
9699
setup: &Setup{
97100
Build: _build,
@@ -108,6 +111,7 @@ func TestExecutor_New(t *testing.T) {
108111
want: _linux,
109112
},
110113
{
114+
name: "driver-local",
111115
failure: false,
112116
setup: &Setup{
113117
Build: _build,
@@ -122,6 +126,7 @@ func TestExecutor_New(t *testing.T) {
122126
want: _local,
123127
},
124128
{
129+
name: "driver-windows",
125130
failure: true,
126131
setup: &Setup{
127132
Build: _build,
@@ -136,6 +141,7 @@ func TestExecutor_New(t *testing.T) {
136141
want: nil,
137142
},
138143
{
144+
name: "driver-invalid",
139145
failure: true,
140146
setup: &Setup{
141147
Build: _build,
@@ -150,6 +156,7 @@ func TestExecutor_New(t *testing.T) {
150156
want: nil,
151157
},
152158
{
159+
name: "driver-empty",
153160
failure: true,
154161
setup: &Setup{
155162
Build: _build,
@@ -167,27 +174,29 @@ func TestExecutor_New(t *testing.T) {
167174

168175
// run tests
169176
for _, test := range tests {
170-
got, err := New(test.setup)
177+
t.Run(test.name, func(t *testing.T) {
178+
got, err := New(test.setup)
171179

172-
if test.failure {
173-
if err == nil {
174-
t.Errorf("New should have returned err")
180+
if test.failure {
181+
if err == nil {
182+
t.Errorf("New should have returned err")
183+
}
184+
185+
if !reflect.DeepEqual(got, test.want) {
186+
t.Errorf("New is %v, want %v", got, test.want)
187+
}
188+
189+
return // continue to next test
190+
}
191+
192+
if err != nil {
193+
t.Errorf("New returned err: %v", err)
175194
}
176195

177196
if !reflect.DeepEqual(got, test.want) {
178197
t.Errorf("New is %v, want %v", got, test.want)
179198
}
180-
181-
continue
182-
}
183-
184-
if err != nil {
185-
t.Errorf("New returned err: %v", err)
186-
}
187-
188-
if !reflect.DeepEqual(got, test.want) {
189-
t.Errorf("New is %v, want %v", got, test.want)
190-
}
199+
})
191200
}
192201
}
193202

executor/linux/api_test.go

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,38 +22,43 @@ func TestLinux_GetBuild(t *testing.T) {
2222

2323
// setup tests
2424
tests := []struct {
25+
name string
2526
failure bool
2627
engine *client
2728
}{
2829
{
30+
name: "with build",
2931
failure: false,
3032
engine: _engine,
3133
},
3234
{
35+
name: "missing build",
3336
failure: true,
3437
engine: new(client),
3538
},
3639
}
3740

3841
// run tests
3942
for _, test := range tests {
40-
got, err := test.engine.GetBuild()
43+
t.Run(test.name, func(t *testing.T) {
44+
got, err := test.engine.GetBuild()
4145

42-
if test.failure {
43-
if err == nil {
44-
t.Errorf("GetBuild should have returned err")
45-
}
46+
if test.failure {
47+
if err == nil {
48+
t.Errorf("GetBuild should have returned err")
49+
}
4650

47-
continue
48-
}
51+
return // continue to next test
52+
}
4953

50-
if err != nil {
51-
t.Errorf("GetBuild returned err: %v", err)
52-
}
54+
if err != nil {
55+
t.Errorf("GetBuild returned err: %v", err)
56+
}
5357

54-
if !reflect.DeepEqual(got, _build) {
55-
t.Errorf("GetBuild is %v, want %v", got, _build)
56-
}
58+
if !reflect.DeepEqual(got, _build) {
59+
t.Errorf("GetBuild is %v, want %v", got, _build)
60+
}
61+
})
5762
}
5863
}
5964

@@ -70,38 +75,43 @@ func TestLinux_GetPipeline(t *testing.T) {
7075

7176
// setup tests
7277
tests := []struct {
78+
name string
7379
failure bool
7480
engine *client
7581
}{
7682
{
83+
name: "with pipeline",
7784
failure: false,
7885
engine: _engine,
7986
},
8087
{
88+
name: "missing pipeline",
8189
failure: true,
8290
engine: new(client),
8391
},
8492
}
8593

8694
// run tests
8795
for _, test := range tests {
88-
got, err := test.engine.GetPipeline()
96+
t.Run(test.name, func(t *testing.T) {
97+
got, err := test.engine.GetPipeline()
8998

90-
if test.failure {
91-
if err == nil {
92-
t.Errorf("GetPipeline should have returned err")
93-
}
99+
if test.failure {
100+
if err == nil {
101+
t.Errorf("GetPipeline should have returned err")
102+
}
94103

95-
continue
96-
}
104+
return // continue to next test
105+
}
97106

98-
if err != nil {
99-
t.Errorf("GetPipeline returned err: %v", err)
100-
}
107+
if err != nil {
108+
t.Errorf("GetPipeline returned err: %v", err)
109+
}
101110

102-
if !reflect.DeepEqual(got, _steps) {
103-
t.Errorf("GetPipeline is %v, want %v", got, _steps)
104-
}
111+
if !reflect.DeepEqual(got, _steps) {
112+
t.Errorf("GetPipeline is %v, want %v", got, _steps)
113+
}
114+
})
105115
}
106116
}
107117

@@ -118,37 +128,42 @@ func TestLinux_GetRepo(t *testing.T) {
118128

119129
// setup tests
120130
tests := []struct {
131+
name string
121132
failure bool
122133
engine *client
123134
}{
124135
{
136+
name: "with repo",
125137
failure: false,
126138
engine: _engine,
127139
},
128140
{
141+
name: "missing repo",
129142
failure: true,
130143
engine: new(client),
131144
},
132145
}
133146

134147
// run tests
135148
for _, test := range tests {
136-
got, err := test.engine.GetRepo()
149+
t.Run(test.name, func(t *testing.T) {
150+
got, err := test.engine.GetRepo()
137151

138-
if test.failure {
139-
if err == nil {
140-
t.Errorf("GetRepo should have returned err")
141-
}
152+
if test.failure {
153+
if err == nil {
154+
t.Errorf("GetRepo should have returned err")
155+
}
142156

143-
continue
144-
}
157+
return // continue to next test
158+
}
145159

146-
if err != nil {
147-
t.Errorf("GetRepo returned err: %v", err)
148-
}
160+
if err != nil {
161+
t.Errorf("GetRepo returned err: %v", err)
162+
}
149163

150-
if !reflect.DeepEqual(got, _repo) {
151-
t.Errorf("GetRepo is %v, want %v", got, _repo)
152-
}
164+
if !reflect.DeepEqual(got, _repo) {
165+
t.Errorf("GetRepo is %v, want %v", got, _repo)
166+
}
167+
})
153168
}
154169
}

0 commit comments

Comments
 (0)