A test suite is a set of tests that run together, one for each thing that must keep working. The shop's checkout takes real money, so it deserves one. It is loaded for you, hidden, as app: POST /checkout takes a cart like {"items": [{"product_id": 1, "quantity": 2}]}, adds up the prices from the database, charges the card through the payment provider and answers 201 with {"status": "paid", "total": 24}.
A good suite tests the normal case and the edge cases, where bugs like to hide. Each test sends one request and checks the status code and the whole body. Here is one test of that shape, for a shop's product page:
def test_missing_product(self):
response = self.client.get("/products/99")
self.assertEqual(response.status_code, 404)
self.assertEqual(response.get_json(), {"error": "Product not found"})
Your suite sends three carts. A Mug (product 1) costs 12, a Pen (product 4) costs 2 and a Gift card (product 8) costs 50. The payment provider's test mode declines every payment over 1000. Work the expected answers out by hand, never with the code you are testing: a test that repeats the code's calculation repeats its bugs too.
POST /checkout 2 Mugs + 5 Pens → 201 {"status": "paid", "total": 34}
POST /checkout {"items": []} → 400 {"error": "The cart is empty"}
POST /checkout 25 Gift cards → 402 {"error": "Payment declined"}
When you run the code, the check runs your suite on the checkout, where all three tests must pass (3 tests: OK). Then it runs it on three broken copies of the checkout, and each copy must fail at least one test.
Your task: write the class CheckoutTests, a unittest.TestCase, with a setUp that stores app.test_client() in self.client, and three tests: test_checkout_succeeds, test_empty_cart and test_declined_card. Each one posts its cart to /checkout and checks the status code and the JSON body.
Assignment
Follow the Coding Tutorial to write a test suite for the checkout: a paid order, an empty cart and a declined card.
Hint
Every test has the same shape: post the cart with self.client.post("/checkout", json=cart), then one self.assertEqual for response.status_code and one for response.get_json(). Work out each expected total on paper first.
Free plan: 30 tutor messages. PRO is unlimited.
