Python 预期==获取的doctests失败

Python 预期==获取的doctests失败,python,doctest,Python,Doctest,今天学习如何使用doctest。早些时候,我发现必须添加。现在,我如何让剩余的失败测试通过?对于这三种情况,预期值和实际值之间没有差异: C:\Windows\System32>python C:\Users\George\AppData\Local\Programs\Python\Python3 5-32\Lib\exemplarypy.py ********************************************************************** Fi

今天学习如何使用doctest。早些时候,我发现必须添加。现在,我如何让剩余的失败测试通过?对于这三种情况,预期值和实际值之间没有差异:

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()
导入系统 类HaltException(异常):传递 def查找名称(单元格值): """ 返回单元格_值中“[example]”后面的(已删除的)标记。 单元格_值的附加参数或“.py”将被排除在外: [示例FizzBuzz(x,y,z)]=>FizzBuzz [示例FizzBuzz.py]=>FizzBuzz 博士测试: >>>导入示例 >>>打印(示例py.find_name(“t(”)) T >>>打印(示例Y.find_name(“”) >>>打印(示例Y.find_name(“”) >>>打印(示例py.find_name(“”) 第一个单元格必须包含“.”xor“(' 没有一个 >>>打印(示例py.find_name(“x(adsf)”) x >>>打印(examplarypy.find_name(“asdf.txt”)) asdf >>>打印(示例py.find_name(“.asdf.txt”)) >>>打印(示例py.find_name(“x.asdf.txt”)) x >>>打印(示例Y.find_name(“(.”)) 第一个单元格不能同时包含“.”和“(” 没有一个 >>>打印(示例Y.find_name(“x(.”)) 第一个单元格不能同时包含“.”和“(” 没有一个 """ 尝试: 如果单元格\u值。查找(“.”>-1: 如果单元格\u值。查找(“”>-1: raise HaltException(“第一个单元格不能同时包含“.”和“(”) 边界=单元格_值。查找(“.”) 返回单元格_值[0:边界] 如果单元格\u值。查找(“”>-1: 边界=单元格_值。查找(“”) 返回单元格_值[0:边界] raise HaltException(“第一个单元格必须包含''xor'(')) 除HaltException外,如h: 打印(h) #要测试嵌入式docstring文档,请执行以下操作: 如果名称=“\uuuuu main\uuuuuuuu”: 进口医生测试 doctest.testmod()
附录:将预期和获取的文本与Notepad++中的Show All字符进行比较;它们的空格是相同的。

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

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


现在所有测试都通过了。

这与错误消息末尾的空白有关吗?可能重复@mkrieger1这两个问题确实有空白解答,但这不是重复,因为这里需要删除的空白是在程序本身中,而不是在doctests中。
raise HaltException("First cell must contain '.' xor '(' ")