diff --git a/_pytest/pytester.py b/_pytest/pytester.py index d19ff1ec6..37348c26e 100644 --- a/_pytest/pytester.py +++ b/_pytest/pytester.py @@ -471,7 +471,7 @@ class Testdir: if not hasattr(self, '_olddir'): self._olddir = old - def _makefile(self, ext, args, kwargs): + def _makefile(self, ext, args, kwargs, encoding="utf-8"): items = list(kwargs.items()) if args: source = py.builtin._totext("\n").join( @@ -490,7 +490,7 @@ class Testdir: source_unicode = "\n".join([my_totext(line) for line in source.lines]) source = py.builtin._totext(source_unicode) - content = source.strip().encode("utf-8") # + "\n" + content = source.strip().encode(encoding) # + "\n" #content = content.rstrip() + "\n" p.write(content, "wb") if ret is None: diff --git a/testing/test_doctest.py b/testing/test_doctest.py index 4ea2cc58e..4ecd5744b 100644 --- a/testing/test_doctest.py +++ b/testing/test_doctest.py @@ -129,6 +129,52 @@ class TestDoctests: '*1 passed*', ]) + def test_encoding_ascii(self, testdir): + """Test support for --doctest-encoding 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", [""" + >>> 'üäö' + 'üäö' + """], {}, 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(""" + >>> 'üäö' + 'üäö' + """) + + result = testdir.runpytest() + result.stdout.fnmatch_lines([ + '*1 passed*', + ]) + + result = testdir.runpytest("--doctest-encoding=utf-8") + result.stdout.fnmatch_lines([ + '*1 passed*', + ]) + def test_doctest_unexpected_exception(self, testdir): testdir.maketxtfile(""" >>> i = 0