Multi version support for Python doctests

前端 未结 3 1934
礼貌的吻别
礼貌的吻别 2020-12-09 20:21

I am writing my doctests like this:

>>> some_function(a=1, b=2)
{u\'id\': u\'123\', u\'name\': u\'abc\'}

This works fine for Pytho

相关标签:
3条回答
  • 2020-12-09 20:33

    If you use pytest, you can just do:

    >>> some_function(a=1, b=2) # doctest: +ALLOW_UNICODE
    {u'id': u'123', u'name': u'abc'}
    

    And the u will be stripped if you are running Python 3, and kept in Python 2.

    0 讨论(0)
  • 2020-12-09 20:40

    I ran into the same problem with doctests in IPython. There's no neat solution, but I wrapped all of the u' prefixes in {}, i.e. {u}', and made a little function that would include or exclude them as appropriate.

    You can see the u_format() function and a doctest using it.

    But that's rather messy, so I've moved many tests away from doctests.

    Alternatively, you can test it like this:

    >>> some_function(a=1, b=2) == {'id': '123', 'name': 'abc'}
    True
    

    If you need some unicode strings in the keys, you can use u'abþ', and use distribute to run 2to3 on the doctests. But that only works on input code, not output reprs.

    0 讨论(0)
  • 2020-12-09 20:44

    I run into the same issue with doctests in NLTK; it was solved by using a custom doctest output checker (that treats u'foo' and 'foo' the same) which is installed by a custom nose plugin: https://github.com/nltk/nltk/blob/develop/nltk/test/doctest_nose_plugin.py

    This solution is not pretty, but it works quite well (there are about 0.5 megabytes of doctests in NLTK) and it doesn't make doctests less readable.

    EDIT: found a simplified standalone version of this nose plugin: https://github.com/gnublade/doctest-ignore-unicode

    0 讨论(0)
提交回复
热议问题