Skip to content

Commit f9aabcd

Browse files
committed
common failed scenario output for progress and pretty format: #113
1 parent cf27a61 commit f9aabcd

File tree

4 files changed

+61
-30
lines changed

4 files changed

+61
-30
lines changed

fmt.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,54 @@ func (f stepResult) line() string {
161161
return fmt.Sprintf("%s:%d", f.feature.Path, f.step.Location.Line)
162162
}
163163

164+
func (f stepResult) scenarioDesc() string {
165+
if sc, ok := f.owner.(*gherkin.Scenario); ok {
166+
return fmt.Sprintf("%s: %s", sc.Keyword, sc.Name)
167+
}
168+
169+
if row, ok := f.owner.(*gherkin.TableRow); ok {
170+
for _, def := range f.feature.Feature.ScenarioDefinitions {
171+
out, ok := def.(*gherkin.ScenarioOutline)
172+
if !ok {
173+
continue
174+
}
175+
176+
for _, ex := range out.Examples {
177+
for _, rw := range ex.TableBody {
178+
if rw.Location.Line == row.Location.Line {
179+
return fmt.Sprintf("%s: %s", out.Keyword, out.Name)
180+
}
181+
}
182+
}
183+
}
184+
}
185+
return f.line() // was not expecting different owner
186+
}
187+
188+
func (f stepResult) scenarioLine() string {
189+
if sc, ok := f.owner.(*gherkin.Scenario); ok {
190+
return fmt.Sprintf("%s:%d", f.feature.Path, sc.Location.Line)
191+
}
192+
193+
if row, ok := f.owner.(*gherkin.TableRow); ok {
194+
for _, def := range f.feature.Feature.ScenarioDefinitions {
195+
out, ok := def.(*gherkin.ScenarioOutline)
196+
if !ok {
197+
continue
198+
}
199+
200+
for _, ex := range out.Examples {
201+
for _, rw := range ex.TableBody {
202+
if rw.Location.Line == row.Location.Line {
203+
return fmt.Sprintf("%s:%d", f.feature.Path, out.Location.Line)
204+
}
205+
}
206+
}
207+
}
208+
}
209+
return f.line() // was not expecting different owner
210+
}
211+
164212
type basefmt struct {
165213
out io.Writer
166214
owner interface{}

fmt_pretty.go

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -120,34 +120,12 @@ func (f *pretty) printUndefinedScenario(sc *gherkin.Scenario) {
120120

121121
// Summary sumarize the feature formatter output
122122
func (f *pretty) Summary() {
123-
// failed steps on background are not scenarios
124-
var failedScenarios []*stepResult
125-
for _, fail := range f.failed {
126-
switch fail.owner.(type) {
127-
case *gherkin.Scenario:
128-
failedScenarios = append(failedScenarios, fail)
129-
case *gherkin.TableRow:
130-
failedScenarios = append(failedScenarios, fail)
131-
}
132-
}
133-
if len(failedScenarios) > 0 {
134-
fmt.Fprintln(f.out, "\n--- "+red("Failed scenarios:")+"\n")
135-
var unique []string
136-
for _, fail := range failedScenarios {
137-
var found bool
138-
for _, in := range unique {
139-
if in == fail.line() {
140-
found = true
141-
break
142-
}
143-
}
144-
if !found {
145-
unique = append(unique, fail.line())
146-
}
147-
}
148-
149-
for _, fail := range unique {
150-
fmt.Fprintln(f.out, " "+red(fail))
123+
if len(f.failed) > 0 {
124+
fmt.Fprintln(f.out, "\n--- "+red("Failed steps:")+"\n")
125+
for _, fail := range f.failed {
126+
fmt.Fprintln(f.out, s(2)+red(fail.scenarioDesc())+black(" # "+fail.scenarioLine()))
127+
fmt.Fprintln(f.out, s(4)+red(strings.TrimSpace(fail.step.Keyword)+" "+fail.step.Text)+black(" # "+fail.line()))
128+
fmt.Fprintln(f.out, s(6)+red("Error: ")+redb(fmt.Sprintf("%+v", fail.err))+"\n")
151129
}
152130
}
153131
f.basefmt.Summary()

fmt_progress.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ func (f *progress) Summary() {
5858
if len(f.failed) > 0 {
5959
fmt.Fprintln(f.out, "\n--- "+red("Failed steps:")+"\n")
6060
for _, fail := range f.failed {
61+
fmt.Fprintln(f.out, s(2)+red(fail.scenarioDesc())+black(" # "+fail.scenarioLine()))
6162
fmt.Fprintln(f.out, s(4)+red(strings.TrimSpace(fail.step.Keyword)+" "+fail.step.Text)+black(" # "+fail.line()))
6263
fmt.Fprintln(f.out, s(6)+red("Error: ")+redb(fmt.Sprintf("%+v", fail.err))+"\n")
6364
}

fmt_progress_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,12 @@ func TestProgressFormatterOutput(t *testing.T) {
4141
4242
--- Failed steps:
4343
44+
Scenario: failing scenario # any.feature:10
4445
When failing # any.feature:11
4546
Error: errored
4647
47-
When failing # any.feature:24
48+
Scenario Outline: outline # any.feature:22
49+
When failing # any.feature:24
4850
Error: errored
4951
5052
@@ -118,7 +120,7 @@ func TestProgressFormatterWhenStepPanics(t *testing.T) {
118120
}
119121

120122
out := buf.String()
121-
if idx := strings.Index(out, "github.com/DATA-DOG/godog/fmt_progress_test.go:116"); idx == -1 {
123+
if idx := strings.Index(out, "github.com/DATA-DOG/godog/fmt_progress_test.go:114"); idx == -1 {
122124
t.Fatalf("expected to find panic stacktrace, actual:\n%s", out)
123125
}
124126
}
@@ -178,6 +180,7 @@ func TestProgressFormatterWithFailingMultisteps(t *testing.T) {
178180
179181
--- Failed steps:
180182
183+
Scenario: passing scenario # some.feature:4
181184
Then two # some.feature:6
182185
Error: sub2: sub-sub: errored
183186
@@ -385,6 +388,7 @@ Feature: basic
385388
386389
--- Failed steps:
387390
391+
Scenario: passing scenario # :4
388392
Then two # :6
389393
Error: nested steps cannot be multiline and have table or content body argument
390394

0 commit comments

Comments
 (0)