Skip to content

Commit b850b44

Browse files
vearutopmattwynne
andauthored
Invoke After Scenario hooks after After Step hooks (cucumber#444)
* Invoke After Scenario hooks after After Step hooks * Update CHANGELOG.md Co-authored-by: Matt Wynne <[email protected]>
1 parent 82bcce7 commit b850b44

File tree

4 files changed

+22
-22
lines changed

4 files changed

+22
-22
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ This document is formatted according to the principles of [Keep A CHANGELOG](htt
1212

1313
### Fixed
1414

15+
- After Scenario hook is called before After Step ([444](https://github.com/cucumber/godog/pull/444) - [vearutop])
1516
- `check-go-version` in Makefile to run on WSL. ([443](https://github.com/cucumber/godog/pull/443) - [mxygem])
1617

1718
## [v0.12.2]

features/events.feature

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,9 @@ Feature: suite events
7272
7373
Scenario: two
7474
Then passing step
75-
And failing step
7675
And adding step state to context
7776
And having correct context
77+
And failing step
7878
7979
Scenario Outline: three
8080
Then passing step

suite.go

+10-12
Original file line numberDiff line numberDiff line change
@@ -89,26 +89,24 @@ func (s *suite) runStep(ctx context.Context, pickle *Scenario, step *Step, prevS
8989
}
9090
}
9191

92-
defer func() {
93-
// run after step handlers
94-
rctx, err = s.runAfterStepHooks(ctx, step, sr.Status, err)
95-
}()
92+
earlyReturn := prevStepErr != nil || err == ErrUndefined
9693

97-
if prevStepErr != nil {
98-
return
99-
}
100-
101-
if err == ErrUndefined {
102-
return
94+
if !earlyReturn {
95+
sr = models.NewStepResult(pickle.Id, step.Id, match)
10396
}
10497

105-
sr = models.NewStepResult(pickle.Id, step.Id, match)
98+
// Run after step handlers.
99+
rctx, err = s.runAfterStepHooks(ctx, step, sr.Status, err)
106100

107101
// Trigger after scenario on failing or last step to attach possible hook error to step.
108-
if (err == nil && isLast) || err != nil {
102+
if sr.Status != StepSkipped && ((err == nil && isLast) || err != nil) {
109103
rctx, err = s.runAfterScenarioHooks(rctx, pickle, err)
110104
}
111105

106+
if earlyReturn {
107+
return
108+
}
109+
112110
switch err {
113111
case nil:
114112
sr.Status = models.Passed

suite_context_test.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -423,12 +423,12 @@ func (tc *godogFeaturesScenario) iAmListeningToSuiteEvents() error {
423423
scenarioContext.Before(func(ctx context.Context, pickle *Scenario) (context.Context, error) {
424424
tc.events = append(tc.events, &firedEvent{"BeforeScenario", []interface{}{pickle}})
425425

426-
return context.WithValue(ctx, ctxKey("BeforeScenario"), true), nil
426+
return context.WithValue(ctx, ctxKey("BeforeScenario"), pickle.Name), nil
427427
})
428428

429429
scenarioContext.Before(func(ctx context.Context, sc *Scenario) (context.Context, error) {
430430
if sc.Name == "failing before and after scenario" || sc.Name == "failing before scenario" {
431-
return context.WithValue(ctx, ctxKey("AfterStep"), true), errors.New("failed in before scenario hook")
431+
return context.WithValue(ctx, ctxKey("AfterStep"), sc.Name), errors.New("failed in before scenario hook")
432432
}
433433

434434
return ctx, nil
@@ -453,7 +453,7 @@ func (tc *godogFeaturesScenario) iAmListeningToSuiteEvents() error {
453453
return ctx, errors.New("missing AfterStep in context")
454454
}
455455

456-
return context.WithValue(ctx, ctxKey("AfterScenario"), true), nil
456+
return context.WithValue(ctx, ctxKey("AfterScenario"), pickle.Name), nil
457457
})
458458

459459
scenarioContext.StepContext().Before(func(ctx context.Context, step *Step) (context.Context, error) {
@@ -463,7 +463,7 @@ func (tc *godogFeaturesScenario) iAmListeningToSuiteEvents() error {
463463
return ctx, errors.New("missing BeforeScenario in context")
464464
}
465465

466-
return context.WithValue(ctx, ctxKey("BeforeStep"), true), nil
466+
return context.WithValue(ctx, ctxKey("BeforeStep"), step.Text), nil
467467
})
468468

469469
scenarioContext.StepContext().After(func(ctx context.Context, step *Step, status StepResultStatus, err error) (context.Context, error) {
@@ -473,6 +473,10 @@ func (tc *godogFeaturesScenario) iAmListeningToSuiteEvents() error {
473473
return ctx, errors.New("missing BeforeScenario in context")
474474
}
475475

476+
if ctx.Value(ctxKey("AfterScenario")) != nil && status != models.Skipped {
477+
panic("unexpected premature AfterScenario during AfterStep: " + ctx.Value(ctxKey("AfterScenario")).(string))
478+
}
479+
476480
if ctx.Value(ctxKey("BeforeStep")) == nil {
477481
return ctx, errors.New("missing BeforeStep in context")
478482
}
@@ -485,7 +489,7 @@ func (tc *godogFeaturesScenario) iAmListeningToSuiteEvents() error {
485489
return ctx, errors.New("missing Step in context")
486490
}
487491

488-
return context.WithValue(ctx, ctxKey("AfterStep"), true), nil
492+
return context.WithValue(ctx, ctxKey("AfterStep"), step.Text), nil
489493
})
490494

491495
return nil
@@ -699,10 +703,7 @@ func (tc *godogFeaturesScenario) theRenderOutputWillBe(docstring *DocString) err
699703
actualTrimmed := actual
700704
actual = trimAllLines(actual)
701705

702-
expectedRows := strings.Split(expected, "\n")
703-
actualRows := strings.Split(actual, "\n")
704-
705-
return assertExpectedAndActual(assert.ElementsMatch, expectedRows, actualRows, actualTrimmed)
706+
return assertExpectedAndActual(assert.Equal, expected, actual, actualTrimmed)
706707
}
707708

708709
func (tc *godogFeaturesScenario) theRenderXMLWillBe(docstring *DocString) error {

0 commit comments

Comments
 (0)