doctests compatibility for python 2 & python 3

Robin Becker robin at reportlab.com
Fri Jan 17 06:16:17 EST 2014


I have some problems making some doctests for python2 code compatible with 
python3. The problem is that as part of our approach we are converting the code 
to use unicode internally. So we allow eihter byte strings or unicode in inputs, 
but we are trying to convert to unicode outputs.

That makes doctests quite hard as

def func(a):
     """
     >>> func(u'aaa')
     'aaa'
     """
     return a

fails in python2 whilst

def func(a):
     """
     >>> func(u'aaa')
     u'aaa'
     """
     return a

fails in python3. Aside from changing the tests so they look like
     """
     >>> func(u'aaa')==u'aaa'
     True
     """
which make the test utility harder. If the test fails I don't see the actual 
outcome and expected I see expected True got False.

Is there an easy way to make these kinds of tests work in python 2 & 3?
-- 
Robin Becker




More information about the Python-list mailing list