From 59cf24feb1cd7e611f94e1d001bed219df6f223e Mon Sep 17 00:00:00 2001 From: Woose Date: Mon, 27 Jan 2025 01:43:04 +0300 Subject: [PATCH] update --- module5.ipynb | 177 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 175 insertions(+), 2 deletions(-) diff --git a/module5.ipynb b/module5.ipynb index d395713..1d63e67 100644 --- a/module5.ipynb +++ b/module5.ipynb @@ -7,17 +7,190 @@ "# Simple Tests" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Unit Tests" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "**Concepts**\n", + "\n", + "Unittest relies on the following concepts:\n", + "\n", + "- **Test fixture:** This refers to preparing to perform one or more tests. In addition, test fixtures also include any actions involved in testing cleanup. This could involve creating temporary or proxy databases, directories, or starting a server process.\n", + "- **Test case:** This is the individual unit of testing that looks for a specific response to a set of inputs. If needed, TestCase is a base class provided by unittest and can be used to create new test cases.\n", + "- **Test suite:** This is a collection of test cases, test suites, or a combination of both. It is used to compile tests that should be executed together.\n", + "- **Test runner:** This runs the test and provides developers with the outcome’s data. The test runner can use different interfaces, like graphical or textual, to provide the developer with the test results. It can also provide a special value to developers to communicate the test results. " + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(['flour', 'sugar', 'eggs', 'cocoa', 'sprinkles', 'cherries'], 14)" + ] + }, + "execution_count": 1, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "from typing import List\n", + "\n", + "\n", + "class CakeFactory:\n", + " def __init__(self, cake_type: str, size: str):\n", + " self.cake_type = cake_type\n", + " self.size = size\n", + " self.toppings = []\n", + "\n", + " # Price based on cake type and size\n", + " self.price = 10 if self.cake_type == \"chocolate\" else 8\n", + " self.price += 2 if self.size == \"medium\" else 4 if self.size == \"large\" else 0\n", + "\n", + " def add_topping(self, topping: str):\n", + " self.toppings.append(topping)\n", + " # Adding 1 to the price for each topping\n", + " self.price += 1\n", + "\n", + " def check_ingredients(self) -> List[str]:\n", + " ingredients = ['flour', 'sugar', 'eggs']\n", + " ingredients.append('cocoa') if self.cake_type == \"chocolate\" else ingredients.append('vanilla extract')\n", + " ingredients += self.toppings\n", + " return ingredients\n", + "\n", + " def check_price(self) -> float:\n", + " return self.price\n", + "\n", + "# Example of creating a cake and adding toppings\n", + "cake = CakeFactory(\"chocolate\", \"medium\")\n", + "cake.add_topping(\"sprinkles\")\n", + "cake.add_topping(\"cherries\")\n", + "cake_ingredients = cake.check_ingredients()\n", + "cake_price = cake.check_price()\n", + "\n", + "\n", + "cake_ingredients, cake_price" + ] + }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], - "source": [] + "source": [ + "import unittest\n", + "\n", + "class TestCakeFactory(unittest.TestCase):\n", + " def test_create_cake(self):\n", + " cake = CakeFactory(\"vanilla\", \"small\")\n", + " self.assertEqual(cake.cake_type, \"vanilla\")\n", + " self.assertEqual(cake.size, \"small\")\n", + " self.assertEqual(cake.price, 8) # Vanilla cake, small size\n", + "\n", + " def test_add_topping(self):\n", + " cake = CakeFactory(\"chocolate\", \"large\")\n", + " cake.add_topping(\"sprinkles\")\n", + " self.assertIn(\"sprinkles\", cake.toppings)\n", + "\n", + " def test_check_ingredients(self):\n", + " cake = CakeFactory(\"chocolate\", \"medium\")\n", + " cake.add_topping(\"cherries\")\n", + " ingredients = cake.check_ingredients()\n", + " self.assertIn(\"cocoa\", ingredients)\n", + " self.assertIn(\"cherries\", ingredients)\n", + " self.assertNotIn(\"vanilla extract\", ingredients)\n", + "\n", + " def test_check_price(self):\n", + " cake = CakeFactory(\"vanilla\", \"large\")\n", + " cake.add_topping(\"sprinkles\")\n", + " cake.add_topping(\"cherries\")\n", + " price = cake.check_price()\n", + " self.assertEqual(price, 13) # Vanilla cake, large size + 2 toppings\n", + "\n", + "\n", + "# Running the unittests\n", + "unittest.TextTestRunner().run(unittest.TestLoader().loadTestsFromTestCase(TestCakeFactory))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## pytest" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "import pytest\n", + "class Fruit:\n", + " def __init__(self, name):\n", + " self.name = name\n", + " self.cubed = False\n", + "\n", + "\n", + " def cube(self):\n", + " self.cubed = True\n", + "\n", + "\n", + "class FruitSalad:\n", + " def __init__(self, *fruit_bowl):\n", + " self.fruit = fruit_bowl\n", + " self._cube_fruit()\n", + "\n", + "\n", + " def _cube_fruit(self):\n", + " for fruit in self.fruit:\n", + " fruit.cube()\n", + "\n", + "\n", + "# Arrange\n", + "@pytest.fixture\n", + "def fruit_bowl():\n", + " return [Fruit(\"apple\"), Fruit(\"banana\")]\n", + "\n", + "\n", + "def test_fruit_salad(fruit_bowl):\n", + " # Act\n", + " fruit_salad = FruitSalad(*fruit_bowl)\n", + "\n", + "\n", + " # Assert\n", + " assert all(fruit.cubed for fruit in fruit_salad.fruit)" + ] } ], "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, "language_info": { - "name": "python" + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.12.3" } }, "nbformat": 4,