テストサンプルを見ると、その関数が何をするのかぱっと見で分かるので、便利。
import doctest import operator def aggregate_val(f, x): """ aggregate values of a dict "x" with a binary function "f". >>> aggregate_val(operator.add, {'x': 10, 'y': 20}) 30 >>> aggregate_val(min, {'x': 10, 'y': 20}) 10 """ return reduce(f, [v for _, v in x.items()]) if __name__ == '__main__': doctest.testmod()
スクリプトから実行して、エラーがあると教えてくれる。
また、-vオプションを指定するとテストの詳細を教えてくれる。
kenjih$ python main.py -v
Trying:
aggregate_val(operator.add, {'x': 10, 'y': 20})
Expecting:
30
ok
Trying:
aggregate_val(min, {'x': 10, 'y': 20})
Expecting:
10
ok
1 items had no tests:
__main__
1 items passed all tests:
2 tests in __main__.aggregate_val
2 tests in 2 items.
2 passed and 0 failed.
Test passed.
0 件のコメント:
コメントを投稿