You can unit test each file by writing as follows (production is skipped)
test.py
#!-*- coding:utf-8 -*-
def double(x):
"""A function that returns 3 times the given number
Unit test the content from here
>>> double(3)
9
Basically you can freely comment">>>"And only the next line is recognized
>>> double(10)
30
>>> double(8)
24
"""
return x * 3
if __name__ == "__main__":
import doctest
doctest.testmod()
You can unit test by running it with the -v option. (If you do not add -v, nothing will be returned if it ends normally)
python test.py -v
Trying:
double(3)
Expecting:
9
ok
Trying:
double(10)
Expecting:
30
ok
Trying:
double(8)
Expecting:
24
ok
1 items had no tests:
__main__
1 items passed all tests:
3 tests in __main__.double
3 tests in 2 items.
3 passed and 0 failed.
Test passed.
ʻIf name == "main": only when executed as a script Operate. (Skip when ʻimport test.py
is done)
The doctest module allows you to write test code and its results as comments It will test automatically. (It is convenient because it remains as a comment about what kind of check was done)
Recommended Posts