2016-01-09 08:19:37 +08:00
|
|
|
|
# encoding: utf-8
|
2017-03-17 09:21:30 +08:00
|
|
|
|
from __future__ import absolute_import, division, print_function
|
2015-08-13 08:41:31 +08:00
|
|
|
|
import sys
|
2015-11-27 22:43:01 +08:00
|
|
|
|
import _pytest._code
|
2016-12-28 08:01:22 +08:00
|
|
|
|
from _pytest.compat import MODULE_NOT_FOUND_ERROR
|
2013-05-17 23:18:22 +08:00
|
|
|
|
from _pytest.doctest import DoctestItem, DoctestModule, DoctestTextfile
|
2015-08-13 08:41:31 +08:00
|
|
|
|
import pytest
|
2009-09-06 22:59:39 +08:00
|
|
|
|
|
2016-12-13 20:49:44 +08:00
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
|
class TestDoctests(object):
|
2010-07-27 03:15:15 +08:00
|
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
|
def test_collect_testtextfile(self, testdir):
|
2010-11-17 21:33:21 +08:00
|
|
|
|
w = testdir.maketxtfile(whatever="")
|
2009-09-06 22:59:39 +08:00
|
|
|
|
checkfile = testdir.maketxtfile(test_something="""
|
|
|
|
|
alskdjalsdk
|
|
|
|
|
>>> i = 5
|
|
|
|
|
>>> i-1
|
|
|
|
|
4
|
|
|
|
|
""")
|
2016-06-02 00:16:05 +08:00
|
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
|
for x in (testdir.tmpdir, checkfile):
|
2017-07-17 07:25:09 +08:00
|
|
|
|
# print "checking that %s returns custom items" % (x,)
|
2010-01-03 06:30:46 +08:00
|
|
|
|
items, reprec = testdir.inline_genitems(x)
|
2009-09-06 22:59:39 +08:00
|
|
|
|
assert len(items) == 1
|
2016-06-02 00:16:05 +08:00
|
|
|
|
assert isinstance(items[0], DoctestItem)
|
|
|
|
|
assert isinstance(items[0].parent, DoctestTextfile)
|
|
|
|
|
# Empty file has no items.
|
2010-11-17 21:33:21 +08:00
|
|
|
|
items, reprec = testdir.inline_genitems(w)
|
2016-06-02 00:16:05 +08:00
|
|
|
|
assert len(items) == 0
|
2009-09-06 22:59:39 +08:00
|
|
|
|
|
2013-05-17 23:18:22 +08:00
|
|
|
|
def test_collect_module_empty(self, testdir):
|
2009-09-06 22:59:39 +08:00
|
|
|
|
path = testdir.makepyfile(whatever="#")
|
2013-05-17 23:18:22 +08:00
|
|
|
|
for p in (path, testdir.tmpdir):
|
|
|
|
|
items, reprec = testdir.inline_genitems(p,
|
2017-07-17 07:25:07 +08:00
|
|
|
|
'--doctest-modules')
|
2013-05-17 23:18:22 +08:00
|
|
|
|
assert len(items) == 0
|
|
|
|
|
|
|
|
|
|
def test_collect_module_single_modulelevel_doctest(self, testdir):
|
|
|
|
|
path = testdir.makepyfile(whatever='""">>> pass"""')
|
2010-07-27 03:15:15 +08:00
|
|
|
|
for p in (path, testdir.tmpdir):
|
|
|
|
|
items, reprec = testdir.inline_genitems(p,
|
2017-07-17 07:25:07 +08:00
|
|
|
|
'--doctest-modules')
|
2009-09-06 22:59:39 +08:00
|
|
|
|
assert len(items) == 1
|
2013-05-17 23:18:22 +08:00
|
|
|
|
assert isinstance(items[0], DoctestItem)
|
|
|
|
|
assert isinstance(items[0].parent, DoctestModule)
|
|
|
|
|
|
|
|
|
|
def test_collect_module_two_doctest_one_modulelevel(self, testdir):
|
|
|
|
|
path = testdir.makepyfile(whatever="""
|
|
|
|
|
'>>> x = None'
|
|
|
|
|
def my_func():
|
|
|
|
|
">>> magic = 42 "
|
|
|
|
|
""")
|
|
|
|
|
for p in (path, testdir.tmpdir):
|
|
|
|
|
items, reprec = testdir.inline_genitems(p,
|
2017-07-17 07:25:07 +08:00
|
|
|
|
'--doctest-modules')
|
2013-05-17 23:18:22 +08:00
|
|
|
|
assert len(items) == 2
|
|
|
|
|
assert isinstance(items[0], DoctestItem)
|
|
|
|
|
assert isinstance(items[1], DoctestItem)
|
|
|
|
|
assert isinstance(items[0].parent, DoctestModule)
|
|
|
|
|
assert items[0].parent is items[1].parent
|
|
|
|
|
|
|
|
|
|
def test_collect_module_two_doctest_no_modulelevel(self, testdir):
|
|
|
|
|
path = testdir.makepyfile(whatever="""
|
|
|
|
|
'# Empty'
|
|
|
|
|
def my_func():
|
|
|
|
|
">>> magic = 42 "
|
|
|
|
|
def unuseful():
|
|
|
|
|
'''
|
|
|
|
|
# This is a function
|
|
|
|
|
# >>> # it doesn't have any doctest
|
|
|
|
|
'''
|
|
|
|
|
def another():
|
|
|
|
|
'''
|
|
|
|
|
# This is another function
|
|
|
|
|
>>> import os # this one does have a doctest
|
|
|
|
|
'''
|
|
|
|
|
""")
|
|
|
|
|
for p in (path, testdir.tmpdir):
|
|
|
|
|
items, reprec = testdir.inline_genitems(p,
|
2017-07-17 07:25:07 +08:00
|
|
|
|
'--doctest-modules')
|
2013-05-17 23:18:22 +08:00
|
|
|
|
assert len(items) == 2
|
|
|
|
|
assert isinstance(items[0], DoctestItem)
|
|
|
|
|
assert isinstance(items[1], DoctestItem)
|
|
|
|
|
assert isinstance(items[0].parent, DoctestModule)
|
|
|
|
|
assert items[0].parent is items[1].parent
|
2009-09-06 22:59:39 +08:00
|
|
|
|
|
|
|
|
|
def test_simple_doctestfile(self, testdir):
|
|
|
|
|
p = testdir.maketxtfile(test_doc="""
|
|
|
|
|
>>> x = 1
|
|
|
|
|
>>> x == 1
|
|
|
|
|
False
|
|
|
|
|
""")
|
2010-01-03 06:30:46 +08:00
|
|
|
|
reprec = testdir.inline_run(p, )
|
|
|
|
|
reprec.assertoutcome(failed=1)
|
|
|
|
|
|
|
|
|
|
def test_new_pattern(self, testdir):
|
2015-12-31 04:19:08 +08:00
|
|
|
|
p = testdir.maketxtfile(xdoc="""
|
2010-01-03 06:30:46 +08:00
|
|
|
|
>>> x = 1
|
|
|
|
|
>>> x == 1
|
|
|
|
|
False
|
|
|
|
|
""")
|
|
|
|
|
reprec = testdir.inline_run(p, "--doctest-glob=x*.txt")
|
2009-09-06 22:59:39 +08:00
|
|
|
|
reprec.assertoutcome(failed=1)
|
|
|
|
|
|
2015-12-31 04:19:08 +08:00
|
|
|
|
def test_multiple_patterns(self, testdir):
|
|
|
|
|
"""Test support for multiple --doctest-glob arguments (#1255).
|
|
|
|
|
"""
|
|
|
|
|
testdir.maketxtfile(xdoc="""
|
|
|
|
|
>>> 1
|
|
|
|
|
1
|
|
|
|
|
""")
|
|
|
|
|
testdir.makefile('.foo', test="""
|
|
|
|
|
>>> 1
|
|
|
|
|
1
|
|
|
|
|
""")
|
|
|
|
|
testdir.maketxtfile(test_normal="""
|
|
|
|
|
>>> 1
|
|
|
|
|
1
|
|
|
|
|
""")
|
|
|
|
|
expected = set(['xdoc.txt', 'test.foo', 'test_normal.txt'])
|
|
|
|
|
assert set(x.basename for x in testdir.tmpdir.listdir()) == expected
|
|
|
|
|
args = ["--doctest-glob=xdoc*.txt", "--doctest-glob=*.foo"]
|
|
|
|
|
result = testdir.runpytest(*args)
|
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
|
'*test.foo *',
|
|
|
|
|
'*xdoc.txt *',
|
|
|
|
|
'*2 passed*',
|
|
|
|
|
])
|
|
|
|
|
result = testdir.runpytest()
|
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
|
'*test_normal.txt *',
|
|
|
|
|
'*1 passed*',
|
|
|
|
|
])
|
|
|
|
|
|
2016-11-30 18:43:33 +08:00
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
|
' test_string, encoding',
|
|
|
|
|
[
|
2017-07-17 07:25:08 +08:00
|
|
|
|
(u'foo', 'ascii'),
|
|
|
|
|
(u'öäü', 'latin1'),
|
|
|
|
|
(u'öäü', 'utf-8')
|
2016-11-30 18:43:33 +08:00
|
|
|
|
]
|
|
|
|
|
)
|
|
|
|
|
def test_encoding(self, testdir, test_string, encoding):
|
|
|
|
|
"""Test support for doctest_encoding ini option.
|
2016-11-29 19:18:41 +08:00
|
|
|
|
"""
|
2016-11-30 18:43:33 +08:00
|
|
|
|
testdir.makeini("""
|
|
|
|
|
[pytest]
|
|
|
|
|
doctest_encoding={0}
|
|
|
|
|
""".format(encoding))
|
|
|
|
|
doctest = u"""
|
2016-11-30 21:19:07 +08:00
|
|
|
|
>>> u"{0}"
|
|
|
|
|
{1}
|
2016-11-30 18:43:33 +08:00
|
|
|
|
""".format(test_string, repr(test_string))
|
|
|
|
|
testdir._makefile(".txt", [doctest], {}, encoding=encoding)
|
2016-11-29 19:18:41 +08:00
|
|
|
|
|
|
|
|
|
result = testdir.runpytest()
|
|
|
|
|
|
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
|
'*1 passed*',
|
|
|
|
|
])
|
|
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
|
def test_doctest_unexpected_exception(self, testdir):
|
2013-10-12 21:39:22 +08:00
|
|
|
|
testdir.maketxtfile("""
|
2009-09-06 22:59:39 +08:00
|
|
|
|
>>> i = 0
|
2010-12-07 02:00:30 +08:00
|
|
|
|
>>> 0 / i
|
2009-09-06 22:59:39 +08:00
|
|
|
|
2
|
|
|
|
|
""")
|
2010-12-07 02:00:30 +08:00
|
|
|
|
result = testdir.runpytest("--doctest-modules")
|
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
|
"*unexpected_exception*",
|
|
|
|
|
"*>>> i = 0*",
|
|
|
|
|
"*>>> 0 / i*",
|
|
|
|
|
"*UNEXPECTED*ZeroDivision*",
|
|
|
|
|
])
|
2009-09-06 22:59:39 +08:00
|
|
|
|
|
2016-01-09 08:19:37 +08:00
|
|
|
|
def test_docstring_context_around_error(self, testdir):
|
|
|
|
|
"""Test that we show some context before the actual line of a failing
|
|
|
|
|
doctest.
|
|
|
|
|
"""
|
|
|
|
|
testdir.makepyfile('''
|
|
|
|
|
def foo():
|
|
|
|
|
"""
|
|
|
|
|
text-line-1
|
|
|
|
|
text-line-2
|
|
|
|
|
text-line-3
|
|
|
|
|
text-line-4
|
|
|
|
|
text-line-5
|
|
|
|
|
text-line-6
|
|
|
|
|
text-line-7
|
|
|
|
|
text-line-8
|
|
|
|
|
text-line-9
|
|
|
|
|
text-line-10
|
|
|
|
|
text-line-11
|
|
|
|
|
>>> 1 + 1
|
|
|
|
|
3
|
|
|
|
|
|
|
|
|
|
text-line-after
|
|
|
|
|
"""
|
|
|
|
|
''')
|
|
|
|
|
result = testdir.runpytest('--doctest-modules')
|
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
|
'*docstring_context_around_error*',
|
|
|
|
|
'005*text-line-3',
|
|
|
|
|
'006*text-line-4',
|
|
|
|
|
'013*text-line-11',
|
|
|
|
|
'014*>>> 1 + 1',
|
|
|
|
|
'Expected:',
|
|
|
|
|
' 3',
|
|
|
|
|
'Got:',
|
|
|
|
|
' 2',
|
|
|
|
|
])
|
|
|
|
|
# lines below should be trimmed out
|
|
|
|
|
assert 'text-line-2' not in result.stdout.str()
|
|
|
|
|
assert 'text-line-after' not in result.stdout.str()
|
|
|
|
|
|
2013-03-25 03:05:29 +08:00
|
|
|
|
def test_doctest_linedata_missing(self, testdir):
|
2015-11-27 22:43:01 +08:00
|
|
|
|
testdir.tmpdir.join('hello.py').write(_pytest._code.Source("""
|
2013-03-25 03:05:29 +08:00
|
|
|
|
class Fun(object):
|
|
|
|
|
@property
|
|
|
|
|
def test(self):
|
|
|
|
|
'''
|
|
|
|
|
>>> a = 1
|
|
|
|
|
>>> 1/0
|
|
|
|
|
'''
|
|
|
|
|
"""))
|
|
|
|
|
result = testdir.runpytest("--doctest-modules")
|
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
|
"*hello*",
|
|
|
|
|
"*EXAMPLE LOCATION UNKNOWN, not showing all tests of that example*",
|
|
|
|
|
"*1/0*",
|
|
|
|
|
"*UNEXPECTED*ZeroDivision*",
|
|
|
|
|
"*1 failed*",
|
|
|
|
|
])
|
|
|
|
|
|
2016-06-20 21:05:50 +08:00
|
|
|
|
def test_doctest_unex_importerror_only_txt(self, testdir):
|
|
|
|
|
testdir.maketxtfile("""
|
|
|
|
|
>>> import asdalsdkjaslkdjasd
|
|
|
|
|
>>>
|
|
|
|
|
""")
|
|
|
|
|
result = testdir.runpytest()
|
|
|
|
|
# doctest is never executed because of error during hello.py collection
|
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
|
"*>>> import asdals*",
|
2016-12-13 20:49:44 +08:00
|
|
|
|
"*UNEXPECTED*{e}*".format(e=MODULE_NOT_FOUND_ERROR),
|
|
|
|
|
"{e}: No module named *asdal*".format(e=MODULE_NOT_FOUND_ERROR),
|
2016-06-20 21:05:50 +08:00
|
|
|
|
])
|
2013-03-25 03:05:29 +08:00
|
|
|
|
|
2016-06-20 21:05:50 +08:00
|
|
|
|
def test_doctest_unex_importerror_with_module(self, testdir):
|
2015-11-27 22:43:01 +08:00
|
|
|
|
testdir.tmpdir.join("hello.py").write(_pytest._code.Source("""
|
2011-05-28 20:38:15 +08:00
|
|
|
|
import asdalsdkjaslkdjasd
|
|
|
|
|
"""))
|
2013-10-12 21:39:22 +08:00
|
|
|
|
testdir.maketxtfile("""
|
2011-05-28 20:38:15 +08:00
|
|
|
|
>>> import hello
|
|
|
|
|
>>>
|
|
|
|
|
""")
|
|
|
|
|
result = testdir.runpytest("--doctest-modules")
|
2016-06-20 21:05:50 +08:00
|
|
|
|
# doctest is never executed because of error during hello.py collection
|
2011-05-28 20:38:15 +08:00
|
|
|
|
result.stdout.fnmatch_lines([
|
2016-06-20 21:05:50 +08:00
|
|
|
|
"*ERROR collecting hello.py*",
|
2016-12-13 20:49:44 +08:00
|
|
|
|
"*{e}: No module named *asdals*".format(e=MODULE_NOT_FOUND_ERROR),
|
2016-06-20 21:05:50 +08:00
|
|
|
|
"*Interrupted: 1 errors during collection*",
|
2011-05-28 20:38:15 +08:00
|
|
|
|
])
|
|
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
|
def test_doctestmodule(self, testdir):
|
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
|
'''
|
|
|
|
|
>>> x = 1
|
|
|
|
|
>>> x == 1
|
|
|
|
|
False
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
""")
|
2010-01-03 06:30:46 +08:00
|
|
|
|
reprec = testdir.inline_run(p, "--doctest-modules")
|
2010-07-27 03:15:15 +08:00
|
|
|
|
reprec.assertoutcome(failed=1)
|
2009-09-06 22:59:39 +08:00
|
|
|
|
|
2010-09-04 15:21:35 +08:00
|
|
|
|
def test_doctestmodule_external_and_issue116(self, testdir):
|
|
|
|
|
p = testdir.mkpydir("hello")
|
2015-11-27 22:43:01 +08:00
|
|
|
|
p.join("__init__.py").write(_pytest._code.Source("""
|
2009-09-06 22:59:39 +08:00
|
|
|
|
def somefunc():
|
|
|
|
|
'''
|
|
|
|
|
>>> i = 0
|
|
|
|
|
>>> i + 1
|
|
|
|
|
2
|
|
|
|
|
'''
|
2010-09-04 15:21:35 +08:00
|
|
|
|
"""))
|
2010-01-03 06:30:46 +08:00
|
|
|
|
result = testdir.runpytest(p, "--doctest-modules")
|
2009-09-06 22:59:39 +08:00
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
|
'004 *>>> i = 0',
|
|
|
|
|
'005 *>>> i + 1',
|
|
|
|
|
'*Expected:',
|
|
|
|
|
"* 2",
|
|
|
|
|
"*Got:",
|
|
|
|
|
"* 1",
|
|
|
|
|
"*:5: DocTestFailure"
|
|
|
|
|
])
|
2010-07-27 03:15:15 +08:00
|
|
|
|
|
2009-09-06 22:59:39 +08:00
|
|
|
|
def test_txtfile_failing(self, testdir):
|
|
|
|
|
p = testdir.maketxtfile("""
|
|
|
|
|
>>> i = 0
|
|
|
|
|
>>> i + 1
|
|
|
|
|
2
|
|
|
|
|
""")
|
2011-05-29 15:21:48 +08:00
|
|
|
|
result = testdir.runpytest(p, "-s")
|
2009-09-06 22:59:39 +08:00
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
|
'001 >>> i = 0',
|
|
|
|
|
'002 >>> i + 1',
|
|
|
|
|
'Expected:',
|
|
|
|
|
" 2",
|
|
|
|
|
"Got:",
|
|
|
|
|
" 1",
|
|
|
|
|
"*test_txtfile_failing.txt:2: DocTestFailure"
|
|
|
|
|
])
|
2013-03-21 00:14:28 +08:00
|
|
|
|
|
2013-03-21 08:03:59 +08:00
|
|
|
|
def test_txtfile_with_fixtures(self, testdir):
|
2013-03-21 00:14:28 +08:00
|
|
|
|
p = testdir.maketxtfile("""
|
2013-03-21 19:04:14 +08:00
|
|
|
|
>>> dir = getfixture('tmpdir')
|
2013-03-21 00:14:28 +08:00
|
|
|
|
>>> type(dir).__name__
|
|
|
|
|
'LocalPath'
|
|
|
|
|
""")
|
|
|
|
|
reprec = testdir.inline_run(p, )
|
|
|
|
|
reprec.assertoutcome(passed=1)
|
2013-03-21 00:32:48 +08:00
|
|
|
|
|
2013-11-22 22:35:20 +08:00
|
|
|
|
def test_txtfile_with_usefixtures_in_ini(self, testdir):
|
|
|
|
|
testdir.makeini("""
|
|
|
|
|
[pytest]
|
|
|
|
|
usefixtures = myfixture
|
|
|
|
|
""")
|
|
|
|
|
testdir.makeconftest("""
|
|
|
|
|
import pytest
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def myfixture(monkeypatch):
|
|
|
|
|
monkeypatch.setenv("HELLO", "WORLD")
|
|
|
|
|
""")
|
|
|
|
|
|
|
|
|
|
p = testdir.maketxtfile("""
|
|
|
|
|
>>> import os
|
|
|
|
|
>>> os.environ["HELLO"]
|
|
|
|
|
'WORLD'
|
|
|
|
|
""")
|
|
|
|
|
reprec = testdir.inline_run(p, )
|
|
|
|
|
reprec.assertoutcome(passed=1)
|
2013-09-06 16:07:06 +08:00
|
|
|
|
|
2013-03-21 08:03:59 +08:00
|
|
|
|
def test_doctestmodule_with_fixtures(self, testdir):
|
2013-03-21 00:32:48 +08:00
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
|
'''
|
2013-03-21 19:04:14 +08:00
|
|
|
|
>>> dir = getfixture('tmpdir')
|
2013-03-21 00:32:48 +08:00
|
|
|
|
>>> type(dir).__name__
|
|
|
|
|
'LocalPath'
|
|
|
|
|
'''
|
|
|
|
|
""")
|
|
|
|
|
reprec = testdir.inline_run(p, "--doctest-modules")
|
|
|
|
|
reprec.assertoutcome(passed=1)
|
2013-05-17 23:18:22 +08:00
|
|
|
|
|
|
|
|
|
def test_doctestmodule_three_tests(self, testdir):
|
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
|
'''
|
|
|
|
|
>>> dir = getfixture('tmpdir')
|
|
|
|
|
>>> type(dir).__name__
|
|
|
|
|
'LocalPath'
|
|
|
|
|
'''
|
|
|
|
|
def my_func():
|
|
|
|
|
'''
|
|
|
|
|
>>> magic = 42
|
|
|
|
|
>>> magic - 42
|
|
|
|
|
0
|
|
|
|
|
'''
|
|
|
|
|
def unuseful():
|
|
|
|
|
pass
|
|
|
|
|
def another():
|
|
|
|
|
'''
|
|
|
|
|
>>> import os
|
|
|
|
|
>>> os is os
|
|
|
|
|
True
|
|
|
|
|
'''
|
|
|
|
|
""")
|
|
|
|
|
reprec = testdir.inline_run(p, "--doctest-modules")
|
|
|
|
|
reprec.assertoutcome(passed=3)
|
|
|
|
|
|
|
|
|
|
def test_doctestmodule_two_tests_one_fail(self, testdir):
|
|
|
|
|
p = testdir.makepyfile("""
|
2017-02-17 02:41:51 +08:00
|
|
|
|
class MyClass(object):
|
2013-05-17 23:18:22 +08:00
|
|
|
|
def bad_meth(self):
|
|
|
|
|
'''
|
|
|
|
|
>>> magic = 42
|
|
|
|
|
>>> magic
|
|
|
|
|
0
|
|
|
|
|
'''
|
|
|
|
|
def nice_meth(self):
|
|
|
|
|
'''
|
|
|
|
|
>>> magic = 42
|
|
|
|
|
>>> magic - 42
|
|
|
|
|
0
|
|
|
|
|
'''
|
|
|
|
|
""")
|
|
|
|
|
reprec = testdir.inline_run(p, "--doctest-modules")
|
|
|
|
|
reprec.assertoutcome(failed=1, passed=1)
|
2014-10-08 20:31:17 +08:00
|
|
|
|
|
|
|
|
|
def test_ignored_whitespace(self, testdir):
|
|
|
|
|
testdir.makeini("""
|
|
|
|
|
[pytest]
|
|
|
|
|
doctest_optionflags = ELLIPSIS NORMALIZE_WHITESPACE
|
|
|
|
|
""")
|
|
|
|
|
p = testdir.makepyfile("""
|
2017-02-17 02:41:51 +08:00
|
|
|
|
class MyClass(object):
|
2014-10-08 20:31:17 +08:00
|
|
|
|
'''
|
|
|
|
|
>>> a = "foo "
|
|
|
|
|
>>> print(a)
|
|
|
|
|
foo
|
|
|
|
|
'''
|
|
|
|
|
pass
|
|
|
|
|
""")
|
|
|
|
|
reprec = testdir.inline_run(p, "--doctest-modules")
|
|
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
|
|
|
|
|
def test_non_ignored_whitespace(self, testdir):
|
|
|
|
|
testdir.makeini("""
|
|
|
|
|
[pytest]
|
|
|
|
|
doctest_optionflags = ELLIPSIS
|
|
|
|
|
""")
|
|
|
|
|
p = testdir.makepyfile("""
|
2017-02-17 02:41:51 +08:00
|
|
|
|
class MyClass(object):
|
2014-10-08 20:31:17 +08:00
|
|
|
|
'''
|
|
|
|
|
>>> a = "foo "
|
|
|
|
|
>>> print(a)
|
|
|
|
|
foo
|
|
|
|
|
'''
|
|
|
|
|
pass
|
|
|
|
|
""")
|
|
|
|
|
reprec = testdir.inline_run(p, "--doctest-modules")
|
|
|
|
|
reprec.assertoutcome(failed=1, passed=0)
|
2014-10-08 21:54:08 +08:00
|
|
|
|
|
|
|
|
|
def test_ignored_whitespace_glob(self, testdir):
|
|
|
|
|
testdir.makeini("""
|
|
|
|
|
[pytest]
|
|
|
|
|
doctest_optionflags = ELLIPSIS NORMALIZE_WHITESPACE
|
|
|
|
|
""")
|
|
|
|
|
p = testdir.maketxtfile(xdoc="""
|
|
|
|
|
>>> a = "foo "
|
|
|
|
|
>>> print(a)
|
|
|
|
|
foo
|
|
|
|
|
""")
|
|
|
|
|
reprec = testdir.inline_run(p, "--doctest-glob=x*.txt")
|
|
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
|
|
|
|
|
def test_non_ignored_whitespace_glob(self, testdir):
|
|
|
|
|
testdir.makeini("""
|
|
|
|
|
[pytest]
|
|
|
|
|
doctest_optionflags = ELLIPSIS
|
|
|
|
|
""")
|
|
|
|
|
p = testdir.maketxtfile(xdoc="""
|
|
|
|
|
>>> a = "foo "
|
|
|
|
|
>>> print(a)
|
|
|
|
|
foo
|
|
|
|
|
""")
|
|
|
|
|
reprec = testdir.inline_run(p, "--doctest-glob=x*.txt")
|
|
|
|
|
reprec.assertoutcome(failed=1, passed=0)
|
2015-02-27 01:39:36 +08:00
|
|
|
|
|
2016-01-09 08:19:37 +08:00
|
|
|
|
def test_contains_unicode(self, testdir):
|
|
|
|
|
"""Fix internal error with docstrings containing non-ascii characters.
|
|
|
|
|
"""
|
|
|
|
|
testdir.makepyfile(u'''
|
|
|
|
|
# encoding: utf-8
|
|
|
|
|
def foo():
|
|
|
|
|
"""
|
|
|
|
|
>>> name = 'с' # not letter 'c' but instead Cyrillic 's'.
|
|
|
|
|
'anything'
|
|
|
|
|
"""
|
|
|
|
|
''')
|
|
|
|
|
result = testdir.runpytest('--doctest-modules')
|
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
|
'Got nothing',
|
|
|
|
|
'* 1 failed in*',
|
|
|
|
|
])
|
|
|
|
|
|
2015-02-27 01:39:36 +08:00
|
|
|
|
def test_ignore_import_errors_on_doctest(self, testdir):
|
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
|
import asdf
|
|
|
|
|
|
|
|
|
|
def add_one(x):
|
|
|
|
|
'''
|
|
|
|
|
>>> add_one(1)
|
|
|
|
|
2
|
|
|
|
|
'''
|
|
|
|
|
return x + 1
|
|
|
|
|
""")
|
|
|
|
|
|
|
|
|
|
reprec = testdir.inline_run(p, "--doctest-modules",
|
|
|
|
|
"--doctest-ignore-import-errors")
|
|
|
|
|
reprec.assertoutcome(skipped=1, failed=1, passed=0)
|
2015-06-17 04:35:31 +08:00
|
|
|
|
|
|
|
|
|
def test_junit_report_for_doctest(self, testdir):
|
2015-06-17 07:42:49 +08:00
|
|
|
|
"""
|
|
|
|
|
#713: Fix --junit-xml option when used with --doctest-modules.
|
|
|
|
|
"""
|
2015-06-17 04:35:31 +08:00
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
|
def foo():
|
|
|
|
|
'''
|
|
|
|
|
>>> 1 + 1
|
|
|
|
|
3
|
|
|
|
|
'''
|
|
|
|
|
pass
|
|
|
|
|
""")
|
|
|
|
|
reprec = testdir.inline_run(p, "--doctest-modules",
|
|
|
|
|
"--junit-xml=junit.xml")
|
|
|
|
|
reprec.assertoutcome(failed=1)
|
2015-07-13 10:43:33 +08:00
|
|
|
|
|
2017-05-26 04:08:32 +08:00
|
|
|
|
def test_unicode_doctest(self, testdir):
|
|
|
|
|
"""
|
|
|
|
|
Test case for issue 2434: DecodeError on Python 2 when doctest contains non-ascii
|
|
|
|
|
characters.
|
|
|
|
|
"""
|
|
|
|
|
p = testdir.maketxtfile(test_unicode_doctest="""
|
|
|
|
|
.. doctest::
|
|
|
|
|
|
|
|
|
|
>>> print(
|
|
|
|
|
... "Hi\\n\\nByé")
|
|
|
|
|
Hi
|
|
|
|
|
...
|
|
|
|
|
Byé
|
|
|
|
|
>>> 1/0 # Byé
|
|
|
|
|
1
|
|
|
|
|
""")
|
|
|
|
|
result = testdir.runpytest(p)
|
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
|
'*UNEXPECTED EXCEPTION: ZeroDivisionError*',
|
|
|
|
|
'*1 failed*',
|
|
|
|
|
])
|
|
|
|
|
|
2017-06-14 06:34:05 +08:00
|
|
|
|
def test_unicode_doctest_module(self, testdir):
|
|
|
|
|
"""
|
|
|
|
|
Test case for issue 2434: DecodeError on Python 2 when doctest docstring
|
|
|
|
|
contains non-ascii characters.
|
|
|
|
|
"""
|
|
|
|
|
p = testdir.makepyfile(test_unicode_doctest_module="""
|
|
|
|
|
# -*- encoding: utf-8 -*-
|
|
|
|
|
from __future__ import unicode_literals
|
2017-07-17 07:25:06 +08:00
|
|
|
|
|
2017-06-14 06:34:05 +08:00
|
|
|
|
def fix_bad_unicode(text):
|
|
|
|
|
'''
|
|
|
|
|
>>> print(fix_bad_unicode('único'))
|
|
|
|
|
único
|
|
|
|
|
'''
|
|
|
|
|
return "único"
|
|
|
|
|
""")
|
|
|
|
|
result = testdir.runpytest(p, '--doctest-modules')
|
|
|
|
|
result.stdout.fnmatch_lines(['* 1 passed *'])
|
|
|
|
|
|
2017-07-25 00:07:45 +08:00
|
|
|
|
def test_reportinfo(self, testdir):
|
|
|
|
|
'''
|
|
|
|
|
Test case to make sure that DoctestItem.reportinfo() returns lineno.
|
|
|
|
|
'''
|
|
|
|
|
p = testdir.makepyfile(test_reportinfo="""
|
|
|
|
|
def foo(x):
|
|
|
|
|
'''
|
|
|
|
|
>>> foo('a')
|
|
|
|
|
'b'
|
|
|
|
|
'''
|
|
|
|
|
return 'c'
|
|
|
|
|
""")
|
|
|
|
|
items, reprec = testdir.inline_genitems(p, '--doctest-modules')
|
|
|
|
|
reportinfo = items[0].reportinfo()
|
|
|
|
|
assert reportinfo[1] == 1
|
|
|
|
|
|
2015-12-30 06:55:19 +08:00
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
|
class TestLiterals(object):
|
2015-12-30 06:55:19 +08:00
|
|
|
|
|
2015-08-13 08:41:31 +08:00
|
|
|
|
@pytest.mark.parametrize('config_mode', ['ini', 'comment'])
|
|
|
|
|
def test_allow_unicode(self, testdir, config_mode):
|
|
|
|
|
"""Test that doctests which output unicode work in all python versions
|
|
|
|
|
tested by pytest when the ALLOW_UNICODE option is used (either in
|
|
|
|
|
the ini file or by an inline comment).
|
|
|
|
|
"""
|
|
|
|
|
if config_mode == 'ini':
|
|
|
|
|
testdir.makeini('''
|
|
|
|
|
[pytest]
|
|
|
|
|
doctest_optionflags = ALLOW_UNICODE
|
|
|
|
|
''')
|
|
|
|
|
comment = ''
|
|
|
|
|
else:
|
|
|
|
|
comment = '#doctest: +ALLOW_UNICODE'
|
|
|
|
|
|
|
|
|
|
testdir.maketxtfile(test_doc="""
|
|
|
|
|
>>> b'12'.decode('ascii') {comment}
|
|
|
|
|
'12'
|
|
|
|
|
""".format(comment=comment))
|
|
|
|
|
testdir.makepyfile(foo="""
|
|
|
|
|
def foo():
|
|
|
|
|
'''
|
|
|
|
|
>>> b'12'.decode('ascii') {comment}
|
|
|
|
|
'12'
|
|
|
|
|
'''
|
|
|
|
|
""".format(comment=comment))
|
|
|
|
|
reprec = testdir.inline_run("--doctest-modules")
|
|
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
|
2015-12-30 06:55:19 +08:00
|
|
|
|
@pytest.mark.parametrize('config_mode', ['ini', 'comment'])
|
|
|
|
|
def test_allow_bytes(self, testdir, config_mode):
|
|
|
|
|
"""Test that doctests which output bytes work in all python versions
|
|
|
|
|
tested by pytest when the ALLOW_BYTES option is used (either in
|
|
|
|
|
the ini file or by an inline comment)(#1287).
|
|
|
|
|
"""
|
|
|
|
|
if config_mode == 'ini':
|
|
|
|
|
testdir.makeini('''
|
|
|
|
|
[pytest]
|
|
|
|
|
doctest_optionflags = ALLOW_BYTES
|
|
|
|
|
''')
|
|
|
|
|
comment = ''
|
|
|
|
|
else:
|
|
|
|
|
comment = '#doctest: +ALLOW_BYTES'
|
|
|
|
|
|
|
|
|
|
testdir.maketxtfile(test_doc="""
|
|
|
|
|
>>> b'foo' {comment}
|
|
|
|
|
'foo'
|
|
|
|
|
""".format(comment=comment))
|
|
|
|
|
testdir.makepyfile(foo="""
|
|
|
|
|
def foo():
|
|
|
|
|
'''
|
|
|
|
|
>>> b'foo' {comment}
|
|
|
|
|
'foo'
|
|
|
|
|
'''
|
|
|
|
|
""".format(comment=comment))
|
|
|
|
|
reprec = testdir.inline_run("--doctest-modules")
|
|
|
|
|
reprec.assertoutcome(passed=2)
|
|
|
|
|
|
2015-08-13 09:46:13 +08:00
|
|
|
|
def test_unicode_string(self, testdir):
|
2015-08-13 08:41:31 +08:00
|
|
|
|
"""Test that doctests which output unicode fail in Python 2 when
|
2015-08-13 09:46:13 +08:00
|
|
|
|
the ALLOW_UNICODE option is not used. The same test should pass
|
|
|
|
|
in Python 3.
|
2015-08-13 08:41:31 +08:00
|
|
|
|
"""
|
|
|
|
|
testdir.maketxtfile(test_doc="""
|
2015-08-13 09:46:13 +08:00
|
|
|
|
>>> b'12'.decode('ascii')
|
2015-08-13 08:41:31 +08:00
|
|
|
|
'12'
|
|
|
|
|
""")
|
|
|
|
|
reprec = testdir.inline_run()
|
2015-08-13 09:46:13 +08:00
|
|
|
|
passed = int(sys.version_info[0] >= 3)
|
|
|
|
|
reprec.assertoutcome(passed=passed, failed=int(not passed))
|
2015-08-13 08:41:31 +08:00
|
|
|
|
|
2015-12-30 06:55:19 +08:00
|
|
|
|
def test_bytes_literal(self, testdir):
|
|
|
|
|
"""Test that doctests which output bytes fail in Python 3 when
|
|
|
|
|
the ALLOW_BYTES option is not used. The same test should pass
|
|
|
|
|
in Python 2 (#1287).
|
|
|
|
|
"""
|
|
|
|
|
testdir.maketxtfile(test_doc="""
|
|
|
|
|
>>> b'foo'
|
|
|
|
|
'foo'
|
|
|
|
|
""")
|
|
|
|
|
reprec = testdir.inline_run()
|
|
|
|
|
passed = int(sys.version_info[0] == 2)
|
|
|
|
|
reprec.assertoutcome(passed=passed, failed=int(not passed))
|
|
|
|
|
|
2015-08-13 08:41:31 +08:00
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
|
class TestDoctestSkips(object):
|
2015-08-26 10:12:02 +08:00
|
|
|
|
"""
|
|
|
|
|
If all examples in a doctest are skipped due to the SKIP option, then
|
|
|
|
|
the tests should be SKIPPED rather than PASSED. (#957)
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(params=['text', 'module'])
|
|
|
|
|
def makedoctest(self, testdir, request):
|
|
|
|
|
def makeit(doctest):
|
|
|
|
|
mode = request.param
|
|
|
|
|
if mode == 'text':
|
|
|
|
|
testdir.maketxtfile(doctest)
|
|
|
|
|
else:
|
|
|
|
|
assert mode == 'module'
|
|
|
|
|
testdir.makepyfile('"""\n%s"""' % doctest)
|
|
|
|
|
|
|
|
|
|
return makeit
|
|
|
|
|
|
|
|
|
|
def test_one_skipped(self, testdir, makedoctest):
|
|
|
|
|
makedoctest("""
|
|
|
|
|
>>> 1 + 1 # doctest: +SKIP
|
|
|
|
|
2
|
|
|
|
|
>>> 2 + 2
|
|
|
|
|
4
|
|
|
|
|
""")
|
|
|
|
|
reprec = testdir.inline_run("--doctest-modules")
|
|
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
|
|
|
|
|
def test_one_skipped_failed(self, testdir, makedoctest):
|
|
|
|
|
makedoctest("""
|
|
|
|
|
>>> 1 + 1 # doctest: +SKIP
|
|
|
|
|
2
|
|
|
|
|
>>> 2 + 2
|
|
|
|
|
200
|
|
|
|
|
""")
|
|
|
|
|
reprec = testdir.inline_run("--doctest-modules")
|
|
|
|
|
reprec.assertoutcome(failed=1)
|
|
|
|
|
|
|
|
|
|
def test_all_skipped(self, testdir, makedoctest):
|
|
|
|
|
makedoctest("""
|
|
|
|
|
>>> 1 + 1 # doctest: +SKIP
|
|
|
|
|
2
|
|
|
|
|
>>> 2 + 2 # doctest: +SKIP
|
|
|
|
|
200
|
|
|
|
|
""")
|
|
|
|
|
reprec = testdir.inline_run("--doctest-modules")
|
|
|
|
|
reprec.assertoutcome(skipped=1)
|
2015-10-02 09:59:37 +08:00
|
|
|
|
|
2016-06-02 00:16:05 +08:00
|
|
|
|
def test_vacuous_all_skipped(self, testdir, makedoctest):
|
|
|
|
|
makedoctest('')
|
|
|
|
|
reprec = testdir.inline_run("--doctest-modules")
|
|
|
|
|
reprec.assertoutcome(passed=0, skipped=0)
|
|
|
|
|
|
2015-10-02 09:59:37 +08:00
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
|
class TestDoctestAutoUseFixtures(object):
|
2015-10-02 09:59:37 +08:00
|
|
|
|
|
|
|
|
|
SCOPES = ['module', 'session', 'class', 'function']
|
|
|
|
|
|
|
|
|
|
def test_doctest_module_session_fixture(self, testdir):
|
|
|
|
|
"""Test that session fixtures are initialized for doctest modules (#768)
|
|
|
|
|
"""
|
|
|
|
|
# session fixture which changes some global data, which will
|
|
|
|
|
# be accessed by doctests in a module
|
|
|
|
|
testdir.makeconftest("""
|
|
|
|
|
import pytest
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
@pytest.yield_fixture(autouse=True, scope='session')
|
|
|
|
|
def myfixture():
|
|
|
|
|
assert not hasattr(sys, 'pytest_session_data')
|
|
|
|
|
sys.pytest_session_data = 1
|
|
|
|
|
yield
|
|
|
|
|
del sys.pytest_session_data
|
|
|
|
|
""")
|
|
|
|
|
testdir.makepyfile(foo="""
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
def foo():
|
|
|
|
|
'''
|
|
|
|
|
>>> assert sys.pytest_session_data == 1
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
def bar():
|
|
|
|
|
'''
|
|
|
|
|
>>> assert sys.pytest_session_data == 1
|
|
|
|
|
'''
|
|
|
|
|
""")
|
|
|
|
|
result = testdir.runpytest("--doctest-modules")
|
|
|
|
|
result.stdout.fnmatch_lines('*2 passed*')
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('scope', SCOPES)
|
|
|
|
|
@pytest.mark.parametrize('enable_doctest', [True, False])
|
|
|
|
|
def test_fixture_scopes(self, testdir, scope, enable_doctest):
|
|
|
|
|
"""Test that auto-use fixtures work properly with doctest modules.
|
|
|
|
|
See #1057 and #1100.
|
|
|
|
|
"""
|
|
|
|
|
testdir.makeconftest('''
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True, scope="{scope}")
|
|
|
|
|
def auto(request):
|
|
|
|
|
return 99
|
|
|
|
|
'''.format(scope=scope))
|
|
|
|
|
testdir.makepyfile(test_1='''
|
|
|
|
|
def test_foo():
|
|
|
|
|
"""
|
|
|
|
|
>>> getfixture('auto') + 1
|
|
|
|
|
100
|
|
|
|
|
"""
|
|
|
|
|
def test_bar():
|
|
|
|
|
assert 1
|
|
|
|
|
''')
|
|
|
|
|
params = ('--doctest-modules',) if enable_doctest else ()
|
|
|
|
|
passes = 3 if enable_doctest else 2
|
|
|
|
|
result = testdir.runpytest(*params)
|
|
|
|
|
result.stdout.fnmatch_lines(['*=== %d passed in *' % passes])
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('scope', SCOPES)
|
|
|
|
|
@pytest.mark.parametrize('autouse', [True, False])
|
|
|
|
|
@pytest.mark.parametrize('use_fixture_in_doctest', [True, False])
|
|
|
|
|
def test_fixture_module_doctest_scopes(self, testdir, scope, autouse,
|
|
|
|
|
use_fixture_in_doctest):
|
|
|
|
|
"""Test that auto-use fixtures work properly with doctest files.
|
|
|
|
|
See #1057 and #1100.
|
|
|
|
|
"""
|
|
|
|
|
testdir.makeconftest('''
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse={autouse}, scope="{scope}")
|
|
|
|
|
def auto(request):
|
|
|
|
|
return 99
|
|
|
|
|
'''.format(scope=scope, autouse=autouse))
|
|
|
|
|
if use_fixture_in_doctest:
|
|
|
|
|
testdir.maketxtfile(test_doc="""
|
|
|
|
|
>>> getfixture('auto')
|
|
|
|
|
99
|
|
|
|
|
""")
|
|
|
|
|
else:
|
|
|
|
|
testdir.maketxtfile(test_doc="""
|
|
|
|
|
>>> 1 + 1
|
|
|
|
|
2
|
|
|
|
|
""")
|
|
|
|
|
result = testdir.runpytest('--doctest-modules')
|
|
|
|
|
assert 'FAILURES' not in str(result.stdout.str())
|
|
|
|
|
result.stdout.fnmatch_lines(['*=== 1 passed in *'])
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('scope', SCOPES)
|
|
|
|
|
def test_auto_use_request_attributes(self, testdir, scope):
|
|
|
|
|
"""Check that all attributes of a request in an autouse fixture
|
|
|
|
|
behave as expected when requested for a doctest item.
|
|
|
|
|
"""
|
|
|
|
|
testdir.makeconftest('''
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True, scope="{scope}")
|
|
|
|
|
def auto(request):
|
|
|
|
|
if "{scope}" == 'module':
|
|
|
|
|
assert request.module is None
|
|
|
|
|
if "{scope}" == 'class':
|
|
|
|
|
assert request.cls is None
|
|
|
|
|
if "{scope}" == 'function':
|
|
|
|
|
assert request.function is None
|
|
|
|
|
return 99
|
|
|
|
|
'''.format(scope=scope))
|
|
|
|
|
testdir.maketxtfile(test_doc="""
|
|
|
|
|
>>> 1 + 1
|
|
|
|
|
2
|
|
|
|
|
""")
|
|
|
|
|
result = testdir.runpytest('--doctest-modules')
|
|
|
|
|
assert 'FAILURES' not in str(result.stdout.str())
|
2015-12-31 04:19:08 +08:00
|
|
|
|
result.stdout.fnmatch_lines(['*=== 1 passed in *'])
|
2016-03-02 20:43:57 +08:00
|
|
|
|
|
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
|
class TestDoctestNamespaceFixture(object):
|
2016-03-02 20:43:57 +08:00
|
|
|
|
|
|
|
|
|
SCOPES = ['module', 'session', 'class', 'function']
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('scope', SCOPES)
|
|
|
|
|
def test_namespace_doctestfile(self, testdir, scope):
|
|
|
|
|
"""
|
|
|
|
|
Check that inserting something into the namespace works in a
|
|
|
|
|
simple text file doctest
|
|
|
|
|
"""
|
|
|
|
|
testdir.makeconftest("""
|
|
|
|
|
import pytest
|
|
|
|
|
import contextlib
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True, scope="{scope}")
|
|
|
|
|
def add_contextlib(doctest_namespace):
|
|
|
|
|
doctest_namespace['cl'] = contextlib
|
|
|
|
|
""".format(scope=scope))
|
|
|
|
|
p = testdir.maketxtfile("""
|
|
|
|
|
>>> print(cl.__name__)
|
|
|
|
|
contextlib
|
|
|
|
|
""")
|
|
|
|
|
reprec = testdir.inline_run(p)
|
|
|
|
|
reprec.assertoutcome(passed=1)
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize('scope', SCOPES)
|
|
|
|
|
def test_namespace_pyfile(self, testdir, scope):
|
|
|
|
|
"""
|
|
|
|
|
Check that inserting something into the namespace works in a
|
|
|
|
|
simple Python file docstring doctest
|
|
|
|
|
"""
|
|
|
|
|
testdir.makeconftest("""
|
|
|
|
|
import pytest
|
|
|
|
|
import contextlib
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(autouse=True, scope="{scope}")
|
|
|
|
|
def add_contextlib(doctest_namespace):
|
|
|
|
|
doctest_namespace['cl'] = contextlib
|
|
|
|
|
""".format(scope=scope))
|
|
|
|
|
p = testdir.makepyfile("""
|
|
|
|
|
def foo():
|
|
|
|
|
'''
|
|
|
|
|
>>> print(cl.__name__)
|
|
|
|
|
contextlib
|
|
|
|
|
'''
|
|
|
|
|
""")
|
|
|
|
|
reprec = testdir.inline_run(p, "--doctest-modules")
|
|
|
|
|
reprec.assertoutcome(passed=1)
|
2016-07-23 21:16:23 +08:00
|
|
|
|
|
|
|
|
|
|
2017-02-17 02:41:51 +08:00
|
|
|
|
class TestDoctestReportingOption(object):
|
2016-07-23 21:16:23 +08:00
|
|
|
|
def _run_doctest_report(self, testdir, format):
|
|
|
|
|
testdir.makepyfile("""
|
|
|
|
|
def foo():
|
|
|
|
|
'''
|
|
|
|
|
>>> foo()
|
|
|
|
|
a b
|
|
|
|
|
0 1 4
|
|
|
|
|
1 2 4
|
|
|
|
|
2 3 6
|
|
|
|
|
'''
|
|
|
|
|
print(' a b\\n'
|
|
|
|
|
'0 1 4\\n'
|
|
|
|
|
'1 2 5\\n'
|
|
|
|
|
'2 3 6')
|
|
|
|
|
""")
|
|
|
|
|
return testdir.runpytest("--doctest-modules", "--doctest-report", format)
|
|
|
|
|
|
2016-07-23 21:21:59 +08:00
|
|
|
|
@pytest.mark.parametrize('format', ['udiff', 'UDIFF', 'uDiFf'])
|
|
|
|
|
def test_doctest_report_udiff(self, testdir, format):
|
2016-07-23 21:16:23 +08:00
|
|
|
|
result = self._run_doctest_report(testdir, format)
|
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
|
' 0 1 4',
|
|
|
|
|
' -1 2 4',
|
|
|
|
|
' +1 2 5',
|
|
|
|
|
' 2 3 6',
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
def test_doctest_report_cdiff(self, testdir):
|
|
|
|
|
result = self._run_doctest_report(testdir, 'cdiff')
|
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
|
' a b',
|
|
|
|
|
' 0 1 4',
|
|
|
|
|
' ! 1 2 4',
|
|
|
|
|
' 2 3 6',
|
|
|
|
|
' --- 1,4 ----',
|
|
|
|
|
' a b',
|
|
|
|
|
' 0 1 4',
|
|
|
|
|
' ! 1 2 5',
|
|
|
|
|
' 2 3 6',
|
|
|
|
|
])
|
|
|
|
|
|
|
|
|
|
def test_doctest_report_ndiff(self, testdir):
|
|
|
|
|
result = self._run_doctest_report(testdir, 'ndiff')
|
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
|
' a b',
|
|
|
|
|
' 0 1 4',
|
|
|
|
|
' - 1 2 4',
|
|
|
|
|
' ? ^',
|
|
|
|
|
' + 1 2 5',
|
|
|
|
|
' ? ^',
|
|
|
|
|
' 2 3 6',
|
|
|
|
|
])
|
|
|
|
|
|
2016-07-23 21:58:13 +08:00
|
|
|
|
@pytest.mark.parametrize('format', ['none', 'only_first_failure'])
|
|
|
|
|
def test_doctest_report_none_or_only_first_failure(self, testdir, format):
|
|
|
|
|
result = self._run_doctest_report(testdir, format)
|
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
|
'Expected:',
|
|
|
|
|
' a b',
|
|
|
|
|
' 0 1 4',
|
|
|
|
|
' 1 2 4',
|
|
|
|
|
' 2 3 6',
|
|
|
|
|
'Got:',
|
|
|
|
|
' a b',
|
|
|
|
|
' 0 1 4',
|
|
|
|
|
' 1 2 5',
|
|
|
|
|
' 2 3 6',
|
|
|
|
|
])
|
2016-07-23 21:16:23 +08:00
|
|
|
|
|
|
|
|
|
def test_doctest_report_invalid(self, testdir):
|
|
|
|
|
result = self._run_doctest_report(testdir, 'obviously_invalid_format')
|
|
|
|
|
result.stderr.fnmatch_lines([
|
|
|
|
|
"*error: argument --doctest-report: invalid choice: 'obviously_invalid_format' (choose from*"
|
|
|
|
|
])
|