Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Challenge: The Cart Quality Assurance | Testing with unittest and pytest
Python Structural Programming
Seção 5. Capítulo 7
single

single

Challenge: The Cart Quality Assurance

Deslize para mostrar o 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.
Tarefa

Deslize para começar a programar

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.

Solução

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 5. Capítulo 7
single

single

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

some-alt