-
Notifications
You must be signed in to change notification settings - Fork 277
Expand file tree
/
Copy pathretry_flaky_test.go
More file actions
131 lines (102 loc) · 3.78 KB
/
Copy pathretry_flaky_test.go
File metadata and controls
131 lines (102 loc) · 3.78 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package godog
import (
"bytes"
"context"
"fmt"
"io"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_RetryFlakySteps(t *testing.T) {
output := new(bytes.Buffer)
featureContents := []Feature{
{
Name: "RetryFlaky",
Contents: []byte(`
Feature: retry flaky steps
Scenario: Test cases that pass aren't retried
Given a step that always passes
Scenario: Test cases that fail are retried if within the limit
Given a step that passes the second time
Scenario: Test cases that fail will continue to retry up to the limit
Given a step that passes the third time
Scenario: Test cases won't retry after failing more than the limit
Given a step that always fails
Scenario: Test cases won't retry when the status is UNDEFINED
Given a non-existent step
`),
},
}
opts := Options{
NoColors: true,
Format: "pretty",
Output: output,
FeatureContents: featureContents,
MaxRetries: 3,
}
status := TestSuite{
Name: "retry flaky",
ScenarioInitializer: func(ctx *ScenarioContext) {
ctx.Step(`^a step that always passes`, func(ctx context.Context) (context.Context, error) {
return ctx, nil
})
secondTimePass := 0
ctx.Step(`^a step that passes the second time`, func(ctx context.Context) (context.Context, error) {
secondTimePass++
if secondTimePass < 2 {
return ctx, fmt.Errorf("unexpected network connection, %w", ErrRetry)
}
return ctx, nil
})
thirdTimePass := 0
ctx.Step(`^a step that passes the third time`, func(ctx context.Context) (context.Context, error) {
thirdTimePass++
if thirdTimePass < 3 {
return ctx, fmt.Errorf("unexpected network connection, %w", ErrRetry)
}
return ctx, nil
})
fifthTimePass := 0
ctx.Step(`^a step that always fails`, func(ctx context.Context) (context.Context, error) {
fifthTimePass++
if fifthTimePass < 5 {
return ctx, fmt.Errorf("must fail, %w", ErrRetry)
}
return ctx, nil
})
},
Options: &opts,
}.Run()
const expected = `Feature: retry flaky steps
Scenario: Test cases that pass aren't retried # RetryFlaky:3
Given a step that always passes # retry_flaky_test.go:51 -> github.com/cucumber/godog.Test_RetryFlakySteps.func1.1
Scenario: Test cases that fail are retried if within the limit # RetryFlaky:6
Given a step that passes the second time # retry_flaky_test.go:56 -> github.com/cucumber/godog.Test_RetryFlakySteps.func1.2
Scenario: Test cases that fail will continue to retry up to the limit # RetryFlaky:9
Given a step that passes the third time # retry_flaky_test.go:65 -> github.com/cucumber/godog.Test_RetryFlakySteps.func1.3
Scenario: Test cases won't retry after failing more than the limit # RetryFlaky:12
Given a step that always fails # retry_flaky_test.go:74 -> github.com/cucumber/godog.Test_RetryFlakySteps.func1.4
must fail, retry step
Scenario: Test cases won't retry when the status is UNDEFINED # RetryFlaky:15
Given a non-existent step
--- Failed steps:
Scenario: Test cases won't retry after failing more than the limit # RetryFlaky:12
Given a step that always fails # RetryFlaky:13
Error: must fail, retry step
5 scenarios (3 passed, 1 failed, 1 undefined)
5 steps (3 passed, 1 failed, 1 undefined)
0s
You can implement step definitions for undefined steps with these snippets:
func aNonexistentStep() error {
return godog.ErrPending
}
func InitializeScenario(ctx *godog.ScenarioContext) {
ctx.Step(` + "`^a non-existent step$`" + `, aNonexistentStep)
}
`
actualOutput, err := io.ReadAll(output)
require.NoError(t, err)
assert.Equal(t, exitFailure, status)
assert.Equal(t, expected, string(actualOutput))
}