From c043bbb8545eecf991cda9bfab95b78bae9ecd08 Mon Sep 17 00:00:00 2001 From: Manuel Krebber Date: Wed, 30 Nov 2016 11:43:33 +0100 Subject: [PATCH] Changed the doctest_encoding option to an ini option. Parametrized the tests for it. --- _pytest/doctest.py | 9 +++---- testing/test_doctest.py | 57 ++++++++++++++--------------------------- 2 files changed, 22 insertions(+), 44 deletions(-) diff --git a/_pytest/doctest.py b/_pytest/doctest.py index 05acc7c59..4ee21b12d 100644 --- a/_pytest/doctest.py +++ b/_pytest/doctest.py @@ -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__'} diff --git a/testing/test_doctest.py b/testing/test_doctest.py index d48e58042..02f4c3b26 100644 --- a/testing/test_doctest.py +++ b/testing/test_doctest.py @@ -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*', ])