Great work!

XP to next level

BugEater

And / But — Chaining Steps, One Scenario One Check

Learning Objectives

By the end of this lesson you will be able to:

  • Explain what And and But do, and what they don't
  • Use But to express a contrasting or negative condition clearly
  • Apply the "one scenario, one business check" rule and recognize when it's broken

And / But Inherit Their Role

And and But are not their own keyword type — each one inherits the role of the step above it. An And after a Given is still logically a Given. An And after a Then is still logically a Then.

Given a registered user with a valid session
And an empty shopping cart
When the user adds an item priced at $50
And applies the discount code "SAVE10"
Then the cart total reflects a 10% discount
And the discount code field shows a confirmation checkmark

Six lines, three logical roles: two Given, two When, two Then. And just avoids repeating the keyword and reads more naturally.

When to Reach for But

But is And's cousin for contrast — typically used before a negative or exceptional condition, to make the contrast readable:

Given a registered user with a valid session
But their account has been suspended
When they attempt to log in
Then access is denied with a "suspended account" message

Using But instead of And before "their account has been suspended" signals to the reader that this fact contradicts the otherwise-normal setup — a small readability win that pure And chains lose.

One Scenario, One Business Check

This is the rule that keeps a growing scenario suite maintainable: each scenario verifies exactly one business-meaningful check. Not one Then line necessarily — a scenario can have several related Then/And assertions — but one idea.

# Wrong — testing two unrelated business rules in one scenario
Scenario: Discount and shipping
  Given a cart containing one item priced at $50
  When the user applies the discount code "SAVE10"
  Then the cart total reflects a 10% discount
  When the user selects express shipping
  Then the estimated delivery date updates to tomorrow

Two Whens is already the tell (from the previous lesson) — this scenario secretly tests discount logic and shipping logic. Split it into two scenarios, each with its own clean Given/When/Then, each verifying one business check.

Why This Rule Matters at Scale

A ten-scenario feature file where every scenario checks one thing is easy to skim, easy to extend, and easy to debug when one fails — you know immediately which business rule broke. A file where scenarios check three things each becomes a maintenance nightmare: a single failure could mean any of three unrelated rules regressed.

Pro Tip: When reviewing someone else's scenario, count the distinct business ideas it verifies. If you need more than one sentence to describe what it checks, it's probably two scenarios wearing one Scenario title.

Key Takeaways

  • And and But inherit the logical role of the step they follow; they aren't a fourth keyword type
  • But signals contrast — commonly used before a negative or exceptional condition
  • Every scenario should verify exactly one business-meaningful check, even if it has multiple Then/And lines
  • This discipline is what keeps a large scenario suite readable and debuggable as it grows

Quiz

What logical role does an And step have when it follows a Given?

When is But typically preferred over And?

What does the "one scenario, one business check" rule require?

Why does mixing multiple business rules into one scenario cause problems as a suite grows?