Skip to content

Commit

Permalink
Added some tests for --docstring-encoding option. Added option to spe…
Browse files Browse the repository at this point in the history
…cify encoding for internal testdir._makefile() for the tests.
  • Loading branch information
Manuel Krebber committed Nov 29, 2016
1 parent ed97751 commit d254c6b
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
4 changes: 2 additions & 2 deletions _pytest/pytester.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ def chdir(self):
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(
Expand All @@ -490,7 +490,7 @@ def my_totext(s, encoding="utf-8"):

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:
Expand Down
46 changes: 46 additions & 0 deletions testing/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,52 @@ def test_multiple_patterns(self, testdir):
'*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
Expand Down

0 comments on commit d254c6b

Please sign in to comment.