Skip to content Skip to sidebar Skip to footer

How Can I Make Doctests Triggered By Pytest Ignore Unicode Prefix `u'...'` Of Strings?

I want my code to work in Python 2 and 3. I use doctests and from __future__ import unicode_literals Is there a flag I can set / a plugin which makes it ignore that Python 2 has t

Solution 1:

If you're using doctest directly, you can override the OutputChecker as per Dirkjan Ochtman's blog post Single-source Python 2/3 doctests:

class Py23DocChecker(doctest.OutputChecker):
  def check_output(self, want, got, optionflags):
    if sys.version_info[0] > 2:
      want = re.sub("u'(.*?)'", "'\\1'", want)
      want = re.sub('u"(.*?)"', '"\\1"', want)
    return doctest.OutputChecker.check_output(self, want, got, optionflags)

doctest.DocTestSuite(mod, checker=Py23DocChecker())

If you're using py.test, you can specify doctest_optionflags = ALLOW_UNICODE in pytest.ini. See https://docs.pytest.org/en/latest/doctest.html

Post a Comment for "How Can I Make Doctests Triggered By Pytest Ignore Unicode Prefix `u'...'` Of Strings?"