Skip to content

Instantly share code, notes, and snippets.

@u1and0
Last active October 9, 2016 00:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save u1and0/d9f729d7d98e9e8de10ea03ee2193119 to your computer and use it in GitHub Desktop.
Save u1and0/d9f729d7d98e9e8de10ea03ee2193119 to your computer and use it in GitHub Desktop.
python docstring内にテストを記述 ref: http://qiita.com/u1and0/items/3a7b73dca38414f3a703
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...
In [81]: run doctest_use.py
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]
name?>> doctest_sample
EOF
[Finished in 0.4s]
import docstring
def hoge():
'''
hoge(args)
'''
if __name__ == "__main__":
doctest.testmod()
def twice(n):
""" 引数を 2 倍して返す関数
>>> twice(8)
16
>>> twice(1850923)
3701846
"""
return n * 2
print('name?>>', __name__)
if __name__ == "__main__":
doctest.testmod()
print('EOF')
{
"cmd": ["python", "$file", "-v"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.python"
}
def twice(n):
""" 引数を 2 倍して返す関数
>>> twice(8)
16
>>> twice(1850923)
"""
return n * 2
'''TEST
twice(8)
# 実行結果
#16
twice(1850923)
# 実行結果
#3701846
'''# ←ここのトリプルクォートを'TEST'って書いてあるところまで引き上げてやることでtwice(8), twice(1850923)を実行する
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment