-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatter_summary.go
150 lines (128 loc) · 4.35 KB
/
formatter_summary.go
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package cucumber
import (
"fmt"
"io"
"strings"
"time"
messages "github.com/cucumber/cucumber-messages-go/v3"
"github.com/fatih/color"
)
type stepDescription struct {
ScenarioName string
ScenarioLocation string
StepName string
StepLocation string
Error string
}
type summaryFormatter struct {
out io.Writer
failedSteps []stepDescription
pickleMap map[string]*messages.Pickle
start time.Time
duration time.Duration
Success bool
TestCasesTotal int
TestCasesPassed int
TestCasesFailed int
TestCasesPending int
TestCasesUndefined int
StepsTotal int
StepsPassed int
StepsFailed int
StepsPending int
StepsUndefined int
StepsSkipped int
}
func NewSummaryFormatter(stdout io.Writer) *summaryFormatter {
return &summaryFormatter{
out: stdout,
pickleMap: map[string]*messages.Pickle{},
}
}
func (sf *summaryFormatter) ProcessMessage(msg *messages.Envelope) {
switch m := msg.Message.(type) {
case *messages.Envelope_TestRunStarted:
sf.start = time.Now()
case *messages.Envelope_TestRunFinished:
sf.duration = time.Since(sf.start)
sf.Success = m.TestRunFinished.Success
sf.displaySummary()
case *messages.Envelope_CommandInitializeTestCase:
sf.TestCasesTotal += 1
sf.pickleMap[m.CommandInitializeTestCase.Pickle.Id] = m.CommandInitializeTestCase.Pickle
case *messages.Envelope_TestCaseFinished:
switch m.TestCaseFinished.TestResult.Status {
case messages.TestResult_PASSED:
sf.TestCasesPassed += 1
case messages.TestResult_FAILED:
sf.TestCasesFailed += 1
case messages.TestResult_PENDING:
sf.TestCasesPending += 1
case messages.TestResult_UNDEFINED:
sf.TestCasesUndefined += 1
}
case *messages.Envelope_TestStepFinished:
sf.StepsTotal += 1
switch m.TestStepFinished.TestResult.Status {
case messages.TestResult_PASSED:
sf.StepsPassed += 1
case messages.TestResult_FAILED:
sf.StepsFailed += 1
pickle := sf.pickleMap[m.TestStepFinished.PickleId]
pickleLocation := pickle.Locations[len(pickle.Locations)-1].Line
step := pickle.Steps[m.TestStepFinished.Index]
stepLocation := step.Locations[len(step.Locations)-1].Line
sf.failedSteps = append(sf.failedSteps, stepDescription{
ScenarioName: pickle.Name,
ScenarioLocation: fmt.Sprintf("%s:%d", pickle.Uri, pickleLocation),
StepName: step.Text,
StepLocation: fmt.Sprintf("%s:%d", pickle.Uri, stepLocation),
Error: m.TestStepFinished.TestResult.Message,
})
case messages.TestResult_PENDING:
sf.StepsPending += 1
case messages.TestResult_UNDEFINED:
sf.StepsUndefined += 1
case messages.TestResult_SKIPPED:
sf.StepsSkipped += 1
}
}
}
func (sf *summaryFormatter) displaySummary() {
if len(sf.failedSteps) > 0 {
color.New(failureColor).Fprint(sf.out, "\n\nFailed steps:\n")
for _, fs := range sf.failedSteps {
color.New(failureColor).Fprintf(sf.out, "\n Scenario: %s", fs.ScenarioName)
color.New(color.FgBlack).Fprintf(sf.out, " # %s\n", fs.ScenarioLocation)
color.New(failureColor).Fprintf(sf.out, " %s", fs.StepName)
color.New(color.FgBlack).Fprintf(sf.out, " # %s\n", fs.StepLocation)
color.New(failureColor).Fprint(sf.out, " Error: ")
color.New(color.FgHiRed).Fprintf(sf.out, "%s\n", fs.Error)
}
}
fmt.Fprint(sf.out, "\n")
scenarioStatusSummary := statusSummary(sf.TestCasesPassed, sf.TestCasesFailed, sf.TestCasesPending, sf.TestCasesUndefined, 0)
fmt.Fprintf(sf.out, "%d scenarios (%s)\n", sf.TestCasesTotal, scenarioStatusSummary)
stepStatusSummary := statusSummary(sf.StepsPassed, sf.StepsFailed, sf.StepsPending, sf.StepsUndefined, sf.StepsSkipped)
fmt.Fprintf(sf.out, "%d steps (%s)\n", sf.StepsTotal, stepStatusSummary)
fmt.Fprintln(sf.out, sf.duration)
}
func statusSummary(passed, failed, pending, undefined, skipped int) string {
var acc []string
if passed > 0 {
acc = append(acc, color.New(successColor).Sprintf("%d passed", passed))
}
if failed > 0 {
acc = append(acc, color.New(failureColor).Sprintf("%d failed", failed))
}
if pending > 0 {
acc = append(acc, color.New(pendingColor).Sprintf("%d pending", pending))
}
if undefined > 0 {
acc = append(acc, color.New(undefinedColor).Sprintf("%d undefined", undefined))
}
if skipped > 0 {
acc = append(acc, color.New(skippedColor).Sprintf("%d skipped", skipped))
}
return strings.Join(acc, ", ")
}