Skip to content

feat: print the scenario narrative without source prefixes - #8

Merged
jacoelho merged 1 commit into
mainfrom
feat/scenario-narrative-output
Nov 10, 2026
Merged

jacoelho merged 1 commit into
mainfrom
feat/scenario-narrative-output

Conversation

@jacoelho

Copy link
Copy Markdown
Owner

What changes

The output of a running scenario (shown with -v or on failure) changes format.

Before:

    cart_test.go:33: scenario: two books cost 2000
    cart_test.go:33: Setup: scenario: two books cost 2000
    cart_test.go:33: Given: an empty cart
    cart_test.go:33: When: the customer adds two books
    cart_test.go:33: Then: the total is 2000

After:

    Scenario: two books cost 2000       # cart_test.go:34
      Given an empty cart               # cart_test.go:35
      When the customer adds two books  # cart_test.go:38
      Then the total is 2000            # cart_test.go:41
  • Lines no longer carry the bdd.Feature call site as a prefix. Each line ends with the file and line that declared it: the scenario, the bdd.Case for an Outline example, and each hook and step.
  • A scenario's Rules or Outline print on one line above its title, for example Rule: outer > Rule: inner.
  • Background prints with a colon (Background: an empty cart); the other keywords do not.
  • The Setup: line is removed. A setup failure is still reported as stopped in Setup.
  • The stop report now follows the failure directly instead of the scenario's cleanups, and names its source as file:line instead of an absolute path.
  • Skip messages capitalise the node kind: bdd: Scenario: ... pending: ... (was bdd: scenario: ...).

Anything that parses the old log lines will need updating. Exported API, generated test names and lifecycle order are unchanged. Lines logged by user code keep the standard file:line: prefix and still appear under the operation that logged them.

Why

Every framework line had the same prefix, the bdd.Feature call line, which told the reader nothing, and the output never said where a step was declared unless the scenario failed.

How

The narrative is written through testing.T.Output() (Go 1.25+; the module requires 1.27), which uses the test's output stream without adding a source prefix. The stop report uses the same stream from a defer, replacing the Cleanup it needed when it went through Log. New file: narrative.go.

Verification

Run locally on Go 1.27.3, darwin/arm64:

  • gofmt -l . reports nothing; go vet ./... is clean; go test -race -count=1 ./... passes.
  • New test TestNarrativeNamesEachLineWithItsDeclarationSite checks each line's declaration site against testing's own attribution of a marker logged on that line, plus line order, interleaving with user logs, and column alignment.
  • Four mutations (wrong source for the title, no alignment, dropped Helper on the leaf closure, narrative line printed after the operation) each failed a test.
  • The output listings in the README and package doc were copied from real runs, and the README's Go blocks still compile and pass in a scratch module.

Where to look

  • narrative.go and featurePlan.runLeaf in suite.go.
  • Column alignment pads by rune count, so wide (for example CJK) characters can misalign the # column. This is cosmetic.
  • Hidden Before hooks are still listed, as Before hook # file:line.

A running scenario now writes its narrative through testing.T.Output
instead of Log. Lines no longer repeat the Feature call site as a
prefix; each ends with the file and line that declared it:

    Scenario: two books cost 2000       # cart_test.go:34
      Given an empty cart               # cart_test.go:35
      When the customer adds two books  # cart_test.go:38
      Then the total is 2000            # cart_test.go:41

Rules and the Outline print on one line above the title. Background
takes a colon. The Setup line is removed. The stop report follows the
failure directly and names its source as file:line. Skip messages
capitalise the node kind.
@jacoelho
jacoelho merged commit e58a985 into main Nov 10, 2026
1 check passed
@jacoelho
jacoelho deleted the feat/scenario-narrative-output branch November 10, 2026 12:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant