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):
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,
@ -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):
@ -166,7 +163,6 @@ def get_optionflags(parent):
flag_acc |= flag_lookup_table[flag]
return flag_acc
class DoctestTextfile(pytest.Module):
obj = None
@ -175,7 +171,8 @@ 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.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__'}

View File

@ -129,48 +129,29 @@ class TestDoctests:
'*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*',
])