Examples to Understand Doctest Module
Below, are the example of testing in Python using Doctest Module in Python for better understanding.
Testing a Function with Outputs
When testing a function with complex outputs in Python using the Doctest module, it’s crucial to provide clear and detailed docstring examples that cover various scenarios. Ensure that the expected outputs are accurately documented for each input.
def complex_function(input_list):
'''
This function performs complex operations on the input list.
Define input and expected output:
>>> complex_function([1, 2, 3])
[3, 4, 5]
>>> complex_function([-1, 0, 1])
[0, 1, 2]
'''
return [x + 2 for x in input_list]
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]
1 items had no tests:
__main__
**********************************************************************
1 items had failures:
1 of 2 in __main__.complex_function
2 tests in 2 items.
1 passed and 1 failed.
***Test Failed*** 1 failures.
Testing a Function with Multiple Test Cases
When testing a function with multiple test cases in Python using the Doctest module, ensure to cover various input scenarios and edge cases within the docstring examples. Each test case should be well-documented with its expected output. Here’s a code example illustrating testing a function with multiple test cases:
def multiple_test_cases(number):
'''
This function checks multiple test cases for a given number.
Define input and expected output:
>>> multiple_test_cases(0)
'Zero'
>>> multiple_test_cases(5)
'Positive'
>>> multiple_test_cases(-5)
'Negative'
>>> multiple_test_cases(100)
'Large positive'
>>> multiple_test_cases(-100)
'Large negative'
'''
if number == 0:
return 'Zero'
elif number > 0:
if number > 10:
return 'Large positive'
else:
return 'Positive'
else:
if number < -10:
return 'Large negative'
else:
return 'Negative'
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:
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
**********************************************************************
1 items had failures:
1 of 2 in __main__.complex_function
7 tests in 3 items.
6 passed and 1 failed.
***Test Failed*** 1 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