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

single

Challenge: The Cart Quality Assurance

Svep för att visa menyn

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.
Uppgift

Svep för att börja koda

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 desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 5. Kapitel 7
single

single

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

some-alt