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

single

Challenge: The Cart Quality Assurance

Scorri per mostrare il 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 assert statements to check if your code behaves as expected, such as verifying list length, item content, or method return values.
Compito

Scorri per iniziare a programmare

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.

Soluzione

Switch to desktopCambia al desktop per esercitarti nel mondo realeContinua da dove ti trovi utilizzando una delle opzioni seguenti
Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 5. Capitolo 7
single

single

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

some-alt