From ed977513ec6af2e9015e6f6eee6e6b2a0b48e2c5 Mon Sep 17 00:00:00 2001 From: Manuel Krebber Date: Tue, 29 Nov 2016 11:51:56 +0100 Subject: [PATCH] Added a console option to specify the encoding to use for doctest files. Defaults to UTF-8. --- _pytest/doctest.py | 6 +++++- doc/en/doctest.rst | 11 +++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/_pytest/doctest.py b/_pytest/doctest.py index f4782dded..05acc7c59 100644 --- a/_pytest/doctest.py +++ b/_pytest/doctest.py @@ -43,6 +43,10 @@ def pytest_addoption(parser): action="store_true", default=False, help="ignore doctest ImportErrors", 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): @@ -171,7 +175,7 @@ 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() + text = self.fspath.read_text(self.config.getoption("doctestencoding")) filename = str(self.fspath) name = self.fspath.basename globs = {'__name__': '__main__'} diff --git a/doc/en/doctest.rst b/doc/en/doctest.rst index 5a1122515..93d7c30c4 100644 --- a/doc/en/doctest.rst +++ b/doc/en/doctest.rst @@ -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:: + + 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::