I tried to use a StringIO instance in a doctest in my class, in a Python 2.7 program. Instead of getting any output from the test, I get a response, "Got nothing".
This simplified test case demonstrates the error:
#!/usr/bin/env python2.7
# encoding: utf-8
class Dummy(object):
'''Dummy: demonstrates a doctest problem
>>> from StringIO import StringIO
... s = StringIO()
... print("s is created")
s is created
'''
if __name__ == "__main__":
import doctest
doctest.testmod()
Expected behaviour: test passes.
Observed behaviour: test fails, with output like this:
% ./src/doctest_fail.py
**********************************************************************
File "./src/doctest_fail.py", line 7, in __main__.Dummy
Failed example:
from StringIO import StringIO
s = StringIO()
print("s is created")
Expected:
s is created
Got nothing
**********************************************************************
1 items had failures:
1 of 1 in __main__.Dummy
***Test Failed*** 1 failures.
Why is this doctest failing? What change to I need to make in order to be able to use StringIO-like functionality (a literal string with a file interface) in my doctests?
It's the continuation line syntax (...) that is confusing doctest parser. This works:
#!/usr/bin/env python2.7
# encoding: utf-8
class Dummy(object):
'''Dummy: demonstrates a doctest problem
>>> from StringIO import StringIO
>>> s = StringIO()
>>> print("s is created")
s is created
'''
if __name__ == "__main__":
import doctest
doctest.testmod()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With