简体   繁体   English

python doctest:预期结果与“got”结果相同,但测试失败

[英]python doctest: expected result is the same as the “got” result but the test failed

I am on a learning stage of using python as a tool for software QA.我正处于使用 python 作为软件 QA 工具的学习阶段。

I wrote the next simple test in order to find the letter 'a' in a text file number matrix.我编写了下一个简单的测试,以便在文本文件编号矩阵中找到字母“a”。 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.

You've got an extra space after the 97 - if you remove it, your test should run fine.在 97 之后你有一个额外的空间 - 如果你删除它,你的测试应该运行良好。

This:这个:

return;

Makes your function return None .使您的 function 返回None

Did you mean return t ?你的意思是return t吗?


Besides that, IMHO doctest tests are meant to be self-contained.除此之外,恕我直言doctest测试是独立的。 This is something the user should see in your documentation and understand without context.这是用户应该在您的文档中看到并在没有上下文的情况下理解的内容。 In your example, you're using a module-local arr_fin_1 object which is completely opaque to the user.在您的示例中,您使用的是对用户完全不透明的模块本地arr_fin_1 object。 It's better to define it in the doctest before the find_letter call to provide a self-contained example.最好在find_letter调用之前在 doctest 中定义它,以提供一个独立的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM