Python doctest failed for equal “Expected” and “Got”

感情迁移 提交于 2020-01-07 04:18:06

问题


I try to use doctest for my scenario:

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()

But I get Failed in out:

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.

Why Expected: [1, 2, 3, 1, 2, 3, 1, 2, 3, 1] and Got: [1, 2, 3, 1, 2, 3, 1, 2, 3, 1] does not equal? (Python 3.6.1 on Windows7)


回答1:


Check for hidden spaces or tabs just after the expected value. E.g.

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

You can see them easily selecting the whole expected line (even in your post above). Hope it helps



来源:https://stackoverflow.com/questions/44174988/python-doctest-failed-for-equal-expected-and-got

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!