python doctest:预期结果与“得到”相同结果但测试失败
我正处于使用 python 作为软件 QA 工具的学习阶段。
我编写了下一个简单的测试,以便在文本文件数字矩阵中找到字母“a”。 问题是测试失败,即使期望等于我得到的。
这是为什么?你能告诉我我做错了什么吗?
测试脚本:
fin = open("abc.txt", "r")
arr_fin = []
for line in fin:
arr_fin.append(line.split())
print arr_fin
for row in arr_fin:
arr_fin_1 = " ".join('{0:4}'.format(i or " ") for i in row)
print arr_fin_1
def find_letter(x, arr_fin_1):
"""
>>> find_letter('a', arr_fin_1)
97
"""
t=ord(x) #exchange to letter's ASCII value
for i in arr_fin_1:
if i==x:
print t
return;
def _test():
import doctest
doctest.testmod()
if __name__ == "__main__":
_test()
错误消息:
Expected:
97
Got:
97
**********************************************************************
1 items had failures:
1 of 1 in __main__.find_letter
***Test Failed*** 1 failures.
I am on a learning stage of using python as a tool for software QA.
I wrote the next simple test in order to find the letter 'a' in a text file number matrix.
problem is that the test fails even though the expect equals to what i got.
Why is that? Can you tell me what am I doing wrong?
test script:
fin = open("abc.txt", "r")
arr_fin = []
for line in fin:
arr_fin.append(line.split())
print arr_fin
for row in arr_fin:
arr_fin_1 = " ".join('{0:4}'.format(i or " ") for i in row)
print arr_fin_1
def find_letter(x, arr_fin_1):
"""
>>> find_letter('a', arr_fin_1)
97
"""
t=ord(x) #exchange to letter's ASCII value
for i in arr_fin_1:
if i==x:
print t
return;
def _test():
import doctest
doctest.testmod()
if __name__ == "__main__":
_test()
error message:
Expected:
97
Got:
97
**********************************************************************
1 items had failures:
1 of 1 in __main__.find_letter
***Test Failed*** 1 failures.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
97 后面有一个额外的空格 - 如果删除它,您的测试应该可以正常运行。
You've got an extra space after the 97 - if you remove it, your test should run fine.
这:
使您的函数返回
None
。您的意思是
return t
吗?除此之外,恕我直言,
doctest
测试应该是独立的。这是用户应该在文档中看到并且无需上下文即可理解的内容。在您的示例中,您使用的是模块本地arr_fin_1
对象,该对象对用户完全不透明。最好在find_letter
调用之前在 doctest 中定义它,以提供一个独立的示例。This:
Makes your function return
None
.Did you mean
return t
?Besides that, IMHO
doctest
tests are meant to be self-contained. This is something the user should see in your documentation and understand without context. In your example, you're using a module-localarr_fin_1
object which is completely opaque to the user. It's better to define it in the doctest before thefind_letter
call to provide a self-contained example.