single
Challenge: The Cart Quality Assurance
Glissez pour afficher le menu
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
assertstatements to check if your code behaves as expected, such as verifying list length, item content, or method return values.
Glissez pour commencer à coder
Implement a ShoppingCart class and test it.
-
Implement the
add_item(name, price)method to add a dictionary with"name"and"price"toitems. -
Implement the
get_total()method to return the total price of all items in the cart. -
Write
test_add_itemto verify that after adding'Laptop'priced at1200.0, theitemslist has length 1 and the item's name is'Laptop'. -
Write
test_total_priceto verify that adding'Book'for20.0and'Coffee'for5.0results in a total of25.0. -
Write
test_empty_cart_totalto verify that an empty cart returns a total of0.
Solution
Merci pour vos commentaires !
single
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion