Python doctest失败了,因为#34;期望"并且"得到"

时间:2017-05-25 07:40:33

标签: python doctest

我尝试将doctest用于我的场景:

def cycle(*its):
    """Return the cyclic iterator.

    >>> c = cycle([1,2,3])
    >>> [next(c) for i in range(10)]
    [1, 2, 3, 1, 2, 3, 1, 2, 3, 1]  
    """
    saved = []
    for it in its:
        for el in it:
            saved.append(el)
            yield el
    while True:
        for el in saved:
            yield el

if __name__ == "__main__":   
    import doctest
    doctest.testmod()

但我失败了:

Trying:
    c = cycle([1,2,3])
Expecting nothing
ok
Trying:
    [next(c) for i in range(10)]
Expecting:
    [1, 2, 3, 1, 2, 3, 1, 2, 3, 1]
**********************************************************************
File "D:\work\python_questions\iterators.py", line 7, in __main__.cycle
Failed example:
    [next(c) for i in range(10)]
Expected:
    [1, 2, 3, 1, 2, 3, 1, 2, 3, 1]
Got:
    [1, 2, 3, 1, 2, 3, 1, 2, 3, 1]
1 items had no tests:
    __main__
**********************************************************************
1 items had failures:
   1 of   2 in __main__.cycle
2 tests in 2 items.
1 passed and 1 failed.
***Test Failed*** 1 failures.

为什么预期:[1,2,3,1,2,3,1,2,3,1]并得到:[1,2,3,1,2,3,1,2,3,1] ] 不等于? (Windows7上的Python 3.6.1)

1 个答案:

答案 0 :(得分:1)

在预期值之后检查隐藏的空格或制表符。例如

[1, 2, 3, 1, 2, 3, 1, 2, 3, 1]___

您可以轻松地看到他们选择整个预期行(即使在上面的帖子中)。 希望对您有帮助