Limitations of Doctest Module
The Doctest module in Python has some limitations:
- Limited Test Coverage: Doctest relies heavily on docstrings, making it suitable mainly for testing small code snippets or simple functions. It may not cover all aspects of complex functions or classes.
- Readability Issues: Test cases embedded within docstrings can sometimes clutter the documentation, leading to reduced readability, especially for larger projects with numerous tests.
- Brittleness: Doctest tests are sensitive to changes in output formatting or minor code modifications, which can cause tests to fail unnecessarily. This brittleness makes maintenance challenging, especially in evolving codebases.
Here’s a code example illustrating these limitations:
In this example, below code showcases how Doctest tests might fail to cover all scenarios, clutter the docstring, and become brittle, especially when dealing with edge cases like division by zero.
def divide(a, b):
"""
This function divides two numbers.
Define input and expected output:
>>> divide(4, 2)
2
>>> divide(10, 0)
Traceback (most recent call last):
...
ZeroDivisionError: division by zero
"""
return a / b
if __name__ == "__main__":
import doctest
doctest.testmod(verbose=True)
Output
Trying:
complex_function([1, 2, 3])
Expecting:
[3, 4, 5]
ok
Trying:
complex_function([-1, 0, 1])
Expecting:
[0, 1, 2]
**********************************************************************
File "__main__", line 7, in __main__.complex_function
Failed example:
complex_function([-1, 0, 1])
Expected:
[0, 1, 2]
Got:
[1, 2, 3]
Trying:
divide(4, 2)
Expecting:
2
**********************************************************************
File "__main__", line 5, in __main__.divide
Failed example:
divide(4, 2)
Expected:
2
Got:
2.0
Trying:
divide(10, 0)
Expecting:
Traceback (most recent call last):
...
ZeroDivisionError: division by zero
ok
Trying:
multiple_test_cases(0)
Expecting:
'Zero'
ok
Trying:
multiple_test_cases(5)
Expecting:
'Positive'
ok
Trying:
multiple_test_cases(-5)
Expecting:
'Negative'
ok
Trying:
multiple_test_cases(100)
Expecting:
'Large positive'
ok
Trying:
multiple_test_cases(-100)
Expecting:
'Large negative'
ok
1 items had no tests:
__main__
1 items passed all tests:
5 tests in __main__.multiple_test_cases
**********************************************************************
2 items had failures:
1 of 2 in __main__.complex_function
1 of 2 in __main__.divide
9 tests in 4 items.
7 passed and 2 failed.
***Test Failed*** 2 failures.
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)
Contact Us