Python: Creating a DocTest / simple example

We use doctest to check whether a written method is giving the correct output or to check for the docstrings for interactive examples or for regression testing or to write documentation.
Consider this example
def multiplyTwoNumbers(a,b):
product = a*b
return product
this method is used for multiplying two numbers. But there is no way to verify that the code written does that actually. What you can do is add a doctest to this.
def multiplyTwoNumbers(a,b):
This test is for multiplication of two numbers
>>> multiplyTwoNumbers(3,4)
product = a*b
return product
def _test():
import doctest
if __name__ == __main__:
This will test the output to the methods output and will give an error if they dont match. In this way doctest can be used. From command line it can be used by calling:
python -v to give a descriptive error sequence else you can just execute the program.


Leave a Reply

Your email address will not be published. Required fields are marked *