diff --git a/CHANGELOG.md b/CHANGELOG.md index 9d8132af..e85ebaad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ This document is formatted according to the principles of [Keep A CHANGELOG](htt ### Fixed +- After Scenario hook is called before After Step ([444](https://github.com/cucumber/godog/pull/444) - [vearutop]) - `check-go-version` in Makefile to run on WSL. ([443](https://github.com/cucumber/godog/pull/443) - [mxygem]) ## [v0.12.2] diff --git a/features/events.feature b/features/events.feature index c9f53f96..8985914c 100644 --- a/features/events.feature +++ b/features/events.feature @@ -72,9 +72,9 @@ Feature: suite events Scenario: two Then passing step - And failing step And adding step state to context And having correct context + And failing step Scenario Outline: three Then passing step diff --git a/suite.go b/suite.go index fc77caba..2d80bed3 100644 --- a/suite.go +++ b/suite.go @@ -89,26 +89,24 @@ func (s *suite) runStep(ctx context.Context, pickle *Scenario, step *Step, prevS } } - defer func() { - // run after step handlers - rctx, err = s.runAfterStepHooks(ctx, step, sr.Status, err) - }() + earlyReturn := prevStepErr != nil || err == ErrUndefined - if prevStepErr != nil { - return - } - - if err == ErrUndefined { - return + if !earlyReturn { + sr = models.NewStepResult(pickle.Id, step.Id, match) } - sr = models.NewStepResult(pickle.Id, step.Id, match) + // Run after step handlers. + rctx, err = s.runAfterStepHooks(ctx, step, sr.Status, err) // Trigger after scenario on failing or last step to attach possible hook error to step. - if (err == nil && isLast) || err != nil { + if sr.Status != StepSkipped && ((err == nil && isLast) || err != nil) { rctx, err = s.runAfterScenarioHooks(rctx, pickle, err) } + if earlyReturn { + return + } + switch err { case nil: sr.Status = models.Passed diff --git a/suite_context_test.go b/suite_context_test.go index f904fa81..e4ae8c7d 100644 --- a/suite_context_test.go +++ b/suite_context_test.go @@ -423,12 +423,12 @@ func (tc *godogFeaturesScenario) iAmListeningToSuiteEvents() error { scenarioContext.Before(func(ctx context.Context, pickle *Scenario) (context.Context, error) { tc.events = append(tc.events, &firedEvent{"BeforeScenario", []interface{}{pickle}}) - return context.WithValue(ctx, ctxKey("BeforeScenario"), true), nil + return context.WithValue(ctx, ctxKey("BeforeScenario"), pickle.Name), nil }) scenarioContext.Before(func(ctx context.Context, sc *Scenario) (context.Context, error) { if sc.Name == "failing before and after scenario" || sc.Name == "failing before scenario" { - return context.WithValue(ctx, ctxKey("AfterStep"), true), errors.New("failed in before scenario hook") + return context.WithValue(ctx, ctxKey("AfterStep"), sc.Name), errors.New("failed in before scenario hook") } return ctx, nil @@ -453,7 +453,7 @@ func (tc *godogFeaturesScenario) iAmListeningToSuiteEvents() error { return ctx, errors.New("missing AfterStep in context") } - return context.WithValue(ctx, ctxKey("AfterScenario"), true), nil + return context.WithValue(ctx, ctxKey("AfterScenario"), pickle.Name), nil }) scenarioContext.StepContext().Before(func(ctx context.Context, step *Step) (context.Context, error) { @@ -463,7 +463,7 @@ func (tc *godogFeaturesScenario) iAmListeningToSuiteEvents() error { return ctx, errors.New("missing BeforeScenario in context") } - return context.WithValue(ctx, ctxKey("BeforeStep"), true), nil + return context.WithValue(ctx, ctxKey("BeforeStep"), step.Text), nil }) 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 { return ctx, errors.New("missing BeforeScenario in context") } + if ctx.Value(ctxKey("AfterScenario")) != nil && status != models.Skipped { + panic("unexpected premature AfterScenario during AfterStep: " + ctx.Value(ctxKey("AfterScenario")).(string)) + } + if ctx.Value(ctxKey("BeforeStep")) == nil { return ctx, errors.New("missing BeforeStep in context") } @@ -485,7 +489,7 @@ func (tc *godogFeaturesScenario) iAmListeningToSuiteEvents() error { return ctx, errors.New("missing Step in context") } - return context.WithValue(ctx, ctxKey("AfterStep"), true), nil + return context.WithValue(ctx, ctxKey("AfterStep"), step.Text), nil }) return nil @@ -699,10 +703,7 @@ func (tc *godogFeaturesScenario) theRenderOutputWillBe(docstring *DocString) err actualTrimmed := actual actual = trimAllLines(actual) - expectedRows := strings.Split(expected, "\n") - actualRows := strings.Split(actual, "\n") - - return assertExpectedAndActual(assert.ElementsMatch, expectedRows, actualRows, actualTrimmed) + return assertExpectedAndActual(assert.Equal, expected, actual, actualTrimmed) } func (tc *godogFeaturesScenario) theRenderXMLWillBe(docstring *DocString) error {