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.
知道为什么会发生这个错误吗?
发布于 2015-12-09 16:00:35
问题是您的doctest
-formatted字符串不是docstring。
搜索模块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.
https://stackoverflow.com/questions/34177812
复制相似问题