问题
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