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

single

Challenge: The Cart Quality Assurance

Pyyhkäise näyttääksesi valikon

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.
Tehtävä

Pyyhkäise aloittaaksesi koodauksen

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.

Ratkaisu

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 5. Luku 7
single

single

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

some-alt