Skip to content

Commit d254c6b

Browse files
author
Manuel Krebber
committed
Added some tests for --docstring-encoding option. Added option to specify encoding for internal testdir._makefile() for the tests.
1 parent ed97751 commit d254c6b

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

Diff for: _pytest/pytester.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ def chdir(self):
471471
if not hasattr(self, '_olddir'):
472472
self._olddir = old
473473

474-
def _makefile(self, ext, args, kwargs):
474+
def _makefile(self, ext, args, kwargs, encoding="utf-8"):
475475
items = list(kwargs.items())
476476
if args:
477477
source = py.builtin._totext("\n").join(
@@ -490,7 +490,7 @@ def my_totext(s, encoding="utf-8"):
490490

491491
source_unicode = "\n".join([my_totext(line) for line in source.lines])
492492
source = py.builtin._totext(source_unicode)
493-
content = source.strip().encode("utf-8") # + "\n"
493+
content = source.strip().encode(encoding) # + "\n"
494494
#content = content.rstrip() + "\n"
495495
p.write(content, "wb")
496496
if ret is None:

Diff for: testing/test_doctest.py

+46
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,52 @@ def test_multiple_patterns(self, testdir):
129129
'*1 passed*',
130130
])
131131

132+
def test_encoding_ascii(self, testdir):
133+
"""Test support for --doctest-encoding option.
134+
"""
135+
testdir._makefile(".txt", ["""
136+
>>> 1
137+
1
138+
"""], {}, encoding='ascii')
139+
140+
result = testdir.runpytest("--doctest-encoding=ascii")
141+
142+
result.stdout.fnmatch_lines([
143+
'*1 passed*',
144+
])
145+
146+
def test_encoding_latin1(self, testdir):
147+
"""Test support for --doctest-encoding option.
148+
"""
149+
testdir._makefile(".txt", ["""
150+
>>> 'üäö'
151+
'üäö'
152+
"""], {}, encoding='latin1')
153+
154+
result = testdir.runpytest("--doctest-encoding=latin1")
155+
156+
result.stdout.fnmatch_lines([
157+
'*1 passed*',
158+
])
159+
160+
def test_encoding_utf8(self, testdir):
161+
"""Test support for --doctest-encoding option.
162+
"""
163+
testdir.maketxtfile("""
164+
>>> 'üäö'
165+
'üäö'
166+
""")
167+
168+
result = testdir.runpytest()
169+
result.stdout.fnmatch_lines([
170+
'*1 passed*',
171+
])
172+
173+
result = testdir.runpytest("--doctest-encoding=utf-8")
174+
result.stdout.fnmatch_lines([
175+
'*1 passed*',
176+
])
177+
132178
def test_doctest_unexpected_exception(self, testdir):
133179
testdir.maketxtfile("""
134180
>>> i = 0

0 commit comments

Comments
 (0)