1
+ # encoding: utf-8
1
2
import sys
2
3
from _pytest .doctest import DoctestItem , DoctestModule , DoctestTextfile
3
4
import py
@@ -109,6 +110,43 @@ def test_doctest_unexpected_exception(self, testdir):
109
110
"*UNEXPECTED*ZeroDivision*" ,
110
111
])
111
112
113
+ def test_docstring_context_around_error (self , testdir ):
114
+ """Test that we show some context before the actual line of a failing
115
+ doctest.
116
+ """
117
+ testdir .makepyfile ('''
118
+ def foo():
119
+ """
120
+ text-line-1
121
+ text-line-2
122
+ text-line-3
123
+ text-line-4
124
+ text-line-5
125
+ text-line-6
126
+ text-line-7
127
+ text-line-8
128
+ text-line-9
129
+ text-line-10
130
+ text-line-11
131
+ >>> 1 + 1
132
+ 3
133
+ """
134
+ ''' )
135
+ result = testdir .runpytest ('--doctest-modules' )
136
+ result .stdout .fnmatch_lines ([
137
+ '*docstring_context_around_error*' ,
138
+ '005*text-line-3' ,
139
+ '006*text-line-4' ,
140
+ '013*text-line-11' ,
141
+ '014*>>> 1 + 1' ,
142
+ 'Expected:' ,
143
+ ' 3' ,
144
+ 'Got:' ,
145
+ ' 2' ,
146
+ ])
147
+ # line containing "text-line-2" should have been trimmed out
148
+ assert 'text-line-2' not in result .stdout .str ()
149
+
112
150
def test_doctest_linedata_missing (self , testdir ):
113
151
testdir .tmpdir .join ('hello.py' ).write (py .code .Source ("""
114
152
class Fun(object):
@@ -339,6 +377,23 @@ def test_non_ignored_whitespace_glob(self, testdir):
339
377
reprec = testdir .inline_run (p , "--doctest-glob=x*.txt" )
340
378
reprec .assertoutcome (failed = 1 , passed = 0 )
341
379
380
+ def test_contains_unicode (self , testdir ):
381
+ """Fix internal error with docstrings containing non-ascii characters.
382
+ """
383
+ testdir .makepyfile (u'''
384
+ # encoding: utf-8
385
+ def foo():
386
+ """
387
+ >>> name = 'с' # not letter 'c' but instead Cyrillic 's'.
388
+ 'anything'
389
+ """
390
+ ''' )
391
+ result = testdir .runpytest ('--doctest-modules' )
392
+ result .stdout .fnmatch_lines ([
393
+ 'Got nothing' ,
394
+ '* 1 failed in*' ,
395
+ ])
396
+
342
397
def test_ignore_import_errors_on_doctest (self , testdir ):
343
398
p = testdir .makepyfile ("""
344
399
import asdf
@@ -579,4 +634,4 @@ def auto(request):
579
634
""" )
580
635
result = testdir .runpytest ('--doctest-modules' )
581
636
assert 'FAILURES' not in str (result .stdout .str ())
582
- result .stdout .fnmatch_lines (['*=== 1 passed in *' ])
637
+ result .stdout .fnmatch_lines (['*=== 1 passed in *' ])
0 commit comments