import doctest
def create_grid(size):
grid = []
for i in range(size):
row = ['0']*size
grid.append(row)
"""
>>> create_grid(4)
[['0', '0', '0', '0'], ['0', '0', '0', '0'],
['0', '0', '0', '0'], ['0', '0', '0', '0']]
"""
return grid
if __name__ == '__main__':
doctest.testmod()
使用python Test_av_doctest.py -v
运行上述内容会显示以下消息:
2 items had no tests:
__main__
__main__.create_grid
0 tests in 2 items.
0 passed and 0 failed.
Test passed.
知道这个错误发生的原因吗?
答案 0 :(得分:0)
问题是您的doctest
格式化字符串不是文档字符串。
Which docstrings are examined?
模块docstring,搜索所有函数,类和方法文档字符串。
如果将测试字符串移到函数定义下面,它将成为函数docstring,因此将被doctest
定位:
def create_grid(size):
"""
>>> create_grid(4)
[['0', '0', '0', '0'], ['0', '0', '0', '0'],
['0', '0', '0', '0'], ['0', '0', '0', '0']]
"""
grid = []
for i in range(size):
row = ['0']*size
grid.append(row)
return grid
if __name__ == '__main__':
doctest.testmod()
$ python Test_av_doctest.py -v
...
1 passed and 0 failed.
Test passed.