From 010e1898a3a8bb65ab8b0a118472b1bfccc422a9 Mon Sep 17 00:00:00 2001 From: Konstantin Zhulev Date: Wed, 20 May 2026 22:22:32 +0300 Subject: [PATCH 1/2] add unit tests for stellar burgers --- .gitignore | 9 ++ README.md | 53 ++++++++--- praktikum.py => main.py | 0 __init__.py => praktikum/__init__.py | 0 bun.py => praktikum/bun.py | 0 burger.py => praktikum/burger.py | 0 database.py => praktikum/database.py | 2 +- ingredient.py => praktikum/ingredient.py | 0 .../ingredient_types.py | 3 +- requirements.txt | Bin 0 -> 66 bytes tests/test_bun.py | 16 ++++ tests/test_burger.py | 87 ++++++++++++++++++ tests/test_database.py | 61 ++++++++++++ tests/test_ingredient.py | 18 ++++ 14 files changed, 234 insertions(+), 15 deletions(-) create mode 100644 .gitignore rename praktikum.py => main.py (100%) rename __init__.py => praktikum/__init__.py (100%) rename bun.py => praktikum/bun.py (100%) rename burger.py => praktikum/burger.py (100%) rename database.py => praktikum/database.py (97%) rename ingredient.py => praktikum/ingredient.py (100%) rename ingredient_types.py => praktikum/ingredient_types.py (80%) create mode 100644 requirements.txt create mode 100644 tests/test_bun.py create mode 100644 tests/test_burger.py create mode 100644 tests/test_database.py create mode 100644 tests/test_ingredient.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 000000000..8a366f7f5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,9 @@ +__pycache__/ +.pytest_cache/ +htmlcov/ +.coverage +.venv/ +venv/ +.idea/ +.vscode/ +*.pyc \ No newline at end of file diff --git a/README.md b/README.md index 272081708..20faa5dbc 100644 --- a/README.md +++ b/README.md @@ -1,24 +1,51 @@ -## Задание 1: Юнит-тесты +# Задание 1: Юнит-тесты -### Автотесты для проверки программы, которая помогает заказать бургер в Stellar Burgers +## Автотесты для проверки программы, которая помогает заказать бургер в Stellar Burgers -### Реализованные сценарии +## Реализованные сценарии -Созданы юнит-тесты, покрывающие классы `Bun`, `Burger`, `Ingredient`, `Database` +Созданы юнит-тесты для классов `Bun`, `Burger`, `Ingredient`, `Database`. -Процент покрытия 100% (отчет: `htmlcov/index.html`) +В тестах используются: +- параметризация; +- моки; +- `pytest`; +- `pytest-cov`. -### Структура проекта +Покрытие проекта — 100%. -- `praktikum` - пакет, содержащий код программы -- `tests` - пакет, содержащий тесты, разделенные по классам. Например, `bun_test.py`, `burger_test.py` и т.д. +HTML-отчёт о покрытии формируется в папке `htmlcov`. -### Запуск автотестов +## Структура проекта -**Установка зависимостей** +- `praktikum` — пакет с кодом приложения; +- `tests` — пакет с юнит-тестами; +- `main.py` — файл запуска программы; +- `requirements.txt` — зависимости проекта; +- `.gitignore` — список игнорируемых файлов и папок. -> `$ pip install -r requirements.txt` +## Установка зависимостей -**Запуск автотестов и создание HTML-отчета о покрытии** +```bash +pip install -r requirements.txt +``` -> `$ pytest --cov=praktikum --cov-report=html` +## Запуск тестов + +```bash +python -m pytest -v +``` + +## Запуск тестов с отчётом покрытия + +```bash +python -m pytest --cov=praktikum --cov-report=term-missing --cov-report=html +``` + +## Отчёт покрытия + +После запуска команды с `pytest-cov` HTML-отчёт будет доступен в файле: + +```bash +htmlcov/index.html +``` \ No newline at end of file diff --git a/praktikum.py b/main.py similarity index 100% rename from praktikum.py rename to main.py diff --git a/__init__.py b/praktikum/__init__.py similarity index 100% rename from __init__.py rename to praktikum/__init__.py diff --git a/bun.py b/praktikum/bun.py similarity index 100% rename from bun.py rename to praktikum/bun.py diff --git a/burger.py b/praktikum/burger.py similarity index 100% rename from burger.py rename to praktikum/burger.py diff --git a/database.py b/praktikum/database.py similarity index 97% rename from database.py rename to praktikum/database.py index 4c75baf71..a202dcc79 100644 --- a/database.py +++ b/praktikum/database.py @@ -30,4 +30,4 @@ def available_buns(self) -> List[Bun]: return self.buns def available_ingredients(self) -> List[Ingredient]: - return self.ingredients + return self.ingredients \ No newline at end of file diff --git a/ingredient.py b/praktikum/ingredient.py similarity index 100% rename from ingredient.py rename to praktikum/ingredient.py diff --git a/ingredient_types.py b/praktikum/ingredient_types.py similarity index 80% rename from ingredient_types.py rename to praktikum/ingredient_types.py index 34940ad5d..d454c885d 100644 --- a/ingredient_types.py +++ b/praktikum/ingredient_types.py @@ -3,5 +3,6 @@ SAUCE – соус FILLING – начинка """ + INGREDIENT_TYPE_SAUCE = 'SAUCE' -INGREDIENT_TYPE_FILLING = 'FILLING' +INGREDIENT_TYPE_FILLING = 'FILLING' \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..5768c1869dd32d2c7720cbd17e12ad210e5cfa38 GIT binary patch literal 66 zcmezWuYjSFp@boop%_To0-+^?9)kf88!_-Qa4{gu>M|rV Date: Wed, 27 May 2026 21:30:44 +0300 Subject: [PATCH 2/2] Fix unit tests structure for Task_1 --- README.md | 27 +++++++++++---------------- tests/test_bun.py | 10 +++++++++- tests/test_database.py | 40 ++++++++++++++++++++++++++++++++++++++-- tests/test_ingredient.py | 18 +++++++++++++++++- 4 files changed, 75 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 20faa5dbc..ddef3aaa7 100644 --- a/README.md +++ b/README.md @@ -2,27 +2,22 @@ ## Автотесты для проверки программы, которая помогает заказать бургер в Stellar Burgers -## Реализованные сценарии +Проект содержит юнит-тесты для классов `Bun`, `Burger`, `Ingredient`, `Database`. -Созданы юнит-тесты для классов `Bun`, `Burger`, `Ingredient`, `Database`. +## Что используется в тестах -В тестах используются: -- параметризация; -- моки; -- `pytest`; -- `pytest-cov`. - -Покрытие проекта — 100%. - -HTML-отчёт о покрытии формируется в папке `htmlcov`. +- `pytest` +- `pytest-cov` +- параметризация +- моки ## Структура проекта -- `praktikum` — пакет с кодом приложения; -- `tests` — пакет с юнит-тестами; -- `main.py` — файл запуска программы; -- `requirements.txt` — зависимости проекта; -- `.gitignore` — список игнорируемых файлов и папок. +- `praktikum` — пакет с кодом приложения +- `tests` — пакет с юнит-тестами +- `main.py` — файл запуска программы +- `requirements.txt` — зависимости проекта +- `.gitignore` — список игнорируемых файлов и папок ## Установка зависимостей diff --git a/tests/test_bun.py b/tests/test_bun.py index a1ded04d5..27edea436 100644 --- a/tests/test_bun.py +++ b/tests/test_bun.py @@ -9,8 +9,16 @@ class TestBun: ("black bun", 100), ("white bun", 200.5) ]) - def test_getters_return_correct_bun_data(self, name, price): + def test_get_name_returns_correct_name(self, name, price): bun = Bun(name, price) assert bun.get_name() == name + + @pytest.mark.parametrize("name, price", [ + ("black bun", 100), + ("white bun", 200.5) + ]) + def test_get_price_returns_correct_price(self, name, price): + bun = Bun(name, price) + assert bun.get_price() == price \ No newline at end of file diff --git a/tests/test_database.py b/tests/test_database.py index 8f59a087f..fd8d523a6 100644 --- a/tests/test_database.py +++ b/tests/test_database.py @@ -29,11 +29,17 @@ def test_available_buns_returns_three_buns(self): assert len(buns) == 3 @pytest.mark.parametrize("index, expected_name, expected_price", EXPECTED_BUNS) - def test_available_buns_returns_correct_buns(self, index, expected_name, expected_price): + def test_available_buns_returns_buns_with_correct_name(self, index, expected_name, expected_price): database = Database() bun = database.available_buns()[index] assert bun.get_name() == expected_name + + @pytest.mark.parametrize("index, expected_name, expected_price", EXPECTED_BUNS) + def test_available_buns_returns_buns_with_correct_price(self, index, expected_name, expected_price): + database = Database() + bun = database.available_buns()[index] + assert bun.get_price() == expected_price def test_available_ingredients_returns_six_ingredients(self): @@ -46,7 +52,7 @@ def test_available_ingredients_returns_six_ingredients(self): "index, expected_type, expected_name, expected_price", EXPECTED_INGREDIENTS ) - def test_available_ingredients_returns_correct_ingredients( + def test_available_ingredients_returns_ingredients_with_correct_type( self, index, expected_type, @@ -57,5 +63,35 @@ def test_available_ingredients_returns_correct_ingredients( ingredient = database.available_ingredients()[index] assert ingredient.get_type() == expected_type + + @pytest.mark.parametrize( + "index, expected_type, expected_name, expected_price", + EXPECTED_INGREDIENTS + ) + def test_available_ingredients_returns_ingredients_with_correct_name( + self, + index, + expected_type, + expected_name, + expected_price + ): + database = Database() + ingredient = database.available_ingredients()[index] + assert ingredient.get_name() == expected_name + + @pytest.mark.parametrize( + "index, expected_type, expected_name, expected_price", + EXPECTED_INGREDIENTS + ) + def test_available_ingredients_returns_ingredients_with_correct_price( + self, + index, + expected_type, + expected_name, + expected_price + ): + database = Database() + ingredient = database.available_ingredients()[index] + assert ingredient.get_price() == expected_price \ No newline at end of file diff --git a/tests/test_ingredient.py b/tests/test_ingredient.py index 4e80cb49b..e9ef1598e 100644 --- a/tests/test_ingredient.py +++ b/tests/test_ingredient.py @@ -10,9 +10,25 @@ class TestIngredient: (INGREDIENT_TYPE_SAUCE, "hot sauce", 100), (INGREDIENT_TYPE_FILLING, "cutlet", 300) ]) - def test_getters_return_correct_ingredient_data(self, ingredient_type, name, price): + def test_get_type_returns_correct_type(self, ingredient_type, name, price): ingredient = Ingredient(ingredient_type, name, price) assert ingredient.get_type() == ingredient_type + + @pytest.mark.parametrize("ingredient_type, name, price", [ + (INGREDIENT_TYPE_SAUCE, "hot sauce", 100), + (INGREDIENT_TYPE_FILLING, "cutlet", 300) + ]) + def test_get_name_returns_correct_name(self, ingredient_type, name, price): + ingredient = Ingredient(ingredient_type, name, price) + assert ingredient.get_name() == name + + @pytest.mark.parametrize("ingredient_type, name, price", [ + (INGREDIENT_TYPE_SAUCE, "hot sauce", 100), + (INGREDIENT_TYPE_FILLING, "cutlet", 300) + ]) + def test_get_price_returns_correct_price(self, ingredient_type, name, price): + ingredient = Ingredient(ingredient_type, name, price) + assert ingredient.get_price() == price \ No newline at end of file