简体   繁体   English

Python doctest失败,原因是“预期”和“等于”相等

[英]Python doctest failed for equal “Expected” and “Got”

I try to use doctest for my scenario: 我尝试对我的场景使用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()

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? 为什么期望:[1、2、3、1、2、3、1、2、3、1]和得到:[1、2、3、1、2、3、1、2、3、1]不等于? (Python 3.6.1 on Windows7) (Windows7上的Python 3.6.1)

Check for hidden spaces or tabs just after the expected value. 在预期值之后检查隐藏的空格或制表符。 Eg 例如

[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 希望能帮助到你

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

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