Skip to content

Commit 669332b

Browse files
authored
Merge pull request #2101 from wheerd/doctest-encoding
Added doctest encoding command line option
2 parents 9c224c9 + 2edfc80 commit 669332b

File tree

6 files changed

+54
-9
lines changed

6 files changed

+54
-9
lines changed

AUTHORS

+1
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ Lukas Bednar
8686
Luke Murphy
8787
Maciek Fijalkowski
8888
Maho
89+
Manuel Krebber
8990
Marc Schlaich
9091
Marcin Bachry
9192
Mark Abramowitz

CHANGELOG.rst

+6-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
New Features
66
------------
77

8-
*
8+
* Added an ini option ``doctest_encoding`` to specify which encoding to use for doctest files.
9+
Thanks `@wheerd`_ for the PR (`#2101`_).
910

1011
*
1112

@@ -25,10 +26,10 @@ Changes
2526
assertion messages if verbosity < 2 (`#1512`_).
2627
Thanks `@mattduck`_ for the PR
2728

28-
* ``--pdbcls`` no longer implies ``--pdb``. This makes it possible to use
29+
* ``--pdbcls`` no longer implies ``--pdb``. This makes it possible to use
2930
``addopts=--pdbcls=module.SomeClass`` on ``pytest.ini``. Thanks `@davidszotten`_ for
3031
the PR (`#1952`_).
31-
* Change exception raised by ``capture.DontReadFromInput.fileno()`` from ``ValueError``
32+
* Change exception raised by ``capture.DontReadFromInput.fileno()`` from ``ValueError``
3233
to ``io.UnsupportedOperation``. Thanks `@vlad-dragos`_ for the PR.
3334

3435

@@ -38,11 +39,13 @@ Changes
3839
.. _@davidszotten: https://github.com/davidszotten
3940
.. _@fushi: https://github.com/fushi
4041
.. _@mattduck: https://github.com/mattduck
42+
.. _@wheerd: https://github.com/wheerd
4143

4244
.. _#1512: https://github.com/pytest-dev/pytest/issues/1512
4345
.. _#1874: https://github.com/pytest-dev/pytest/pull/1874
4446
.. _#1952: https://github.com/pytest-dev/pytest/pull/1952
4547
.. _#2013: https://github.com/pytest-dev/pytest/issues/2013
48+
.. _#2101: https://github.com/pytest-dev/pytest/pull/2101
4649

4750

4851
3.0.5.dev0

_pytest/doctest.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
def pytest_addoption(parser):
2626
parser.addini('doctest_optionflags', 'option flags for doctests',
2727
type="args", default=["ELLIPSIS"])
28+
parser.addini("doctest_encoding", 'encoding used for doctest files', default="utf-8")
2829
group = parser.getgroup("collect")
2930
group.addoption("--doctest-modules",
3031
action="store_true", default=False,
@@ -162,7 +163,6 @@ def get_optionflags(parent):
162163
flag_acc |= flag_lookup_table[flag]
163164
return flag_acc
164165

165-
166166
class DoctestTextfile(pytest.Module):
167167
obj = None
168168

@@ -171,7 +171,8 @@ def collect(self):
171171

172172
# inspired by doctest.testfile; ideally we would use it directly,
173173
# but it doesn't support passing a custom checker
174-
text = self.fspath.read()
174+
encoding = self.config.getini("doctest_encoding")
175+
text = self.fspath.read_text(encoding)
175176
filename = str(self.fspath)
176177
name = self.fspath.basename
177178
globs = {'__name__': '__main__'}

_pytest/pytester.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ def chdir(self):
471471
if not hasattr(self, '_olddir'):
472472
self._olddir = old
473473

474-
def _makefile(self, ext, args, kwargs):
474+
def _makefile(self, ext, args, kwargs, encoding="utf-8"):
475475
items = list(kwargs.items())
476476
if args:
477477
source = py.builtin._totext("\n").join(
@@ -490,7 +490,7 @@ def my_totext(s, encoding="utf-8"):
490490

491491
source_unicode = "\n".join([my_totext(line) for line in source.lines])
492492
source = py.builtin._totext(source_unicode)
493-
content = source.strip().encode("utf-8") # + "\n"
493+
content = source.strip().encode(encoding) # + "\n"
494494
#content = content.rstrip() + "\n"
495495
p.write(content, "wb")
496496
if ret is None:

doc/en/doctest.rst

+15-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ can change the pattern by issuing::
1111
on the command line. Since version ``2.9``, ``--doctest-glob``
1212
can be given multiple times in the command-line.
1313

14+
.. versionadded:: 3.1
15+
16+
You can specify the encoding that will be used for those doctest files
17+
using the ``doctest_encoding`` ini option:
18+
19+
.. code-block:: ini
20+
21+
# content of pytest.ini
22+
[pytest]
23+
doctest_encoding = latin1
24+
25+
The default encoding is UTF-8.
26+
1427
You can also trigger running of doctests
1528
from docstrings in all python modules (including regular
1629
python test modules)::
@@ -52,9 +65,9 @@ then you can just invoke ``pytest`` without command line options::
5265
platform linux -- Python 3.5.2, pytest-3.0.4, py-1.4.31, pluggy-0.4.0
5366
rootdir: $REGENDOC_TMPDIR, inifile: pytest.ini
5467
collected 1 items
55-
68+
5669
mymodule.py .
57-
70+
5871
======= 1 passed in 0.12 seconds ========
5972

6073
It is possible to use fixtures using the ``getfixture`` helper::

testing/test_doctest.py

+27
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,33 @@ def test_multiple_patterns(self, testdir):
129129
'*1 passed*',
130130
])
131131

132+
@pytest.mark.parametrize(
133+
' test_string, encoding',
134+
[
135+
(u'foo', 'ascii'),
136+
(u'öäü', 'latin1'),
137+
(u'öäü', 'utf-8')
138+
]
139+
)
140+
def test_encoding(self, testdir, test_string, encoding):
141+
"""Test support for doctest_encoding ini option.
142+
"""
143+
testdir.makeini("""
144+
[pytest]
145+
doctest_encoding={0}
146+
""".format(encoding))
147+
doctest = u"""
148+
>>> u"{0}"
149+
{1}
150+
""".format(test_string, repr(test_string))
151+
testdir._makefile(".txt", [doctest], {}, encoding=encoding)
152+
153+
result = testdir.runpytest()
154+
155+
result.stdout.fnmatch_lines([
156+
'*1 passed*',
157+
])
158+
132159
def test_doctest_unexpected_exception(self, testdir):
133160
testdir.maketxtfile("""
134161
>>> i = 0

0 commit comments

Comments
 (0)