Skip to content
Tutorials Center
My learning

Topics

Lesson 54 of 60 ยท Real-World Projects

Project: Test a Shopping Cart

Test a shopping cart end to end: decision tables for discounts, boundary values for prices, state transitions and a regression pack that catches five planted bugs.

A shopping cart looks simple until you try to test it. There are prices that depend on totals, discounts that interact, quantity limits, and a status that decides what the customer may still do. It is a perfect project for combining the techniques you have learned, because each rule calls for a different tool.

In this project you test a small cart that has five bugs planted in it. Your job is to design tests that find every one of them, and then to keep those tests as a regression pack. The cart is a stand-in: a real shop is a website, a database and a payment provider, while ours is about 40 lines of JavaScript that follow the same business rules. The rules, the techniques and the thinking are the real part. If you have not yet done Project: Test a Login Form, it makes a good warm-up.

The project in four steps

Read the rules Turn the requirements into testable statements.
Pick techniques One technique per kind of rule.
Run the pack Find the planted bugs.
Keep it The same tests become your regression pack.
1 / 2

The requirements

Every test starts from a requirement. Here are the rules of our cart. They are short on purpose, and you will see that even six rules produce dozens of tests.

ID Rule
R1 A product line holds 1 to 10 units. Adding more, or fewer than one, is rejected. Adding the same product again adds to the same line.
R2 An order whose subtotal is $100.00 or more gets 10% off.
R3 A member gets a further 5% off. The two percentages are added together (so a member's large order gets 15%).
R4 The coupon SAVE5 takes $5.00 off after the percentage discount. The goods total never goes below $0.00.
R5 Shipping is $5.99 unless the goods total (after all discounts) is $50.00 or more, in which case it is free.
R6 A cart moves from Empty to Active when something is added, from Active to CheckedOut on checkout, and from CheckedOut to Paid on payment. A checked-out cart cannot be changed.

Notice how the rules sort themselves. R2 to R4 combine several conditions (a decision table), R1 and the thresholds in R2 and R5 are limits (boundary values), and R6 is a lifecycle (state transitions). If you can name the technique from the wording of the rule, you are already thinking like a tester. The techniques are covered in Decision Table Testing, Boundary Value Analysis and State Transition Testing.

Step 1: meet the cart

Before designing tests, spend a few minutes using the thing. This is exploratory testing in miniature: you learn the vocabulary, the states and the obvious behaviour, and you often get your first suspicions. The editor below builds a cart and prices a few orders. The cart keeps money in cents (so $12.00 is 1200) to avoid decimal rounding problems, which is a common choice in real systems.

JS Meet the cart (the correct build)

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

No output.

Even this short session raises questions a tester would write down: is a discount applied to the shipping fee? (The rules say no.) What happens at exactly $100.00? Can I still add an item after checkout? Each question becomes a test.

Step 2: a decision table for the discounts

The total depends on three yes/no conditions: is this a large order, is the customer a member, is a coupon used? Three conditions give 2 x 2 x 2 = 8 rules. A decision table lists them all, so no combination is forgotten.

Rule Large order Member Coupon Expected total
DT1 yes no no $108.00
DT2 yes no yes $103.00
DT3 yes yes no $102.00
DT4 yes yes yes $97.00
DT5 no no no $45.99
DT6 no no yes $40.99
DT7 no yes no $43.99
DT8 no yes yes $38.99

Look at how the expected values were worked out. A "large" order is $120.00 and a "small" one is $40.00. For DT3: 15% off $120.00 is $18.00, leaving $102.00, which is above $50.00, so shipping is free. For DT7: 5% off $40.00 is $2.00, leaving $38.00, plus $5.99 shipping gives $43.99. The expected results come from the requirements and a calculator, never from running the code under test. If you copy the program's output into the "expected" column you are testing that the program agrees with itself.

JS Run the decision table against two builds

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

No output.

The second run finds the first planted bug: when a member makes a large order, the member's extra 5% is silently dropped. Only rules DT3 and DT4 combine "large" and "member", so a tester who checked "members get a discount" and "large orders get a discount" separately would have passed both and shipped the bug. That is the main argument for decision tables: bugs hide in combinations.

Step 3: boundaries for prices and quantities

Three numbers in the requirements are thresholds: $100.00 (R2), $50.00 (R5) and the 1 to 10 quantity range (R1). For each you test the value just below, on, and just above the boundary, as in boundary value analysis. Cart rules also have a boundary in a less obvious place: the coupon (R4) can push the goods total across the $50.00 line, and it can push it below zero. Both deserve a test.

Notice an interesting consequence of R2: an order of $99.99 pays $99.99, while an order of $100.00 pays only $90.00. A customer could save money by buying more. That is allowed by the rules, but a tester should mention it to the product owner, because it is probably surprising to the business.

JS Boundary tests on three planted bugs

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

No output.

Three more bugs are caught here. The threshold build uses "greater than $100.00" where the rule says "$100.00 or more", the classic off-by-one, and only test PB2 (exactly $100.00) notices. The negative build lets a $5.00 coupon turn a $3.00 order into a negative goods total, so the customer pays $3.99 for the order and shipping together, less than the shipping fee alone. The qty build accepts 11 units, caught by the boundary test and by the "6 + 5" test that adds to an existing line. That last test is easy to forget: a limit applies to the line, not to a single click.

Step 4: state transitions for the cart status

R6 describes a lifecycle. Four states and four events (add, remove, checkout, pay) give a table of 16 cells. Only five are valid; the other eleven must be rejected and leave the state unchanged.

State add remove checkout pay
Empty Active rejected rejected rejected
Active Active Empty (last line) CheckedOut rejected
CheckedOut rejected rejected rejected Paid
Paid rejected rejected rejected rejected

One more test covers the "(last line)" guard: removing one of two lines must leave the cart Active. The test below sets up a cart in each state, fires each event and checks both the answer and the resulting state.

JS Walk the 4 x 4 state table

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

No output.

The lock build only blocks changes once the cart is Paid. A cart that is merely CheckedOut still accepts new items, and worse, it jumps back to Active, so the customer could add a product after the price was confirmed. Happy-path tests would never find this: checkout works, payment works. Only a test that tries a forbidden event in a quiet state does.

Step 5: the regression pack

You now own 8 + 8 + 8 + 17 = 41 tests: eight decision rules, eight price boundaries, eight quantity cases and seventeen state checks. Together they form a regression pack: a set you rerun after every change to prove that old behaviour still works. After a bug is fixed you do not run only the test that failed. You run the whole pack, because fixes break neighbours.

A good pack should fail on every buggy build and pass on the correct one. The stand-in below checks that: it runs the whole pack against the correct build and against five builds with exactly one planted bug each, then prints which tests fail for each. This is the idea behind mutation testing: if you deliberately break the program and your tests do not notice, your tests have a gap.

JS Run the full pack against every build

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

No output.

Read the output as a coverage report for your tests. Every bug is caught, and by only one or two tests, which tells you how precise the boundary and state tests need to be. The decision table, the star of step 2, does not catch the threshold bug at all, because its orders ($120 and $40) sit far from $100. Techniques complement each other: that is why you use several.

Reporting what you found

A found bug is only useful once it is reported clearly. Here is the threshold bug in the format from Writing Excellent Bug Reports.

TEXT
Title:        Order of exactly $100.00 gets no 10% discount
Environment:  Cart stand-in, build with the "threshold" switch
Steps:        1. Add products so the subtotal is $100.00
              2. Look at the price summary
Expected:     10% discount; total $90.00 (requirement R2: "$100.00 or more")
Actual:       No discount; total $100.00
Severity:     Major (customers are overcharged)
Priority:     High (affects every order at the threshold)
Evidence:     Test PB2; PB1 (99.99) and PB3 (100.01) behave correctly

The evidence line matters. It tells the developer the exact boundary where behaviour changes, which usually points straight at the faulty comparison.

When a pack finds a bug, ask whether a cheaper test could have found it earlier. A > instead of >= in a price function is the perfect target for a unit test, where it would fail in milliseconds. System testing is the wrong place to discover it, but a good tester still writes it down and suggests the unit test. See Test Levels.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which technique fits the rule 'a member gets 5% extra, a large order gets 10%, a coupon takes off $5'?

    Several conditions that combine to decide the result are the textbook case for a decision table: three yes/no conditions give eight rules.
  2. 2. Why must expected totals be worked out from the requirements and not copied from the program's output?

    An expected result is an independent statement of what is right. If it comes from the code under test, the test cannot fail when the code is wrong.
  3. 3. The rule says '$100.00 or more'. Which test finds a bug where the code uses 'greater than $100.00'?

    Only the value exactly on the boundary distinguishes >= from >. Values far from the boundary behave the same in both versions.
  4. 4. In the state table, a customer adds a product to a CheckedOut cart. What is the correct result?

    R6 says a checked-out cart cannot be changed. A correct system rejects the event and does not change the state.
  5. 5. What is the main purpose of a regression pack?

    A regression pack is a reusable set of tests rerun after changes, because a fix to one place can break another.

Summary

  • Sort the requirements by shape: combinations of conditions, thresholds, and lifecycles map to decision tables, boundary values and state transitions.
  • Derive expected results from the requirements, never from the program under test.
  • Bugs hide in combinations (member plus large order), on boundaries (exactly $100.00) and in forbidden transitions (adding after checkout).
  • Test the invalid cells of a state table and check that the state is unchanged.
  • Keep every test as a regression pack and rerun all of it after each fix; planting bugs shows whether your pack really catches them.

Continue with Project: Plan and Report a Release.