-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added doctest encoding command line option #2101
Changes from 3 commits
ed97751
d254c6b
929912d
c043bbb
1f62e5b
b7fb9fa
f8fef07
2edfc80
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,13 @@ can change the pattern by issuing:: | |
on the command line. Since version ``2.9``, ``--doctest-glob`` | ||
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. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please update the docs to mention the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, forgot about that. |
||
|
||
pytest --doctest-encoding='ascii' | ||
|
||
The default encoding is UTF-8. | ||
|
||
You can also trigger running of doctests | ||
from docstrings in all python modules (including regular | ||
python test modules):: | ||
|
@@ -52,9 +59,9 @@ then you can just invoke ``pytest`` without command line options:: | |
platform linux -- Python 3.5.2, pytest-3.0.4, py-1.4.31, pluggy-0.4.0 | ||
rootdir: $REGENDOC_TMPDIR, inifile: pytest.ini | ||
collected 1 items | ||
|
||
mymodule.py . | ||
|
||
======= 1 passed in 0.12 seconds ======== | ||
|
||
It is possible to use fixtures using the ``getfixture`` helper:: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,6 +129,52 @@ def test_multiple_patterns(self, testdir): | |
'*1 passed*', | ||
]) | ||
|
||
def test_encoding_ascii(self, testdir): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you could use 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 |
||
"""Test support for --doctest-encoding option. | ||
""" | ||
testdir._makefile(".txt", [""" | ||
>>> 1 | ||
1 | ||
"""], {}, encoding='ascii') | ||
|
||
result = testdir.runpytest("--doctest-encoding=ascii") | ||
|
||
result.stdout.fnmatch_lines([ | ||
'*1 passed*', | ||
]) | ||
|
||
def test_encoding_latin1(self, testdir): | ||
"""Test support for --doctest-encoding option. | ||
""" | ||
testdir._makefile(".txt", [u""" | ||
>>> len(u'üäö') | ||
3 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i propose making those a format string adding u in front of it for py2, and leaving it out for py3 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In Python 2, they will be formatted as escape sequences: |
||
"""], {}, encoding='latin1') | ||
|
||
result = testdir.runpytest("--doctest-encoding=latin1") | ||
|
||
result.stdout.fnmatch_lines([ | ||
'*1 passed*', | ||
]) | ||
|
||
def test_encoding_utf8(self, testdir): | ||
"""Test support for --doctest-encoding option. | ||
""" | ||
testdir.maketxtfile(u""" | ||
>>> len(u'üäö') | ||
3 | ||
""") | ||
|
||
result = testdir.runpytest() | ||
result.stdout.fnmatch_lines([ | ||
'*1 passed*', | ||
]) | ||
|
||
result = testdir.runpytest("--doctest-encoding=utf-8") | ||
result.stdout.fnmatch_lines([ | ||
'*1 passed*', | ||
]) | ||
|
||
def test_doctest_unexpected_exception(self, testdir): | ||
testdir.maketxtfile(""" | ||
>>> i = 0 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this would be better off being a
ini
option instead:doctest.txt
files will have the same encoding (if the developers want to keep their sanity that is) so it makes sense to configure that inpytest.ini
;-o
option to overrideini
options in the command-line.