Doctest Python Implementation in Step By Step

Doctest Python Implementation Step By Step

Doctest python is a Lightweight Test Automation utility. It helps the developer to write and test the code piece at the same place. Only we need to provide input and expected output in the docstring. Well, This article will give you an overview of doctest with step by step implementation.

 

Doctest python example –

Here we will create a very simple python function for cube calculation. This function will take a number as a parameter and return its cube. We will apply doctest python over this function. After it, we will invoke the complete flow of doctest. Let’s see Step by step.

 

Step 1: Importing doctest module

from doctest import testmod 

 

Step 2: Applying doctest

The correct way to implement doctest is to write input and output in Docstrings of the function. Actually, it works on text pattern. Lets see the below code for a better understanding.

def cube_cal(num):
  '''
  cube_cal function calculate te cube of the user input
  >>> cube_cal(3) 
  27 
  >>> cube_cal(5) 
  125
  '''
  result=num*num*num
  return result

As I have mentioned above the definition of doctest function contains the docstring. This docstring contains the user input and expected output of the function.

Step 3: invoking doctest-

Let’s invoke the testmod module. Here we will provide the function name as the argument. Here verbose is set True for complete detail tracing otherwise it will show only the failure report at the console.

testmod(name ='cube_cal', verbose = True) 

Complete Code with output –

#import 
from doctest import testmod 

#function with doctest
def cube_cal(num):
  '''
  cube_cal function calculate te cube of the user input
  >>> cube_cal(3) 
  27 
  '''
  result=num*(num*num)
  return result

#invoking
if __name__ == '__main__':
  testmod(name ='cube_cal', verbose = True) 
doctest python
doctest python

 

As it is clear from the above example the wrong implementation of the function at the logical level will be caught immediately.

Conclusion –

Well, In many scenarios we need to validate the logic of the function. For this, we can go the manual option but it will take a longer time. In the place of it, we may use doctest module. It will automate the stuff for us in just a few lines.

I hope you must like this short article about doctest uses and examples. Please write your comment over it.

 

Thanks 

Data Science Learner Team

 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Abhishek ( Chief Editor) , a data scientist with major expertise in NLP and Text Analytics. He has worked on various projects involving text data and have been able to achieve great results. He is currently manages Datasciencelearner.com, where he and his team share knowledge and help others learn more about data science.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner