What if our Logic goes Wrong?

Below code, uses doctests to validate the factorial function. The function is designed to recursively compute the factorial of a positive integer, as documented in the docstring. However, there’s an issue with the recursive logic. The testmod function is called to verify the function’s correctness against the provided test cases.

Python3
# import testmod for testing our function 
from doctest import testmod 
  
# define a function to test 
def factorial(n): 
    ''' 
    This function calculates recursively and 
    returns the factorial of a positive number. 
    Define input and expected output: 
    >>> factorial(3) 
    6 
    >>> factorial(5) 
    120 
    '''
    if n <= 1: 
        return 1
    # wrong logic for factorial 
    return factorial(n - 1) 
  
# call the testmod function 
if __name__ == '__main__': 
    testmod(name ='factorial', verbose = True) 

Output:

Trying:
factorial(3)
Expecting:
6
**********************************************************************
File "woking_with_csv.py", line 33, in factorial.factorial
Failed example:
factorial(3)
Expected:
6
Got:
1
Trying:
factorial(5)
Expecting:
120
**********************************************************************
File "woking_with_csv.py", line 35, in factorial.factorial
Failed example:
factorial(5)
Expected:
120
Got:
1
1 items had no tests:
factorial
**********************************************************************
1 items had failures:
2 of 2 in factorial.factorial
2 tests in 2 items.
0 passed and 2 failed.
***Test Failed*** 2 failures.

Note: If ‘verbose’ is set to False(default), output will be shown in case of failure only, not in the case of success.

Testing in Python using Doctest module

Docstrings in Python are used not only for the description of a class or a function to provide a better understanding of the code and use but, also used for Testing purposes. Testing is a critical aspect of software development that ensures code functions as expected and guards against bugs. In Python, the Doctest module provides a simple yet powerful way to test code by embedding test cases within docstrings.

Table of Content

  • What is Doctest Module in Python
  • Why Choose Doctest in Python?
  • How to Use Doctest in Python?
  • What if our Logic goes Wrong?
  • Examples to Understand Doctest Module
  • Limitations of Doctest module
  • Frequently Asked Questions (FAQ’s)

Similar Reads

What is Doctest Module in Python

The Doctest Module finds patterns in the docstring that look like interactive shell commands. The input and expected output are included in the docstring, then the doctest module uses this docstring for testing the processed output. After parsing through the docstring, the parsed text is executed as Python shell commands and the result is compared with the expected outcome fetched from the docstring....

Why Choose Doctest in Python?

The reason for choosing Doctest in Python is that Doctest in Python is favored for its simplicity, seamlessly integrating documentation with testing. By embedding test cases within docstrings, promotes clear and concise code examples. It’s a choice unburdened by AI, relying solely on human-authored tests for verification, ensuring transparency and control over the testing process....

How to Use Doctest in Python ?

Here’s we will see how to use doctest in Python, here is a simple example of how to use doctest in Python....

What if our Logic goes Wrong?

Below code, uses doctests to validate the factorial function. The function is designed to recursively compute the factorial of a positive integer, as documented in the docstring. However, there’s an issue with the recursive logic. The testmod function is called to verify the function’s correctness against the provided test cases....

Examples to Understand Doctest Module

Below, are the example of testing in Python using Doctest Module in Python for better understanding....

Limitations of Doctest Module

The Doctest module in Python has some limitations:...

Frequently Asked Questions (FAQ’s)

1. How to Run Doctest in Python?...

Contact Us