I have the following function which I do unit testing with doctest.
from collections import deque
def fill_q(histq=deque([])):
"""
>>> fill_q()
deque([1, 2, 3])
>>> fill_q()
deque([1, 2, 3])
"""
if histq:
assert(len(histq) == 0)
histq.append(1)
histq.append(2)
histq.append(3)
return histq
if __name__ == "__main__":
import doctest
doctest.testmod()
the first case passes, but the second call to fill_q fails, yet it's the same code:
**********************************************************************
File "trial.py", line 7, in __main__.fill_q
Failed example:
fill_q()
Exception raised:
Traceback (most recent call last):
File "/usr/lib/python2.7/doctest.py", line 1289, in __run
compileflags, 1) in test.globs
File "<doctest __main__.fill_q[1]>", line 1, in <module>
fill_q()
File "trial.py", line 11, in fill_q
assert(len(histq) == 0)
AssertionError
**********************************************************************
1 items had failures:
1 of 2 in __main__.fill_q
***Test Failed*** 1 failures.
It looks like that doctest re-uses the local variable histq from the first test call, why is it doing this? This is very silly behaviour (provided it's not me doing sth crazy here).
Trueonly ifhistqwas empty?