Skip to content
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

Merged
merged 8 commits into from
Nov 30, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Lukas Bednar
Luke Murphy
Maciek Fijalkowski
Maho
Manuel Krebber
Marc Schlaich
Marcin Bachry
Mark Abramowitz
Expand Down
9 changes: 6 additions & 3 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`_).

*

Expand All @@ -21,10 +22,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.


Expand All @@ -34,11 +35,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
Expand Down
5 changes: 3 additions & 2 deletions _pytest/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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,
Expand Down Expand Up @@ -162,7 +163,6 @@ def get_optionflags(parent):
flag_acc |= flag_lookup_table[flag]
return flag_acc


class DoctestTextfile(pytest.Module):
obj = None

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

# 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__'}
Expand Down
4 changes: 2 additions & 2 deletions _pytest/pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ def chdir(self):
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(
Expand All @@ -490,7 +490,7 @@ def my_totext(s, encoding="utf-8"):

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:
Expand Down
17 changes: 15 additions & 2 deletions doc/en/doctest.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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)::
Expand Down Expand Up @@ -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::
Expand Down
27 changes: 27 additions & 0 deletions testing/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,33 @@ def test_multiple_patterns(self, testdir):
'*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
Expand Down