{"id":3643,"date":"2022-07-04T04:56:30","date_gmt":"2022-07-04T04:56:30","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=3643"},"modified":"2022-07-09T07:26:18","modified_gmt":"2022-07-09T07:26:18","slug":"python-run-unittest","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-unit-testing\/python-run-unittest\/","title":{"rendered":"Organizing Code &#038; Running unittest"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to organize the test code and how to use the various commands to run unit tests.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='organizing-code'>Organizing code <a href=\"#organizing-code\" class=\"anchor\" id=\"organizing-code\" title=\"Anchor for Organizing code\">#<\/a><\/h2>\n\n\n\n<p>If you have a few <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-module\/\">modules<\/a>, you can create test modules and place them within the same directory. <\/p>\n\n\n\n<p>In practice, you may have many modules organized into <a href=\"https:\/\/www.pythontutorial.net\/python-basics\/python-packages\/\">packages<\/a>. Therefore, it&#8217;s important to keep the development code and test code more organized.<\/p>\n\n\n\n<p>It&#8217;s a good practice to keep the development code and the test code in separate directories. And you should place the test code in a directory called <code>test<\/code> to make it obvious.<\/p>\n\n\n\n<p>For demonstration purposes, we&#8217;ll create a sample project with the following structure:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">D:\\python-unit-testing\n\u251c\u2500\u2500 shapes\n|  \u251c\u2500\u2500 circle.py\n|  \u251c\u2500\u2500 shape.py\n|  \u2514\u2500\u2500 square.py\n\u2514\u2500\u2500 test\n   \u251c\u2500\u2500 test_circle.py\n   \u251c\u2500\u2500 test_square.py\n   \u2514\u2500\u2500 __init__.py<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>First, create the <code>shapes<\/code> and <code>test<\/code> directories in the project folder (<code>python-unit-testing<\/code>).<\/p>\n\n\n\n<p>Second, create three modules <code>shape.py<\/code>, <code>circle.py<\/code>, and <code>square.py<\/code> modules and place them in the <code>shapes<\/code> directory.<\/p>\n\n\n\n<p>shape.py<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> abc <span class=\"hljs-keyword\">import<\/span> ABC, abstractmethod\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Shape<\/span><span class=\"hljs-params\">(ABC)<\/span>:<\/span>\n<span class=\"hljs-meta\">    @abstractmethod<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">area<\/span><span class=\"hljs-params\">()<\/span> -&gt; float:<\/span>\n        <span class=\"hljs-keyword\">pass<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>Shape<\/code> class is an <a href=\"https:\/\/www.pythontutorial.net\/python-oop\/python-abstract-class\/\">abstract class<\/a> that has the <code>area()<\/code> method. It is the base class of the <code>Circle<\/code> and <code>Square<\/code> class.<\/p>\n\n\n\n<p>circle.py<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> math\n<span class=\"hljs-keyword\">from<\/span> .shape <span class=\"hljs-keyword\">import<\/span> Shape\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Circle<\/span><span class=\"hljs-params\">(Shape)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, radius: float)<\/span> -&gt; <span class=\"hljs-keyword\">None<\/span>:<\/span>\n        <span class=\"hljs-keyword\">if<\/span> radius &lt; <span class=\"hljs-number\">0<\/span>:\n            <span class=\"hljs-keyword\">raise<\/span> ValueError(<span class=\"hljs-string\">'The radius cannot be negative'<\/span>)\n\n        self._radius = radius\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">area<\/span><span class=\"hljs-params\">(self)<\/span> -&gt; float:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> math.pi * math.pow(self._radius, <span class=\"hljs-number\">2<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>Circle<\/code> class also inherits from the <code>Shape<\/code> class. It implements the <code>area()<\/code> method that returns the area of the circle. <\/p>\n\n\n\n<p>square.py<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> math\n<span class=\"hljs-keyword\">from<\/span> .shape <span class=\"hljs-keyword\">import<\/span> Shape\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">Square<\/span><span class=\"hljs-params\">(Shape)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">__init__<\/span><span class=\"hljs-params\">(self, length: float)<\/span> -&gt; <span class=\"hljs-keyword\">None<\/span>:<\/span>\n        <span class=\"hljs-keyword\">if<\/span> length &lt; <span class=\"hljs-number\">0<\/span>:\n            <span class=\"hljs-keyword\">raise<\/span> ValueError(<span class=\"hljs-string\">'The length cannot be negative'<\/span>)\n        self._length = length\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">area<\/span><span class=\"hljs-params\">(self)<\/span> -&gt; float:<\/span>\n        <span class=\"hljs-keyword\">return<\/span> math.pow(self._length, <span class=\"hljs-number\">2<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Like the <code>Circle<\/code> class, the <code>Square<\/code> class has the <code>area()<\/code> method that returns the area of the square.<\/p>\n\n\n\n<p>Third, create the <code>test_circle.py<\/code> and <code>test_square.py<\/code> test modules and place them in the <code>test<\/code> folder:<\/p>\n\n\n\n<p>test_circle.py<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> unittest\n<span class=\"hljs-keyword\">import<\/span> math\n\n<span class=\"hljs-keyword\">from<\/span> shapes.circle <span class=\"hljs-keyword\">import<\/span> Circle\n<span class=\"hljs-keyword\">from<\/span> shapes.shape <span class=\"hljs-keyword\">import<\/span> Shape\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">TestCircle<\/span><span class=\"hljs-params\">(unittest.TestCase)<\/span>:<\/span>\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_circle_instance_of_shape<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        circle = Circle(<span class=\"hljs-number\">10<\/span>)\n        self.assertIsInstance(circle, Shape)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_create_circle_negative_radius<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">with<\/span> self.assertRaises(ValueError):\n            circle = Circle(<span class=\"hljs-number\">-1<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_area<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        circle = Circle(<span class=\"hljs-number\">2.5<\/span>)\n        self.assertAlmostEqual(circle.area(), math.pi * <span class=\"hljs-number\">2.5<\/span>*<span class=\"hljs-number\">2.5<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>test_circle<\/code> module uses the <code>Circle<\/code> and <code>Shape<\/code> from the <code>circle<\/code> and <code>shape<\/code> test modules in the <code>shapes<\/code> package.<\/p>\n\n\n\n<p>test_square.py<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">import<\/span> unittest\n\n<span class=\"hljs-keyword\">from<\/span> shapes.square <span class=\"hljs-keyword\">import<\/span> Square\n<span class=\"hljs-keyword\">from<\/span> shapes.shape <span class=\"hljs-keyword\">import<\/span> Shape\n\n\n<span class=\"hljs-class\"><span class=\"hljs-keyword\">class<\/span> <span class=\"hljs-title\">TestSquare<\/span><span class=\"hljs-params\">(unittest.TestCase)<\/span>:<\/span>\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_create_square_negative_length<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        <span class=\"hljs-keyword\">with<\/span> self.assertRaises(ValueError):\n            square = Square(<span class=\"hljs-number\">-1<\/span>)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_square_instance_of_shape<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        square = Square(<span class=\"hljs-number\">10<\/span>)\n        self.assertIsInstance(square, Shape)\n\n    <span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">test_area<\/span><span class=\"hljs-params\">(self)<\/span>:<\/span>\n        square = Square(<span class=\"hljs-number\">10<\/span>)\n        area = square.area()\n        self.assertEqual(area, <span class=\"hljs-number\">100<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>test_square<\/code> module uses the <code>Square<\/code> and <code>Shape<\/code> classes from the <code>square<\/code> and <code>shape<\/code> modules in the <code>shapes<\/code> package.<\/p>\n\n\n\n<p>It&#8217;s important to create an <code>__init__.py<\/code> file and place it in the <code>test<\/code> folder. Otherwise, commands in the following section won&#8217;t work as expected.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='running-unit-tests'>Running unit tests <a href=\"#running-unit-tests\" class=\"anchor\" id=\"running-unit-tests\" title=\"Anchor for Running unit tests\">#<\/a><\/h2>\n\n\n\n<p>The <a href=\"https:\/\/www.pythontutorial.net\/python-unit-testing\/python-unittest\/\">unittest<\/a> module provides you with many ways to run unit tests.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id='1-running-all-tests'>1) Running all tests <a href=\"#1-running-all-tests\" class=\"anchor\" id=\"1-running-all-tests\" title=\"Anchor for 1) Running all tests\">#<\/a><\/h3>\n\n\n\n<p>To run all tests in the <code>test<\/code> directory, you execute the following command from the project directory folder (<code>python-unit-testing<\/code>):<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">python -m unittest discover -v<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The <code>discover<\/code> is a subcommand that finds all the tests in the project.<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">test_area (test_circle.TestCircle) ... ok\ntest_circle_instance_of_shape (test_circle.TestCircle) ... ok\ntest_create_circle_negative_radius (test_circle.TestCircle) ... ok\ntest_area (test_square.TestSquare) ... ok\ntest_create_square_negative_length (test_square.TestSquare) ... ok\ntest_square_instance_of_shape (test_square.TestSquare) ... ok\n\n----------------------------------------------------------------------\nRan <span class=\"hljs-number\">6<\/span> tests <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-number\">0.002<\/span>s\n\nOK<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='2-running-a-single-test-module'>2) Running a single test module <a href=\"#2-running-a-single-test-module\" class=\"anchor\" id=\"2-running-a-single-test-module\" title=\"Anchor for 2) Running a single test module\">#<\/a><\/h3>\n\n\n\n<p>To run a single test module, you use the following command:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">python -m unittest test_package.test_module -v<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example, the following execute all tests in the <code>test_circle<\/code> module of the <code>test<\/code> package:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">python -m unittest test.test_circle -v<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">test_area (test.test_circle.TestCircle) ... ok\ntest_circle_instance_of_shape (test.test_circle.TestCircle) ... ok     \ntest_create_circle_negative_radius (test.test_circle.TestCircle) ... ok\n\n---------------------------------------------------------------------- \nRan <span class=\"hljs-number\">3<\/span> tests <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-number\">0.000<\/span>s\n\nOK<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='3-running-a-single-test-class'>3) Running a single test class <a href=\"#3-running-a-single-test-class\" class=\"anchor\" id=\"3-running-a-single-test-class\" title=\"Anchor for 3) Running a single test class\">#<\/a><\/h3>\n\n\n\n<p>A test module may have multiple classes. To run a single test class in a test module, you use the following command:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">python -m unittest test_package.test_module.TestClass -v<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example, the following command tests the <code>TestSquare<\/code> class from the <code>test_square<\/code> module of the test package:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">python -m unittest test.test_circle.TestCircle -v<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">test_area (test.test_square.TestSquare) ... ok\ntest_create_square_negative_length (test.test_square.TestSquare) ... ok\ntest_square_instance_of_shape (test.test_square.TestSquare) ... ok\n\n----------------------------------------------------------------------\nRan <span class=\"hljs-number\">3<\/span> tests <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-number\">0.001<\/span>s\n\nOK<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\" id='4-running-a-single-test-method'>4) Running a single test method <a href=\"#4-running-a-single-test-method\" class=\"anchor\" id=\"4-running-a-single-test-method\" title=\"Anchor for 4) Running a single test method\">#<\/a><\/h3>\n\n\n\n<p>To run a single test method of a test class, you use the following command:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">python -m unittest test_package.test_module.TestClass.test_method -v<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example, the following command tests the <code>test_area()<\/code> method of the <code>TestCircle<\/code> class:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">python -m unittest test.test_circle.TestCircle.test_area -v<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">test_area (test.test_circle.TestCircle) ... ok\n\n----------------------------------------------------------------------\nRan <span class=\"hljs-number\">1<\/span> test <span class=\"hljs-keyword\">in<\/span> <span class=\"hljs-number\">0.001<\/span>s\n\nOK<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>Place the development code and test code in separate directories. It&#8217;s a good practice to store the test code in the test directory.<\/li><li>Use the command <code>python -m unittest discover -v<\/code> to discover and execute all the tests.<\/li><li>Use the command <code>python -m unittest test_package.test_module -v<\/code> to run a single test module.<\/li><li>Use the command <code>python -m unittest test_package.test_module.TestClass -v<\/code> to run a single test class.<\/li><li>Use the command <code>python -m unittest test_package.test_module.TestClass.test_method<\/code> -v to run a single test method.<\/li><\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"3643\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-unit-testing\/python-run-unittest\/\"\n\t\t\t\tdata-post-title=\"Organizing Code &#038; Running unittest\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"3643\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-unit-testing\/python-run-unittest\/\"\n\t\t\t\tdata-post-title=\"Organizing Code &#038; Running unittest\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll learn how to organize the test code and how to use the various commands to run unit tests. Organizing code # If you have a few modules, you can create test modules and place them within the same directory. In practice, you may have many modules organized into packages. Therefore, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3553,"menu_order":3,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3643","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3643","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=3643"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3643\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/3553"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=3643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}