{"id":18415,"date":"2021-12-03T09:06:55","date_gmt":"2021-12-03T03:36:55","guid":{"rendered":"http:\/\/www.pythonpool.com\/?p=18415"},"modified":"2021-12-03T09:06:58","modified_gmt":"2021-12-03T03:36:58","slug":"python-doctest","status":"publish","type":"post","link":"https:\/\/www.pythonpool.com\/python-doctest\/","title":{"rendered":"Testing Got Easy with Python Doctest"},"content":{"rendered":"\n<p>Hello Everyone! I hope all are doing great. So, today in this article, we will discuss the python doctest module. We will see what docstrings are and how they help us write clear, understandable, and testable codes. It provides us a <a href=\"http:\/\/www.pythonpool.com\/python-list-intersection\/\" target=\"_blank\" rel=\"noopener\">way to use the interactive mode of python<\/a> in script mode with the help of docstrings. Let&#8217;s understand them one by one. <\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_74 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #990303;color:#990303\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #990303;color:#990303\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 eztoc-toggle-hide-by-default' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.pythonpool.com\/python-doctest\/#Python_Docstrings\" >Python Docstrings<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.pythonpool.com\/python-doctest\/#Doctest_Module\" >Doctest Module<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.pythonpool.com\/python-doctest\/#Example_1_-_Defining_Docstrings_within_same_file\" >Example 1:- Defining Docstrings within same file<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.pythonpool.com\/python-doctest\/#Example_2_-_Defining_Docstrings_within_same_Modules\" >Example 2:-  Defining Docstrings within same Modules<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.pythonpool.com\/python-doctest\/#Example_3_-_Using_testmode_function\" >Example 3:- Using testmode() function<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.pythonpool.com\/python-doctest\/#DocTest_Vs_UnitTest\" >DocTest Vs UnitTest<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.pythonpool.com\/python-doctest\/#How_to_Skip_Entire_Block_in_Python_Doctest\" >How to Skip Entire Block in Python Doctest<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.pythonpool.com\/python-doctest\/#FAQs\" >FAQs<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/www.pythonpool.com\/python-doctest\/#Conclusion\" >Conclusion<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/www.pythonpool.com\/python-doctest\/#Trending_Python_Articles\" >Trending Python Articles<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-python-docstrings\"><span class=\"ez-toc-section\" id=\"Python_Docstrings\"><\/span>Python Docstrings<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>So, python docstrings are the kind of strings declared using triple single quotes or triple-double quotes. <strong>The primary function of docstrings is to associate documentation with <span style=\"text-decoration: underline;\"><a href=\"http:\/\/www.pythonpool.com\/category\/module\/\" target=\"_blank\" rel=\"noreferrer noopener\">python modules<\/a><\/span>, python functions, or <span style=\"text-decoration: underline;\"><a href=\"http:\/\/www.pythonpool.com\/python-class-vs-module\/\" target=\"_blank\" rel=\"noreferrer noopener\">python classes<\/a><\/span>. Unlike comments, docstrings are used to describe the use of the function rather than explaining how it works. <\/strong><\/p>\n\n\n\n<p>It is advised to use docstrings for each function to explain its use. And we can access them by using either of the ways.<\/p>\n\n\n\n<ul class=\"has-base-background-color has-background wp-block-list\"><li><strong>__doc__ method<\/strong> of the object :- Ex: &lt;function_name&gt;.__doc__<\/li><li><strong>help() :-<\/strong> help(&lt;function_name&gt;)<\/li><\/ul>\n\n\n\n<p>However, it is compulsory to use docstrings while testing the file using the python doctest module. We won&#8217;t go into detail about it as this is enough to understand the doctest module. So let&#8217;s move on to that.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-doctest-module\"><span class=\"ez-toc-section\" id=\"Doctest_Module\"><\/span>Doctest Module<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Doctest Module gives us the option to test our file by using docstrings. It looks for the code section in docstrings, which appears like an interactive python shell. Then it runs the given samples on those sections of code and returns whether it passed the test or failed it. There are several ways to use doctest module. We will see each of them and better understand each part.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-example-1-defining-docstrings-within-same-file\"><span class=\"ez-toc-section\" id=\"Example_1_-_Defining_Docstrings_within_same_file\"><\/span>Example 1:- Defining Docstrings within same file<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In this example, we will see how we can use docstrings to test the file while executing them. To can do that by following the given steps.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Step 1: Create docstrings by using triple single quotes or triple double quotes with the function we want to test.<\/li><li>Step 2: Call the function as python interactive shell mode starting with &#8220;&gt;&gt;&gt;&#8221;.<\/li><li>Step3: Define the output in next line.<\/li><li>Step4: Run the file by using command,<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>python -m doctest -v filename.py<\/code><\/pre>\n\n\n\n<p>Let&#8217;s take an example.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&quot;&quot;&quot;\nExamples on Doctest\n&quot;&quot;&quot;\n\ndef addition_function(obj1,obj2):\n    &quot;&quot;&quot;\n    &gt;&gt;&gt; addition_function(5,2)\n    7\n    &gt;&gt;&gt; addition_function('Python','Pool')\n    'PythonPool'\n    &quot;&quot;&quot;    \n    return obj1+obj2\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Trying:\n    addition_function(5,2)\nExpecting:\n    7\nok\nTrying:\n    addition_function('Python','Pool')\nExpecting:\n    'PythonPool'\nok\n1 items had no tests:\n    2\n1 items passed all tests:\n   2 tests in 2.addition_function\n2 tests in 2 items.\n2 passed and 0 failed.\nTest passed.<\/code><\/pre>\n\n\n\n<p>Now, think, what if one of the test cases failed. Let&#8217;s check that now.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&quot;&quot;&quot;\nExamples on Doctest\n&quot;&quot;&quot;\n\ndef addition_function(obj1,obj2):\n    &quot;&quot;&quot;\n    &gt;&gt;&gt; addition_function(5,2)\n    8   \n    &gt;&gt;&gt; addition_function('Python','Pool')\n    'PythonPool'\n    &quot;&quot;&quot;    \n    return obj1+obj2\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Trying:\n    addition_function(5,2)\nExpecting:\n    8\n**********************************************************************\nFile \"C:\\Users\\Rishav Raj\\Documents\\filename.py\", line 7, in 2.addition_function\nFailed example:\n    addition_function(5,2)\nExpected:\n    8\nGot:\n    7\nTrying:\n    addition_function('Python','Pool')\nExpecting:\n    'PythonPool'\nok\n1 items had no tests:\n    2\n**********************************************************************\n1 items had failures:\n   1 of   2 in 2.addition_function\n2 tests in 2 items.\n1 passed and 1 failed.\n***Test Failed*** 1 failures.<\/code><\/pre>\n\n\n\n<p>So, if a test case fails, it shows which test case is failed and the whole description about that.<\/p>\n\n\n<div class=\"monsterinsights-inline-popular-posts monsterinsights-inline-popular-posts-india monsterinsights-popular-posts-styled\" ><div class=\"monsterinsights-inline-popular-posts-text\"><span class=\"monsterinsights-inline-popular-posts-label\" >Trending:<\/span><span class=\"monsterinsights-inline-popular-posts-border\" ><\/span><div class=\"monsterinsights-inline-popular-posts-post\"><a class=\"monsterinsights-inline-popular-posts-title\" style=\"font-size:18px;\" href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-cant-compare-datetime-datetime-to-datetime-date\/\">[Fixed] typeerror can&#8217;t compare datetime.datetime to datetime.date<\/a><\/div><\/div><\/div><p><\/p>\n\n\n<h3 class=\"wp-block-heading\" id=\"h-example-2-defining-docstrings-within-same-modules\"><span class=\"ez-toc-section\" id=\"Example_2_-_Defining_Docstrings_within_same_Modules\"><\/span>Example 2:-  Defining Docstrings within same Modules<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>In the above example, we have seen how we can define a docstring within the same file and then test it. Now, we will see how we can maintain a separate file for testing purposes. The procedure for defining the docstring is as above. We need to import functions <span style=\"text-decoration: underline;\"><strong><a href=\"http:\/\/www.pythonpool.com\/import-classes-from-another-file-in-python\/\" target=\"_blank\" rel=\"noreferrer noopener\">from other classes<\/a><\/strong><\/span> and define docstrings there. Let&#8217;s see an example of that.<\/p>\n\n\n\n<p>First, we will create a sample.py file that contains the function we want to test, and then we create a test.py file for the testing. We will use the following command to run the testing file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>python -m doctest -v test.py<\/code><\/pre>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# sample.py file\ndef multiplication_function(a,b):\n    return a*b\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Test.py file\n\n#Using Python interactive shell mode\n&quot;&quot;&quot;\n&gt;&gt;&gt; from sample import *\n&gt;&gt;&gt; multiplication_function(8,2)\n16\n&gt;&gt;&gt; multiplication_function('PythonPool',2)\n'PythonPoolPythonPool'\n&quot;&quot;&quot;\n\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Trying:\n    from sample import *\nExpecting nothing\nok\nTrying:\n    multiplication_function(8,2)\nExpecting:\n    16\nok\nTrying:\n    multiplication_function('PythonPool',2)\nExpecting:\n    'PythonPoolPythonPool'\nok\n1 items passed all tests:\n   3 tests in test\n3 tests in 1 items.\n3 passed and 0 failed.\nTest passed.<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"h-example-3-using-testmode-function\"><span class=\"ez-toc-section\" id=\"Example_3_-_Using_testmode_function\"><\/span>Example 3:- Using testmode() function<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>We can also check the test cases using the testmode method. We have to pass the function (we want to test) as an argument of the testmode function. Let&#8217;s take an example to understand it clearly.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# import testmod\nfrom doctest import testmod\n\ndef factorial_function(num):\n    &quot;&quot;&quot;\n    &gt;&gt;&gt; factorial_function(5)\n    120\n    &quot;&quot;&quot;   \n    factorial = 1\n    if num &lt; 0:\n        print(&quot;Number should be greater than 0&quot;)\n    elif num == 0:\n        print(&quot;The factorial of 0 is 1&quot;)\n    else:\n        for i in range(1,num + 1):\n            factorial = factorial*i\n        print(factorial)\n<\/pre><\/div>\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Trying:\n    factorial_function(5)\nExpecting:\n    120\nok\n1 items had no tests:\n    factorial_function\n1 items passed all tests:\n   1 tests in factorial_function.factorial_function\n1 tests in 2 items.\n1 passed and 0 failed.\nTest passed.<\/code><\/pre>\n\n\n\n<p>In the above example, we have the second argument named verbose, which should be &#8220;true&#8221; to get the test results on our output screen.<\/p>\n\n\n<div class=\"monsterinsights-inline-popular-posts monsterinsights-inline-popular-posts-kilo monsterinsights-popular-posts-styled\" ><div class=\"monsterinsights-inline-popular-posts-text\"><span class=\"monsterinsights-inline-popular-posts-label\" >Popular now<\/span><span class=\"monsterinsights-inline-popular-posts-border\" ><\/span><span class=\"monsterinsights-inline-popular-posts-border-2\" ><\/span><div class=\"monsterinsights-inline-popular-posts-post\"><a class=\"monsterinsights-inline-popular-posts-title\"  href=\"https:\/\/www.pythonpool.com\/fixed-nameerror-name-unicode-is-not-defined\/\">[Fixed] nameerror: name Unicode is not defined<\/a><\/div><\/div><\/div><p><\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-doctest-vs-unittest\"><span class=\"ez-toc-section\" id=\"DocTest_Vs_UnitTest\"><\/span>DocTest Vs UnitTest<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Comparing both is a kind of injustice to them as both serve different purposes. Doctest is used when we need to give examples of our code for informative documentation, while the goal of the <span style=\"text-decoration: underline;\"><strong><a href=\"http:\/\/www.pythonpool.com\/python-unittest-vs-pytest\/\" target=\"_blank\" rel=\"noreferrer noopener\">unitest<\/a><\/strong><\/span> is to thoroughly test every case rather than illustrate what it does by the example. Moreover, we can also say that by using doctest, we can also check the proper documentation only based on the code, which is not possible in the unittest framework.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-how-to-skip-entire-block-in-python-doctest\"><span class=\"ez-toc-section\" id=\"How_to_Skip_Entire_Block_in_Python_Doctest\"><\/span>How to Skip Entire Block in Python Doctest<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>You can also skip the entire block within the docstring while doctesting. To do that, You have to change <strong>&#8220;>>>&#8221; to &#8220;>>&#8221; and &#8220;&#8230;&#8221; to &#8220;..&#8221;<\/strong>. So,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"\"\"\n&gt;&gt;&gt; multiplication_function(8,2)\n16\n&gt;&gt;&gt; multiplication_function('PythonPool',2)\n'PythonPoolPythonPool'\n\"\"\"<\/code><\/pre>\n\n\n\n<p><strong>becomes<\/strong>:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\"\"\"\n&gt;&gt; multiplication_function(8,2)\n16\n&gt;&gt; multiplication_function('PythonPool',2)\n'PythonPoolPythonPool'\n\"\"\"<\/code><\/pre>\n\n\n<div class=\"monsterinsights-inline-popular-posts monsterinsights-inline-popular-posts-delta monsterinsights-popular-posts-styled\" ><div class=\"monsterinsights-inline-popular-posts-text\"><span class=\"monsterinsights-inline-popular-posts-icon\" style=\"\"><svg width=\"14\" height=\"19\" viewBox=\"0 0 14 19\" fill=\"none\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\"><path d=\"M7.875 0.899463C7.875 1.59183 8.0816 2.24711 8.49479 2.8653C8.93229 3.48349 9.44271 4.06458 10.026 4.60859C10.6337 5.15259 11.2292 5.73369 11.8125 6.35188C12.4201 6.97007 12.9306 7.76135 13.3438 8.72572C13.7812 9.66537 14 10.7163 14 11.8785C14 13.832 13.3073 15.5011 11.9219 16.8858C10.5608 18.2953 8.92014 19 7 19C5.07986 19 3.42708 18.2953 2.04167 16.8858C0.680556 15.5011 0 13.832 0 11.8785C0 9.94973 0.668403 8.28062 2.00521 6.87116C2.27257 6.57443 2.58854 6.50024 2.95312 6.64861C3.31771 6.79697 3.5 7.08134 3.5 7.50171V10.6545C3.5 11.3221 3.71875 11.8908 4.15625 12.3607C4.61806 12.8305 5.16493 13.0654 5.79688 13.0654C6.45312 13.0654 7.01215 12.8428 7.47396 12.3978C7.93576 11.9279 8.16667 11.3592 8.16667 10.6916C8.16667 10.2712 8.04514 9.86318 7.80208 9.46754C7.58333 9.0719 7.31597 8.71336 7 8.3919C6.68403 8.07044 6.34375 7.73662 5.97917 7.39043C5.63889 7.04425 5.34722 6.66097 5.10417 6.2406C4.88542 5.82024 4.73958 5.35041 4.66667 4.83114C4.59375 4.31186 4.67882 3.68131 4.92188 2.93948C5.18924 2.17293 5.63889 1.33219 6.27083 0.417277C6.51389 0.0463641 6.84201 -0.0772735 7.25521 0.0463641C7.6684 0.170002 7.875 0.454368 7.875 0.899463Z\" fill=\"#EB5757\"><\/path><\/svg><\/span><span class=\"monsterinsights-inline-popular-posts-label\" >Trending<\/span><div class=\"monsterinsights-inline-popular-posts-post\"><a class=\"monsterinsights-inline-popular-posts-title\" style=\"font-size:18px;\" href=\"https:\/\/www.pythonpool.com\/solved-runtimeerror-cuda-error-invalid-device-ordinal\/\">[Solved] runtimeerror: cuda error: invalid device ordinal<\/a><\/div><\/div><\/div><p><\/p>\n\n\n<h2 class=\"wp-block-heading\" id=\"h-faqs\"><span class=\"ez-toc-section\" id=\"FAQs\"><\/span>FAQs<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1638409730322\"><strong class=\"schema-faq-question\">Q1) How to make reusable objects in python doctest?<\/strong> <p class=\"schema-faq-answer\">You can use\u00a0<code>testmod(extraglobs={:})<\/code>\u00a0it to define a reusable object globally.<br\/><code>testmod(extraglobs={'f': initFileGenerator('')})<\/code><\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1638409901064\"><strong class=\"schema-faq-question\">Q2) Is it possible to run python doctest on a jupyter cell function?<\/strong> <p class=\"schema-faq-answer\">Yes, You can use them in the jupyter cell function by using testmode method as discussed above.<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1638410003460\"><strong class=\"schema-faq-question\">Q3) How to write a python doctest that is os independent regarding path separator?<\/strong> <p class=\"schema-faq-answer\">You can use the &#8220;os.path.join()&#8221; method to do that. <br\/>e.g.<br\/><code>>>> relative_path == os.path.join('bar', 'foobar')<\/code><br\/>True<\/p> <\/div> <div class=\"schema-faq-section\" id=\"faq-question-1638410166077\"><strong class=\"schema-faq-question\">Q4)Can you check that an exception is thrown with doctest in python?<\/strong> <p class=\"schema-faq-answer\">Yes, we can do that. Let&#8217;s see the following example to understand it.<br\/><br\/>>>> x<br\/><code>Traceback (most recent call last):<\/code><br\/><code> ... <\/code><br\/><code>NameError: name 'x' is not defined<\/code><br\/><br\/>By defining the exception after the shell mode, you can check the error in doctest mode.<\/p> <\/div> <\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"h-conclusion\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>So, today in this article, we have seen how we can use the testing functionality provided by python doctest to test our file. We have witnessed <a href=\"https:\/\/en.wikipedia.org\/wiki\/Docstring\" target=\"_blank\" rel=\"noreferrer noopener\">docstrings<\/a> and how we can use those for our testing purpose. At the end of the article, we have seen the different ways of testing our file. <\/p>\n\n\n\n<p>I hope this article has helped you. Thank You.<\/p>\n\n\n<div class=\"monsterinsights-widget-popular-posts monsterinsights-widget-popular-posts-delta monsterinsights-popular-posts-styled monsterinsights-widget-popular-posts-columns-2\"><h2 class=\"monsterinsights-widget-popular-posts-widget-title\"><span class=\"ez-toc-section\" id=\"Trending_Python_Articles\"><\/span>Trending Python Articles<span class=\"ez-toc-section-end\"><\/span><\/h2><ul class=\"monsterinsights-widget-popular-posts-list\"><li ><a href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-cant-compare-datetime-datetime-to-datetime-date\/\"><div class=\"monsterinsights-widget-popular-posts-image\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/typeerror-cant-compare-datetime.datetime-to-datetime.date_.webp 1200w \" alt=\"[Fixed] typeerror can&#8217;t compare datetime.datetime to datetime.date\" \/><\/div><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" >[Fixed] typeerror can&#8217;t compare datetime.datetime to datetime.date<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><span class=\"monsterinsights-widget-popular-posts-author\">by Namrata Gulati<\/span><span>&#9679;<\/span><span class=\"monsterinsights-widget-popular-posts-date\">January 11, 2024<\/span><\/div><\/div><\/a><\/li><li ><a href=\"https:\/\/www.pythonpool.com\/fixed-nameerror-name-unicode-is-not-defined\/\"><div class=\"monsterinsights-widget-popular-posts-image\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-nameerror-name-Unicode-is-not-defined.webp 1200w \" alt=\"[Fixed] nameerror: name Unicode is not defined\" \/><\/div><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" >[Fixed] nameerror: name Unicode is not defined<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><span class=\"monsterinsights-widget-popular-posts-author\">by Namrata Gulati<\/span><span>&#9679;<\/span><span class=\"monsterinsights-widget-popular-posts-date\">January 2, 2024<\/span><\/div><\/div><\/a><\/li><li ><a href=\"https:\/\/www.pythonpool.com\/solved-runtimeerror-cuda-error-invalid-device-ordinal\/\"><div class=\"monsterinsights-widget-popular-posts-image\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Solved-runtimeerror-cuda-error-invalid-device-ordinal.webp 1200w \" alt=\"[Solved] runtimeerror: cuda error: invalid device ordinal\" \/><\/div><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" >[Solved] runtimeerror: cuda error: invalid device ordinal<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><span class=\"monsterinsights-widget-popular-posts-author\">by Namrata Gulati<\/span><span>&#9679;<\/span><span class=\"monsterinsights-widget-popular-posts-date\">January 2, 2024<\/span><\/div><\/div><\/a><\/li><li ><a href=\"https:\/\/www.pythonpool.com\/fixed-typeerror-type-numpy-ndarray-doesnt-define-__round__-method\/\"><div class=\"monsterinsights-widget-popular-posts-image\"><img decoding=\"async\" src=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method-300x157.webp\" srcset=\" https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method-300x157.webp 300w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method-1024x536.webp 1024w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method-768x402.webp 768w, https:\/\/www.pythonpool.com\/wp-content\/uploads\/2024\/01\/Fixed-typeerror-type-numpy.ndarray-doesnt-define-__round__-method.webp 1200w \" alt=\"[Fixed] typeerror: type numpy.ndarray doesn&#8217;t define __round__ method\" \/><\/div><div class=\"monsterinsights-widget-popular-posts-text\"><span class=\"monsterinsights-widget-popular-posts-title\" >[Fixed] typeerror: type numpy.ndarray doesn&#8217;t define __round__ method<\/span><div class=\"monsterinsights-widget-popular-posts-meta\" ><span class=\"monsterinsights-widget-popular-posts-author\">by Namrata Gulati<\/span><span>&#9679;<\/span><span class=\"monsterinsights-widget-popular-posts-date\">January 2, 2024<\/span><\/div><\/div><\/a><\/li><\/ul><\/div><p><\/p>","protected":false},"excerpt":{"rendered":"<p>Hello Everyone! I hope all are doing great. So, today in this article, we will discuss the python doctest module. We will see what docstrings &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"Testing Got Easy with Python Doctest\" class=\"read-more button\" href=\"https:\/\/www.pythonpool.com\/python-doctest\/#more-18415\" aria-label=\"More on Testing Got Easy with Python Doctest\">Read more<\/a><\/p>\n","protected":false},"author":25,"featured_media":18583,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4345],"tags":[4568,4567,4566,4565],"class_list":["post-18415","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-module","tag-doctest-python-correct-way","tag-python-doctest-command-line","tag-python-doctest-example","tag-run-doctest-python","infinite-scroll-item"],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v20.1 (Yoast SEO v25.0) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Testing Got Easy with Python Doctest - Python Pool<\/title>\n<meta name=\"description\" content=\"Python Doctest: In this article we have seen how can we use different method to test our file using python doctest module.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pythonpool.com\/python-doctest\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Testing Got Easy with Python Doctest\" \/>\n<meta property=\"og:description\" content=\"Hello Everyone! I hope all are doing great. So, today in this article, we will discuss the python doctest module. We will see what docstrings are and how\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pythonpool.com\/python-doctest\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Pool\" \/>\n<meta property=\"article:published_time\" content=\"2021-12-03T03:36:55+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-12-03T03:36:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/12\/Python-Doctest.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"Rishav Raj\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:site\" content=\"@pythonpool\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Rishav Raj\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.pythonpool.com\/python-doctest\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-doctest\/\"},\"author\":{\"name\":\"Rishav Raj\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/025222e28182ecbb97e17f9f1bf15ac4\"},\"headline\":\"Testing Got Easy with Python Doctest\",\"datePublished\":\"2021-12-03T03:36:55+00:00\",\"dateModified\":\"2021-12-03T03:36:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-doctest\/\"},\"wordCount\":865,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-doctest\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/12\/Python-Doctest.webp\",\"keywords\":[\"doctest python correct way\",\"python doctest command line\",\"python doctest example\",\"run doctest python\"],\"articleSection\":[\"Module\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.pythonpool.com\/python-doctest\/#respond\"]}]},{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\/\/www.pythonpool.com\/python-doctest\/\",\"url\":\"https:\/\/www.pythonpool.com\/python-doctest\/\",\"name\":\"Testing Got Easy with Python Doctest - Python Pool\",\"isPartOf\":{\"@id\":\"https:\/\/www.pythonpool.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-doctest\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-doctest\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/12\/Python-Doctest.webp\",\"datePublished\":\"2021-12-03T03:36:55+00:00\",\"dateModified\":\"2021-12-03T03:36:58+00:00\",\"description\":\"Python Doctest: In this article we have seen how can we use different method to test our file using python doctest module.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.pythonpool.com\/python-doctest\/#breadcrumb\"},\"mainEntity\":[{\"@id\":\"https:\/\/www.pythonpool.com\/python-doctest\/#faq-question-1638409730322\"},{\"@id\":\"https:\/\/www.pythonpool.com\/python-doctest\/#faq-question-1638409901064\"},{\"@id\":\"https:\/\/www.pythonpool.com\/python-doctest\/#faq-question-1638410003460\"},{\"@id\":\"https:\/\/www.pythonpool.com\/python-doctest\/#faq-question-1638410166077\"}],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.pythonpool.com\/python-doctest\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/python-doctest\/#primaryimage\",\"url\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/12\/Python-Doctest.webp\",\"contentUrl\":\"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/12\/Python-Doctest.webp\",\"width\":1200,\"height\":628,\"caption\":\"Python Doctest\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.pythonpool.com\/python-doctest\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.pythonpool.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Testing Got Easy with Python Doctest\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.pythonpool.com\/#website\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"name\":\"Python Pool\",\"description\":\"Your One-Stop Python Learning Destination\",\"publisher\":{\"@id\":\"https:\/\/www.pythonpool.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.pythonpool.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.pythonpool.com\/#organization\",\"name\":\"Python Pool\",\"url\":\"https:\/\/www.pythonpool.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\",\"url\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"contentUrl\":\"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png\",\"width\":452,\"height\":185,\"caption\":\"Python Pool\"},\"image\":{\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/pythonpool\",\"https:\/\/www.youtube.com\/c\/pythonpool\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/025222e28182ecbb97e17f9f1bf15ac4\",\"name\":\"Rishav Raj\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/77d441cf39fc8183322bfc1dcaf04be5c1fd429574820606cccac81fd8e8749a?s=96&d=wavatar&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/77d441cf39fc8183322bfc1dcaf04be5c1fd429574820606cccac81fd8e8749a?s=96&d=wavatar&r=g\",\"caption\":\"Rishav Raj\"}},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/python-doctest\/#faq-question-1638409730322\",\"position\":1,\"url\":\"https:\/\/www.pythonpool.com\/python-doctest\/#faq-question-1638409730322\",\"name\":\"Q1) How to make reusable objects in python doctest?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"You can use\u00a0testmod(extraglobs={:})\u00a0it to define a reusable object globally.<br\/>testmod(extraglobs={'f': initFileGenerator('')})\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/python-doctest\/#faq-question-1638409901064\",\"position\":2,\"url\":\"https:\/\/www.pythonpool.com\/python-doctest\/#faq-question-1638409901064\",\"name\":\"Q2) Is it possible to run python doctest on a jupyter cell function?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Yes, You can use them in the jupyter cell function by using testmode method as discussed above.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/python-doctest\/#faq-question-1638410003460\",\"position\":3,\"url\":\"https:\/\/www.pythonpool.com\/python-doctest\/#faq-question-1638410003460\",\"name\":\"Q3) How to write a python doctest that is os independent regarding path separator?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"You can use the \\\"os.path.join()\\\" method to do that. <br\/>e.g.<br\/>>>> relative_path == os.path.join('bar', 'foobar')<br\/>True\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"},{\"@type\":\"Question\",\"@id\":\"https:\/\/www.pythonpool.com\/python-doctest\/#faq-question-1638410166077\",\"position\":4,\"url\":\"https:\/\/www.pythonpool.com\/python-doctest\/#faq-question-1638410166077\",\"name\":\"Q4)Can you check that an exception is thrown with doctest in python?\",\"answerCount\":1,\"acceptedAnswer\":{\"@type\":\"Answer\",\"text\":\"Yes, we can do that. Let's see the following example to understand it.<br\/><br\/>>>> x<br\/>Traceback (most recent call last):<br\/> ... <br\/>NameError: name 'x' is not defined<br\/><br\/>By defining the exception after the shell mode, you can check the error in doctest mode.\",\"inLanguage\":\"en-US\"},\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Testing Got Easy with Python Doctest - Python Pool","description":"Python Doctest: In this article we have seen how can we use different method to test our file using python doctest module.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.pythonpool.com\/python-doctest\/","og_locale":"en_US","og_type":"article","og_title":"Testing Got Easy with Python Doctest","og_description":"Hello Everyone! I hope all are doing great. So, today in this article, we will discuss the python doctest module. We will see what docstrings are and how","og_url":"https:\/\/www.pythonpool.com\/python-doctest\/","og_site_name":"Python Pool","article_published_time":"2021-12-03T03:36:55+00:00","article_modified_time":"2021-12-03T03:36:58+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/12\/Python-Doctest.webp","type":"image\/webp"}],"author":"Rishav Raj","twitter_card":"summary_large_image","twitter_creator":"@pythonpool","twitter_site":"@pythonpool","twitter_misc":{"Written by":"Rishav Raj","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.pythonpool.com\/python-doctest\/#article","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/python-doctest\/"},"author":{"name":"Rishav Raj","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/025222e28182ecbb97e17f9f1bf15ac4"},"headline":"Testing Got Easy with Python Doctest","datePublished":"2021-12-03T03:36:55+00:00","dateModified":"2021-12-03T03:36:58+00:00","mainEntityOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-doctest\/"},"wordCount":865,"commentCount":0,"publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-doctest\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/12\/Python-Doctest.webp","keywords":["doctest python correct way","python doctest command line","python doctest example","run doctest python"],"articleSection":["Module"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.pythonpool.com\/python-doctest\/#respond"]}]},{"@type":["WebPage","FAQPage"],"@id":"https:\/\/www.pythonpool.com\/python-doctest\/","url":"https:\/\/www.pythonpool.com\/python-doctest\/","name":"Testing Got Easy with Python Doctest - Python Pool","isPartOf":{"@id":"https:\/\/www.pythonpool.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.pythonpool.com\/python-doctest\/#primaryimage"},"image":{"@id":"https:\/\/www.pythonpool.com\/python-doctest\/#primaryimage"},"thumbnailUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/12\/Python-Doctest.webp","datePublished":"2021-12-03T03:36:55+00:00","dateModified":"2021-12-03T03:36:58+00:00","description":"Python Doctest: In this article we have seen how can we use different method to test our file using python doctest module.","breadcrumb":{"@id":"https:\/\/www.pythonpool.com\/python-doctest\/#breadcrumb"},"mainEntity":[{"@id":"https:\/\/www.pythonpool.com\/python-doctest\/#faq-question-1638409730322"},{"@id":"https:\/\/www.pythonpool.com\/python-doctest\/#faq-question-1638409901064"},{"@id":"https:\/\/www.pythonpool.com\/python-doctest\/#faq-question-1638410003460"},{"@id":"https:\/\/www.pythonpool.com\/python-doctest\/#faq-question-1638410166077"}],"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pythonpool.com\/python-doctest\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/python-doctest\/#primaryimage","url":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/12\/Python-Doctest.webp","contentUrl":"https:\/\/www.pythonpool.com\/wp-content\/uploads\/2021\/12\/Python-Doctest.webp","width":1200,"height":628,"caption":"Python Doctest"},{"@type":"BreadcrumbList","@id":"https:\/\/www.pythonpool.com\/python-doctest\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pythonpool.com\/"},{"@type":"ListItem","position":2,"name":"Testing Got Easy with Python Doctest"}]},{"@type":"WebSite","@id":"https:\/\/www.pythonpool.com\/#website","url":"https:\/\/www.pythonpool.com\/","name":"Python Pool","description":"Your One-Stop Python Learning Destination","publisher":{"@id":"https:\/\/www.pythonpool.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pythonpool.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.pythonpool.com\/#organization","name":"Python Pool","url":"https:\/\/www.pythonpool.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/","url":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","contentUrl":"http:\/\/www.pythonpool.com\/wp-content\/uploads\/2020\/08\/aa.png","width":452,"height":185,"caption":"Python Pool"},"image":{"@id":"https:\/\/www.pythonpool.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/pythonpool","https:\/\/www.youtube.com\/c\/pythonpool"]},{"@type":"Person","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/025222e28182ecbb97e17f9f1bf15ac4","name":"Rishav Raj","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.pythonpool.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/77d441cf39fc8183322bfc1dcaf04be5c1fd429574820606cccac81fd8e8749a?s=96&d=wavatar&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/77d441cf39fc8183322bfc1dcaf04be5c1fd429574820606cccac81fd8e8749a?s=96&d=wavatar&r=g","caption":"Rishav Raj"}},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/python-doctest\/#faq-question-1638409730322","position":1,"url":"https:\/\/www.pythonpool.com\/python-doctest\/#faq-question-1638409730322","name":"Q1) How to make reusable objects in python doctest?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"You can use\u00a0testmod(extraglobs={:})\u00a0it to define a reusable object globally.<br\/>testmod(extraglobs={'f': initFileGenerator('')})","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/python-doctest\/#faq-question-1638409901064","position":2,"url":"https:\/\/www.pythonpool.com\/python-doctest\/#faq-question-1638409901064","name":"Q2) Is it possible to run python doctest on a jupyter cell function?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"Yes, You can use them in the jupyter cell function by using testmode method as discussed above.","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/python-doctest\/#faq-question-1638410003460","position":3,"url":"https:\/\/www.pythonpool.com\/python-doctest\/#faq-question-1638410003460","name":"Q3) How to write a python doctest that is os independent regarding path separator?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"You can use the \"os.path.join()\" method to do that. <br\/>e.g.<br\/>>>> relative_path == os.path.join('bar', 'foobar')<br\/>True","inLanguage":"en-US"},"inLanguage":"en-US"},{"@type":"Question","@id":"https:\/\/www.pythonpool.com\/python-doctest\/#faq-question-1638410166077","position":4,"url":"https:\/\/www.pythonpool.com\/python-doctest\/#faq-question-1638410166077","name":"Q4)Can you check that an exception is thrown with doctest in python?","answerCount":1,"acceptedAnswer":{"@type":"Answer","text":"Yes, we can do that. Let's see the following example to understand it.<br\/><br\/>>>> x<br\/>Traceback (most recent call last):<br\/> ... <br\/>NameError: name 'x' is not defined<br\/><br\/>By defining the exception after the shell mode, you can check the error in doctest mode.","inLanguage":"en-US"},"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/18415","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/users\/25"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/comments?post=18415"}],"version-history":[{"count":21,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/18415\/revisions"}],"predecessor-version":[{"id":24481,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/posts\/18415\/revisions\/24481"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media\/18583"}],"wp:attachment":[{"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/media?parent=18415"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/categories?post=18415"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.pythonpool.com\/wp-json\/wp\/v2\/tags?post=18415"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}