The doctest.testmod ()
function of the doctest
module describes the test in the docstring and runs it in verbose mode to execute the test.
sample.py
def twice(n):
"""A function that doubles the argument and returns
>>> twice(8)
16
>>> twice(1850923)
"""
return n * 2
'''TEST
twice(8)
#Execution result
#16
twice(1850923)
#Execution result
#3701846
'''#← Triple quotes here'TEST'By pulling up to the place where it is written, twice(8), twice(1850923)To run
If you raise the triple quote under TEST
to TEST
, the docstring-like multi-line comment out will be removed, and when executed, the twice
function will be executed and tested.
That TEST
is below the function, so it looks bad.
I want to put it together in a docstring if possible.
However, if you use the doctest module, you can write it in the docstring, and you can do dexterity that does not execute when executed from other modules.
doctest_sample.py
def twice(n):
"""A function that doubles the argument and returns
>>> twice(8)
16
>>> twice(1850923)
3701846
"""
return n * 2
print('name?>>', __name__)
if __name__ == "__main__":
doctest.testmod()
print('EOF')
%run doctest_sample -v
%run doctest_use.py -v
Just import the contents of doctest_use.py.py
import doctest_sample
```
When running a python file on ipython, run it with'% run doctest.testmod ()
, execute it in verbose mode -v
.
When executing on sublime text, build with ctrl + b
.
Execution of verbose mode is not included by default (whether it is false or true or not verified), so create a build system and save it in the User directory
Select Python_Verbose
created from Menu> Tools> Buile system and ctrl + b
Python_Verbose.sublime-build
{
"cmd": ["python", "$file", "-v"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
1 Command line and environment (http://docs.python.jp/3.3/using/cmdline.html) 1.1 Command line 1.1.3 Other options
-u¶ (original)
Force the binary layer of the stdout and stderr streams (which is available as their buffer attribute) to be unbuffered. The text I/O layer will still be line-buffered if writing to the console, or block-buffered if redirected to a non-interactive file.
See also PYTHONUNBUFFERED.
-v
Each time a module is initialized, it displays a message telling you where it was loaded (filename or built-in module). If specified twice (-vv), a message will be displayed for each file checked when searching for modules. It also provides information about module cleanup on exit. See also PYTHONVERBOSE.
% run doctest_sample -v
on ipython1.1
In [80]: run doctest_sample.py -v
name?>> __main__
Trying:
twice(8)
Expecting:
16
ok
Trying:
twice(1850923)
Expecting:
3701846
ok
1 items had no tests:
__main__
1 items passed all tests:
2 tests in __main__.twice
2 tests in 2 items.
2 passed and 0 failed.
Test passed.
EOF
Since the name attribute is main, doctest is executed
The description of doctest is
Execute in python format where you wrote > >>
and return the result
If there are no errors, issue ʻok` and proceed to the next test.
1.1
Trying:
twice(8)
Expecting:
16
ok
View test summary
1.1
1 items had no tests:
__main__
1 items passed all tests:
2 tests in __main__.twice
2 tests in 2 items.
2 passed and 0 failed.
Test passed.
body...
% run doctest_use -v
on ipython1.2
In [81]: run doctest_use.py
No output is displayed.
2.1
name?>> __main__
Trying:
twice(8)
Expecting:
16
ok
Trying:
twice(1850923)
Expecting:
3701846
ok
1 items had no tests:
__main__
1 items passed all tests:
2 tests in __main__.twice
2 tests in 2 items.
2 passed and 0 failed.
Test passed.
EOF
[Finished in 0.5s]
Same as 1.1 result except [Finished in 0.5s]
that appears in sublime text build result
2.2
name?>> doctest_sample
EOF
[Finished in 0.4s]
Unlike result 1.2, the print statement seems to be executed.
docstring.testmod ()
python <filename> -v
)doc.py
import docstring
def hoge():
'''
hoge(args)
'''
if __name__ == "__main__":
doctest.testmod()
Test with Python Command line and environment
Recommended Posts