Merge pull request #2101 from wheerd/doctest-encoding

Added doctest encoding command line option
This commit is contained in:
Bruno Oliveira 2016-11-30 17:43:42 -02:00 committed by GitHub
commit 669332b7e0
6 changed files with 54 additions and 9 deletions

View File

@ -86,6 +86,7 @@ Lukas Bednar
Luke Murphy
Maciek Fijalkowski
Maho
Manuel Krebber
Marc Schlaich
Marcin Bachry
Mark Abramowitz

View File

@ -5,7 +5,8 @@
New Features
------------
*
* Added an ini option ``doctest_encoding`` to specify which encoding to use for doctest files.
Thanks `@wheerd`_ for the PR (`#2101`_).
*
@ -25,10 +26,10 @@ Changes
assertion messages if verbosity < 2 (`#1512`_).
Thanks `@mattduck`_ for the PR
* ``--pdbcls`` no longer implies ``--pdb``. This makes it possible to use
* ``--pdbcls`` no longer implies ``--pdb``. This makes it possible to use
``addopts=--pdbcls=module.SomeClass`` on ``pytest.ini``. Thanks `@davidszotten`_ for
the PR (`#1952`_).
* Change exception raised by ``capture.DontReadFromInput.fileno()`` from ``ValueError``
* Change exception raised by ``capture.DontReadFromInput.fileno()`` from ``ValueError``
to ``io.UnsupportedOperation``. Thanks `@vlad-dragos`_ for the PR.
@ -38,11 +39,13 @@ Changes
.. _@davidszotten: https://github.com/davidszotten
.. _@fushi: https://github.com/fushi
.. _@mattduck: https://github.com/mattduck
.. _@wheerd: https://github.com/wheerd
.. _#1512: https://github.com/pytest-dev/pytest/issues/1512
.. _#1874: https://github.com/pytest-dev/pytest/pull/1874
.. _#1952: https://github.com/pytest-dev/pytest/pull/1952
.. _#2013: https://github.com/pytest-dev/pytest/issues/2013
.. _#2101: https://github.com/pytest-dev/pytest/pull/2101
3.0.5.dev0

View File

@ -25,6 +25,7 @@ DOCTEST_REPORT_CHOICES = (
def pytest_addoption(parser):
parser.addini('doctest_optionflags', 'option flags for doctests',
type="args", default=["ELLIPSIS"])
parser.addini("doctest_encoding", 'encoding used for doctest files', default="utf-8")
group = parser.getgroup("collect")
group.addoption("--doctest-modules",
action="store_true", default=False,
@ -162,7 +163,6 @@ def get_optionflags(parent):
flag_acc |= flag_lookup_table[flag]
return flag_acc
class DoctestTextfile(pytest.Module):
obj = None
@ -171,7 +171,8 @@ class DoctestTextfile(pytest.Module):
# inspired by doctest.testfile; ideally we would use it directly,
# but it doesn't support passing a custom checker
text = self.fspath.read()
encoding = self.config.getini("doctest_encoding")
text = self.fspath.read_text(encoding)
filename = str(self.fspath)
name = self.fspath.basename
globs = {'__name__': '__main__'}

View File

@ -471,7 +471,7 @@ class Testdir:
if not hasattr(self, '_olddir'):
self._olddir = old
def _makefile(self, ext, args, kwargs):
def _makefile(self, ext, args, kwargs, encoding="utf-8"):
items = list(kwargs.items())
if args:
source = py.builtin._totext("\n").join(
@ -490,7 +490,7 @@ class Testdir:
source_unicode = "\n".join([my_totext(line) for line in source.lines])
source = py.builtin._totext(source_unicode)
content = source.strip().encode("utf-8") # + "\n"
content = source.strip().encode(encoding) # + "\n"
#content = content.rstrip() + "\n"
p.write(content, "wb")
if ret is None:

View File

@ -11,6 +11,19 @@ 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.
.. versionadded:: 3.1
You can specify the encoding that will be used for those doctest files
using the ``doctest_encoding`` ini option:
.. code-block:: ini
# content of pytest.ini
[pytest]
doctest_encoding = latin1
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 +65,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::

View File

@ -129,6 +129,33 @@ class TestDoctests:
'*1 passed*',
])
@pytest.mark.parametrize(
' test_string, encoding',
[
(u'foo', 'ascii'),
(u'öäü', 'latin1'),
(u'öäü', 'utf-8')
]
)
def test_encoding(self, testdir, test_string, encoding):
"""Test support for doctest_encoding ini option.
"""
testdir.makeini("""
[pytest]
doctest_encoding={0}
""".format(encoding))
doctest = u"""
>>> u"{0}"
{1}
""".format(test_string, repr(test_string))
testdir._makefile(".txt", [doctest], {}, encoding=encoding)
result = testdir.runpytest()
result.stdout.fnmatch_lines([
'*1 passed*',
])
def test_doctest_unexpected_exception(self, testdir):
testdir.maketxtfile("""
>>> i = 0