├── README.md ├── item_database.py ├── shopping_cart.py └── test_shopping_cart.py /README.md: -------------------------------------------------------------------------------- 1 | # Simple Pytest Example 2 | 3 | This is the code for my video tutorial for getting started with Pytest. 4 | 5 | * [Pytest Documentation](https://docs.pytest.org/en/6.2.x/getting-started.html#getstarted) 6 | * [unittest.mock Documentation](https://docs.python.org/3/library/unittest.mock.html) 7 | 8 | ### Install Pytest 9 | 10 | ```bash 11 | pip install pytest 12 | ``` 13 | 14 | ### Run the tests 15 | 16 | ```bash 17 | pytest 18 | ``` 19 | 20 | ### Run a specific file 21 | 22 | ```bash 23 | pytest test_shopping_cart.py 24 | ``` 25 | 26 | ### Run a specific test 27 | 28 | ```bash 29 | pytest test_shopping_cart.py::test_can_get_total_price 30 | ``` 31 | -------------------------------------------------------------------------------- /item_database.py: -------------------------------------------------------------------------------- 1 | class ItemDatabase: 2 | def __init__(self) -> None: 3 | pass 4 | 5 | def get(self, item: str) -> float: 6 | pass 7 | -------------------------------------------------------------------------------- /shopping_cart.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | 4 | class ShoppingCart: 5 | def __init__(self, max_size: int) -> None: 6 | self.items: List[str] = [] 7 | self.max_size = max_size 8 | 9 | def add(self, item: str): 10 | if self.size() == self.max_size: 11 | raise OverflowError("cannot add more items") 12 | self.items.append(item) 13 | 14 | def size(self) -> int: 15 | return len(self.items) 16 | 17 | def get_items(self) -> List[str]: 18 | return self.items 19 | 20 | def get_total_price(self, price_map): 21 | total_price = 0 22 | for item in self.items: 23 | total_price += price_map.get(item) 24 | return total_price 25 | -------------------------------------------------------------------------------- /test_shopping_cart.py: -------------------------------------------------------------------------------- 1 | from unittest.mock import Mock 2 | from item_database import ItemDatabase 3 | from shopping_cart import ShoppingCart 4 | import pytest 5 | 6 | 7 | @pytest.fixture 8 | def cart(): 9 | # All setup for the cart here... 10 | return ShoppingCart(5) 11 | 12 | 13 | def test_can_add_item_to_cart(cart): 14 | cart.add("apple") 15 | assert cart.size() == 1 16 | 17 | 18 | def test_when_item_added_then_cart_contains_item(cart): 19 | cart.add("apple") 20 | assert "apple" in cart.get_items() 21 | 22 | 23 | def test_when_add_more_than_max_items_should_fail(cart): 24 | for _ in range(5): 25 | cart.add("apple") 26 | 27 | with pytest.raises(OverflowError): 28 | cart.add("apple") 29 | 30 | 31 | def test_can_get_total_price(cart): 32 | cart.add("apple") 33 | cart.add("orange") 34 | item_database = ItemDatabase() 35 | 36 | def mock_get_item(item: str): 37 | if item == "apple": 38 | return 1.0 39 | if item == "orange": 40 | return 2.0 41 | 42 | item_database.get = Mock(side_effect=mock_get_item) 43 | assert cart.get_total_price(item_database) == 3.0 44 | --------------------------------------------------------------------------------