Added doctest encoding command line option#2101
Added doctest encoding command line option#2101nicoddemus merged 8 commits intopytest-dev:featuresfrom
Conversation
…es. Defaults to UTF-8.
…cify encoding for internal testdir._makefile() for the tests.
testing/test_doctest.py
Outdated
| def test_encoding_latin1(self, testdir): | ||
| """Test support for --doctest-encoding option. | ||
| """ | ||
| testdir._makefile(".txt", [""" |
There was a problem hiding this comment.
at first glance i wonder if this shouldn't use unicode marked strings like u"..."
i believe the actual encoding can be a parametrize parameter, and we might want to add an exotic one just to be on the paranoid side
There was a problem hiding this comment.
That would break the code for Python 3.0 I guess because iirc it didn't have the unicode prefix anymore.
There was a problem hiding this comment.
@wheerd python 3.3 added it back for more pleasant dual codebases
testing/test_doctest.py
Outdated
| 'üäö' | ||
| testdir._makefile(".txt", [u""" | ||
| >>> len(u'üäö') | ||
| 3 |
There was a problem hiding this comment.
i propose making those a format string adding u in front of it for py2, and leaving it out for py3
that way the output can be tested
There was a problem hiding this comment.
In Python 2, they will be formatted as escape sequences: u'\x84\x94\x81'. Probably I can just repr(value) to the files.
_pytest/doctest.py
Outdated
| action="store_true", default=False, | ||
| help="ignore doctest ImportErrors", | ||
| dest="doctest_ignore_import_errors") | ||
| group.addoption("--doctest-encoding", |
There was a problem hiding this comment.
I think this would be better off being a ini option instead:
- If one has to use this option, then usually all
doctest.txtfiles will have the same encoding (if the developers want to keep their sanity that is) so it makes sense to configure that inpytest.ini; - If really desired, one can use the
-ooption to overrideinioptions in the command-line. - It is usually not a good idea to require users to pass certain command-line options to make the test suite pass, and in which case the users don't pass the correct option the test-suite will fail in mysterious ways;
testing/test_doctest.py
Outdated
| '*1 passed*', | ||
| ]) | ||
|
|
||
| def test_encoding_ascii(self, testdir): |
There was a problem hiding this comment.
I think you could use @pytest.mark.parametrize on the tests to avoid repeating yourself. Something like (already incorporating my request to change to an ini option):
import pytest
@pytest.mark.parametrize('string, encoding', [
(u'foo', 'ascii'),
(u'üäö', 'latin1'),
(u'üäö', 'utf-8'),
])
def test_encoding_latin1(testdir, string, encoding):
"""Test support for doctest_encoding option.
"""
testdir.makeini("""
[pytest]
doctest_encoding={0}
""".format(encoding))
testdir._makefile(".txt", u"""
>>> len(u'{0}')
3
""".format(string), encoding=encoding)
result = testdir.runpytest()
result.stdout.fnmatch_lines([
'*1 passed*',
])EDIT: fixed a comment left by accident
nicoddemus
left a comment
There was a problem hiding this comment.
Other than my comments, please add a CHANGELOG entry under New Features and add yourself to the CHANGELOG. 😁
Parametrized the tests for it.
doc/en/doctest.rst
Outdated
| can be given multiple times in the command-line. | ||
|
|
||
| You can specify the encoding that will be used for those doctest files | ||
| using the ``--doctest-encoding`` command line option:: |
There was a problem hiding this comment.
Please update the docs to mention the doctest_encoding ini option instead.
There was a problem hiding this comment.
Sorry, forgot about that.
testing/test_doctest.py
Outdated
| doctest_encoding={0} | ||
| """.format(encoding)) | ||
| doctest = u""" | ||
| >>> u"{}" |
There was a problem hiding this comment.
The use of {} is not supported by py26 and is breaking the CI, please change that to {0}, {1}, etc.
|
I think we're almost there, thanks again for the PR! |
doc/en/doctest.rst
Outdated
| can be given multiple times in the command-line. | ||
|
|
||
| You can specify the encoding that will be used for those doctest files | ||
| using the ``doctest_encoding`` ini option: |
There was a problem hiding this comment.
One last thing, please add this:
You can specify the encoding that will be used for those doctest files
using the ``doctest_encoding`` ini option:
.. versionadded:: 3.1
.. code-block:: iniTo convey to the users that this is new and was added in 3.1.
|
lovely - this is shaping up, good work |
|
The Thanks again @wheerd! |
I added a command line option to specify the encoding of doctest (.txt/.rst) files.
This should fix the error described in #540 which I am also having on my windows machine.