Answer the question
In order to leave comments, you need to log in
How to write a doc test for a function?
The first time you need to write a doc test for a small function. Based on what I found on the Internet, I do the following:
1. I write the function code and save it to a file called func_name.py
def func_name(argument):
#Some code
return result
>>> import ~\func_name.py
>>> func_name(test_argument)
test_result
python -m doctest func_name_test.txt
name 'func_name'
- from file_name import *
from file_name import func_name
Answer the question
In order to leave comments, you need to log in
Decided as follows:
We write the test (except for import, of course) to the module file immediately after the function declaration. So:
def func_name(argument):
"""
>>> func_name(test_argument)
test_result
"""
#Some code
return result
if __name__ == "__main__":
import doctest
doctest.testmod()
python func_name.py -v
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question