预期==获得doctests失败

时间:2017-02-26 22:22:24

标签: python doctest

今天学习如何使用doctest。之前我发现必须添加< BLANKLINE>。现在我如何让剩下的失败测试通过?对于所有三个,预期和得到之间没有区别:

C:\Windows\System32>python C:\Users\George\AppData\Local\Programs\Python\Python3
5-32\Lib\exemplarypy.py
**********************************************************************
File "C:\Users\George\AppData\Local\Programs\Python\Python35-32\Lib\exemplarypy.
py", line 27, in __main__.find_name
Failed example:
    print(exemplarypy.find_name(""))
Expected:
    First cell must contain '.' xor '('
    None
Got:
    First cell must contain '.' xor '('
    None
**********************************************************************
File "C:\Users\George\AppData\Local\Programs\Python\Python35-32\Lib\exemplarypy.
py", line 38, in __main__.find_name
Failed example:
    print(exemplarypy.find_name("(."))
Expected:
    First cell cannot contain both '.' and '('
    None
Got:
    First cell cannot contain both '.' and '('
    None
**********************************************************************
File "C:\Users\George\AppData\Local\Programs\Python\Python35-32\Lib\exemplarypy.
py", line 41, in __main__.find_name
Failed example:
    print(exemplarypy.find_name("x(."))
Expected:
    First cell cannot contain both '.' and '('
    None
Got:
    First cell cannot contain both '.' and '('
    None
**********************************************************************
1 items had failures:
   3 of  11 in __main__.find_name
***Test Failed*** 3 failures.

C:\Windows\System32>

正在运行的文件:

import sys

class HaltException(Exception): pass

def find_name(cell_value):
    """
    Return the (denuded) token that comes after "[example " in cell_value.

    cell_value's parenthetical argument or ".py" will be excluded:
        [example FizzBuzz(x, y, z)] => FizzBuzz
        [example FizzBuzz.py]       => FizzBuzz

    Doctesting:
>>> import exemplarypy
>>> print(exemplarypy.find_name("t(("))
t
>>> print(exemplarypy.find_name("(("))
<BLANKLINE>
>>> print(exemplarypy.find_name("("))
<BLANKLINE>
>>> print(exemplarypy.find_name(""))
First cell must contain '.' xor '('
None
>>> print(exemplarypy.find_name("x(adsf)"))
x
>>> print(exemplarypy.find_name("asdf.txt"))
asdf
>>> print(exemplarypy.find_name(".asdf.txt"))
<BLANKLINE>
>>> print(exemplarypy.find_name("x.asdf.txt"))
x
>>> print(exemplarypy.find_name("(."))
First cell cannot contain both '.' and '('
None
>>> print(exemplarypy.find_name("x(."))
First cell cannot contain both '.' and '('
None

    """
    try:
        if cell_value.find(".") > -1:
            if cell_value.find("(") > -1:
                raise HaltException("First cell cannot contain both '.' and '(' ")
            boundary = cell_value.find(".")
            return cell_value[0:boundary]
        if cell_value.find("(") > -1:
            boundary = cell_value.find("(")
            return cell_value[0:boundary]
        raise HaltException("First cell must contain '.' xor '(' ")
    except HaltException as h:
        print(h)

# To test the embedded docstring documentation:
if __name__ == "__main__":
    import doctest
    doctest.testmod()

附录:在Notepad ++中比较Expected和Got with Show All Characters;他们的空白是相同的。

1 个答案:

答案 0 :(得分:0)

好的电话,@ jonrsharpe,谢谢:我只需删除这些字符串末尾的空格,即在最后一个单引号和双引号之间。

raise HaltException("First cell cannot contain both '.' and '(' ")

raise HaltException("First cell must contain '.' xor '(' ")

现在所有测试都通过了。