Added a console option to specify the encoding to use for doctest files. Defaults to UTF-8.

This commit is contained in:
Manuel Krebber 2016-11-29 11:51:56 +01:00
parent 36eb5b36d1
commit ed977513ec
2 changed files with 14 additions and 3 deletions

View File

@ -43,6 +43,10 @@ def pytest_addoption(parser):
action="store_true", default=False, action="store_true", default=False,
help="ignore doctest ImportErrors", help="ignore doctest ImportErrors",
dest="doctest_ignore_import_errors") dest="doctest_ignore_import_errors")
group.addoption("--doctest-encoding",
type=str.lower, default="utf-8",
help="choose the encoding to use for doctest files",
dest="doctestencoding")
def pytest_collect_file(path, parent): def pytest_collect_file(path, parent):
@ -171,7 +175,7 @@ class DoctestTextfile(pytest.Module):
# inspired by doctest.testfile; ideally we would use it directly, # inspired by doctest.testfile; ideally we would use it directly,
# but it doesn't support passing a custom checker # but it doesn't support passing a custom checker
text = self.fspath.read() text = self.fspath.read_text(self.config.getoption("doctestencoding"))
filename = str(self.fspath) filename = str(self.fspath)
name = self.fspath.basename name = self.fspath.basename
globs = {'__name__': '__main__'} globs = {'__name__': '__main__'}

View File

@ -11,6 +11,13 @@ can change the pattern by issuing::
on the command line. Since version ``2.9``, ``--doctest-glob`` on the command line. Since version ``2.9``, ``--doctest-glob``
can be given multiple times in the command-line. 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::
pytest --doctest-encoding='ascii'
The default encoding is UTF-8.
You can also trigger running of doctests You can also trigger running of doctests
from docstrings in all python modules (including regular from docstrings in all python modules (including regular
python test modules):: python test modules)::