Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge: The Cart Quality Assurance | Testing with unittest and pytest
Python Structural Programming
Sektion 5. Kapitel 7
single

single

Challenge: The Cart Quality Assurance

Stryg for at vise menuen

Key Points to Remember

  • Test isolation: each test should run independently, so changes in one test do not affect others;
  • Fixtures in pytest: fixtures provide fresh instances or setup for each test, ensuring test isolation;
  • Class design: define classes with attributes and methods to organize behavior and state;
  • Assertions in tests: use assert statements to check if your code behaves as expected, such as verifying list length, item content, or method return values.
Opgave

Swipe to start coding

Implement a ShoppingCart class and test it.

  • Implement the add_item(name, price) method to add a dictionary with "name" and "price" to items.

  • Implement the get_total() method to return the total price of all items in the cart.

  • Write test_add_item to verify that after adding 'Laptop' priced at 1200.0, the items list has length 1 and the item's name is 'Laptop'.

  • Write test_total_price to verify that adding 'Book' for 20.0 and 'Coffee' for 5.0 results in a total of 25.0.

  • Write test_empty_cart_total to verify that an empty cart returns a total of 0.

Løsning

Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 5. Kapitel 7
single

single

Spørg AI

expand

Spørg AI

ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

some-alt