Changed the doctest_encoding option to an ini option.

Parametrized the tests for it.
This commit is contained in:
Manuel Krebber 2016-11-30 11:43:33 +01:00
parent 929912de29
commit c043bbb854
2 changed files with 22 additions and 44 deletions

View File

@ -25,6 +25,7 @@ DOCTEST_REPORT_CHOICES = (
def pytest_addoption(parser): def pytest_addoption(parser):
parser.addini('doctest_optionflags', 'option flags for doctests', parser.addini('doctest_optionflags', 'option flags for doctests',
type="args", default=["ELLIPSIS"]) type="args", default=["ELLIPSIS"])
parser.addini("doctest_encoding", 'encoding used for doctest files', default="utf-8")
group = parser.getgroup("collect") group = parser.getgroup("collect")
group.addoption("--doctest-modules", group.addoption("--doctest-modules",
action="store_true", default=False, action="store_true", default=False,
@ -43,10 +44,6 @@ 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):
@ -166,7 +163,6 @@ def get_optionflags(parent):
flag_acc |= flag_lookup_table[flag] flag_acc |= flag_lookup_table[flag]
return flag_acc return flag_acc
class DoctestTextfile(pytest.Module): class DoctestTextfile(pytest.Module):
obj = None obj = None
@ -175,7 +171,8 @@ 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.config.getoption("doctestencoding")) encoding = self.config.getini("doctest_encoding")
text = self.fspath.read_text(encoding)
filename = str(self.fspath) filename = str(self.fspath)
name = self.fspath.basename name = self.fspath.basename
globs = {'__name__': '__main__'} globs = {'__name__': '__main__'}

View File

@ -129,48 +129,29 @@ class TestDoctests:
'*1 passed*', '*1 passed*',
]) ])
def test_encoding_ascii(self, testdir): @pytest.mark.parametrize(
"""Test support for --doctest-encoding option. ' 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", [""" testdir.makeini("""
>>> 1 [pytest]
1 doctest_encoding={0}
"""], {}, encoding='ascii') """.format(encoding))
doctest = u"""
result = testdir.runpytest("--doctest-encoding=ascii") >>> u"{}"
{}
result.stdout.fnmatch_lines([ """.format(test_string, repr(test_string))
'*1 passed*', testdir._makefile(".txt", [doctest], {}, encoding=encoding)
])
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
""")
result = testdir.runpytest() result = testdir.runpytest()
result.stdout.fnmatch_lines([
'*1 passed*',
])
result = testdir.runpytest("--doctest-encoding=utf-8")
result.stdout.fnmatch_lines([ result.stdout.fnmatch_lines([
'*1 passed*', '*1 passed*',
]) ])