Skip to content

Commit

Permalink
Changed the doctest_encoding option to an ini option.
Browse files Browse the repository at this point in the history
Parametrized the tests for it.
  • Loading branch information
Manuel Krebber committed Nov 30, 2016
1 parent 929912d commit c043bbb
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 44 deletions.
9 changes: 3 additions & 6 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 All @@ -43,10 +44,6 @@ 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):
Expand Down Expand Up @@ -166,7 +163,6 @@ def get_optionflags(parent):
flag_acc |= flag_lookup_table[flag]
return flag_acc


class DoctestTextfile(pytest.Module):
obj = None

Expand All @@ -175,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_text(self.config.getoption("doctestencoding"))
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
57 changes: 19 additions & 38 deletions testing/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,48 +129,29 @@ def test_multiple_patterns(self, testdir):
'*1 passed*',
])

def test_encoding_ascii(self, testdir):
"""Test support for --doctest-encoding option.
@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._makefile(".txt", ["""
>>> 1
1
"""], {}, encoding='ascii')

result = testdir.runpytest("--doctest-encoding=ascii")

result.stdout.fnmatch_lines([
'*1 passed*',
])

def test_encoding_latin1(self, testdir):
"""Test support for --doctest-encoding option.
"""
testdir._makefile(".txt", [u"""
>>> len(u'üäö')
3
"""], {}, encoding='latin1')

result = testdir.runpytest("--doctest-encoding=latin1")

result.stdout.fnmatch_lines([
'*1 passed*',
])

def test_encoding_utf8(self, testdir):
"""Test support for --doctest-encoding option.
"""
testdir.maketxtfile(u"""
>>> len(u'üäö')
3
""")
testdir.makeini("""
[pytest]
doctest_encoding={0}
""".format(encoding))
doctest = u"""
>>> u"{}"
{}
""".format(test_string, repr(test_string))
testdir._makefile(".txt", [doctest], {}, encoding=encoding)

result = testdir.runpytest()
result.stdout.fnmatch_lines([
'*1 passed*',
])

result = testdir.runpytest("--doctest-encoding=utf-8")
result.stdout.fnmatch_lines([
'*1 passed*',
])
Expand Down

0 comments on commit c043bbb

Please sign in to comment.