Skip to content
Tutorials Center
My learning

Topics

Lesson 19 of 60 ยท Test Design Techniques

Use Case and Scenario Testing

Learn use case and scenario testing: turn main, alternate and exception flows and user stories into end-to-end test scenarios, with a runnable checkout example.

Most of the techniques so far look at one rule at a time: a range, a table of conditions, a set of states. Real users do not work that way. They arrive with a goal ("buy this lamp", "reset my password") and move through a series of steps, and the product is only good if the whole journey works.

Use case testing derives tests from use cases: descriptions of how an actor reaches a goal with the system. Scenario testing is the broader idea of testing realistic, end-to-end stories, whether they come from formal use cases, user stories or interviews with customers. Both aim at the same target: the behaviour a user would actually notice, including what happens when things go wrong.

Anatomy of a use case

Goal What the actor wants to achieve.
Main flow The happy path, step by step.
Alternate flows Valid variations that still reach the goal.
Exception flows Things go wrong and the goal fails.

Preconditions say where you start; postconditions say what must be true at the end.

1 / 2

What a use case contains

A use case is a short, structured story. The classic parts are:

  • Actor: who is acting (a customer, an admin, another system).
  • Goal: what the actor wants ("buy the items in my cart").
  • Preconditions: what must be true before it starts (the cart has items; the customer is signed in).
  • Main flow (also called the basic or happy path): the numbered steps when everything goes right.
  • Alternate flows: variations that are still successes, such as paying with a coupon.
  • Exception flows: failures the system must handle, such as a declined card.
  • Postconditions: what is true at the end, for each outcome (an order exists and stock is reduced; or nothing changed).

For our examples, the use case is "Buy the items in the cart". The main flow has six steps:

  1. The customer reviews the cart.
  2. The customer optionally enters a coupon code.
  3. The customer enters card details.
  4. The system checks that every item is in stock.
  5. The system charges the card.
  6. The system confirms the order.

The branches attach to steps. A1 (valid coupon) and E3 (expired coupon) branch at step 2. E2 (not enough stock) branches at step 4. E1 (card declined) branches at step 5. E0 (empty cart) branches at step 1. A1 and E3 both return to the main flow; E0, E1 and E2 end the use case early.

flowchart TD
  S1["1 Review cart"] -->|"cart empty"| E0["E0 Reject, stop"]
  S1 --> S2["2 Enter coupon"]
  S2 -->|"valid coupon"| A1["A1 Apply discount"]
  S2 -->|"expired coupon"| E3["E3 Warn, full price"]
  S2 --> S3["3 Enter card"]
  A1 --> S3
  E3 --> S3
  S3 --> S4["4 Check stock"]
  S4 -->|"not enough"| E2["E2 Reject, stop"]
  S4 --> S5["5 Charge card"]
  S5 -->|"declined"| E1["E1 Reject, stop"]
  S5 --> S6["6 Confirm order"]

The checkout use case: one main flow, one alternate flow and four exception paths.

Turning flows into scenarios

A scenario is one concrete route through the use case, with specific data and an expected result. The minimum rule is: one scenario for the main flow, plus one for every alternate and exception flow. For checkout that is six scenarios.

The stand-in below holds the use case as data and prints every route as a path of step numbers. It also counts how many combinations exist if you test every coupon outcome with every ending, which shows why you choose deliberately.

JS Use case flows as paths

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Six scenarios cover every flow once. If you tried every coupon outcome with every ending you would need ten. That jump shows the usual trade-off: the minimum set is cheap and finds most flow bugs; combinations are added where risk is high (for example, "coupon plus declined card", to check that a discount is not left half-applied).

Running scenarios against a checkout

Real use case testing happens in the product through its UI or API, and the real shop cannot run on this site. The code below is a stand-in: a small checkout function that mimics the system, plus a table of scenarios. The scenarios check what the postconditions promise: the status, the total, and the stock afterwards.

JS Scenarios run against a mock checkout

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice what each scenario asserts. S4 does not stop at "the message says declined". It also checks that the stock is untouched, because the postcondition of a failed payment is "nothing changed".

Why the end state matters

A classic defect hides here: the payment is declined and the user sees the right error, but the code had already reduced the stock. Over a day, a few declined cards make popular items look sold out. A test that only reads the message passes; a scenario that also checks the postcondition catches it.

The next stand-in plants that bug and runs the same scenario list against it.

JS A scenario catches a planted end-state bug

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Both versions pass the message check. Only the end-state check separates them.

From user stories to scenarios

Agile teams often do not have formal use cases; they have user stories: "As a customer, I want to apply a coupon so that I pay less." The story comes with acceptance criteria, and each criterion can become one or more scenarios. A popular way to write them is Given-When-Then, which you met in reviewing requirements for testability:

  • Given the cart holds a lamp priced 30 and the coupon SAVE10 gives 10 percent off, when the customer applies the coupon and pays, then the total charged is 27.
  • Given the coupon EXPIRED5 is expired, when the customer applies it, then a warning is shown and the total stays at full price.

The exercise is the same as with use cases: list the happy path, then ask "what else can happen here?" for each step. The answers (expired coupon, two coupons, coupon on an empty cart) are the alternate and exception scenarios.

Tips for good scenarios

  • Write scenarios in the user's language. "Customer buys the last lamp while another customer is paying" is a story anyone on the team can review.
  • Keep each scenario independent. It should start from a known state so that a failure in one does not break the others.
  • Mix in realism. Real users abandon carts, go back, double click and lose connection. Add the odd route to the straight ones.
  • Prioritize. Run the main flow and the most frequent alternates on every build; run rarer exceptions less often. Risk-based testing explains how to rank them.
  • Know the limits. Use case testing finds integration and flow defects, but it is not good at hitting every boundary or every combination of rules. Pair it with equivalence partitioning and decision tables.

When a defect is found in production, write the scenario that would have caught it and add it to the regression pack. Over time your scenarios become a record of how the product really fails.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is the main flow of a use case?

    The main (basic) flow is the straightforward sequence of steps when the actor reaches the goal without problems.
  2. 2. A use case has a main flow, 2 alternate flows and 3 exception flows. What is the minimum number of scenarios?

    One scenario per flow: 1 main + 2 alternate + 3 exception = 6.
  3. 3. Which difference separates an alternate flow from an exception flow?

    An alternate flow is a valid variation that still achieves the goal (a coupon). An exception flow is a failure the system must handle (a declined card).
  4. 4. Why should a failed-payment scenario also check the stock level afterwards?

    Checking the end state against the postconditions catches defects hidden behind a correct-looking message.
  5. 5. How do acceptance criteria of a user story relate to scenarios?

    Acceptance criteria describe expected behaviour; written as Given-When-Then, each one maps to concrete test scenarios.

Summary

  • A use case describes an actor's goal with a main flow, alternate flows and exception flows, plus pre- and postconditions.
  • Test at least one scenario per flow; add combinations where the risk is high.
  • Assert the end state (data and side effects), not only the message on the screen.
  • User stories and Given-When-Then criteria turn into scenarios the same way: happy path first, then ask what else can happen.
  • Use case testing finds flow and integration defects; combine it with other techniques for boundaries and rules.

Continue with White-Box Testing and Code Coverage.