2013-05-17 23:18:22 +08:00
|
|
|
from _pytest.doctest import DoctestItem, DoctestModule, DoctestTextfile
|
2015-04-28 17:54:53 +08:00
|
|
|
import py
|
2009-09-06 22:59:39 +08:00
|
|
|
|
|
|
|
class TestDoctests:
|
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
|
|
|
|
""")
|
2010-07-27 03:15:15 +08:00
|
|
|
for x in (testdir.tmpdir, checkfile):
|
|
|
|
#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
|
|
|
|
assert isinstance(items[0], DoctestTextfile)
|
2010-11-17 21:33:21 +08:00
|
|
|
items, reprec = testdir.inline_genitems(w)
|
|
|
|
assert len(items) == 1
|
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,
|
|
|
|
'--doctest-modules')
|
|
|
|
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,
|
2009-12-31 01:11:00 +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,
|
|
|
|
'--doctest-modules')
|
|
|
|
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,
|
|
|
|
'--doctest-modules')
|
|
|
|
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):
|
|
|
|
p = testdir.maketxtfile(xdoc ="""
|
|
|
|
>>> 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)
|
|
|
|
|
|
|
|
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
|
|
|
|
2013-03-25 03:05:29 +08:00
|
|
|
def test_doctest_linedata_missing(self, testdir):
|
|
|
|
testdir.tmpdir.join('hello.py').write(py.code.Source("""
|
|
|
|
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*",
|
|
|
|
])
|
|
|
|
|
|
|
|
|
2011-05-28 20:38:15 +08:00
|
|
|
def test_doctest_unex_importerror(self, testdir):
|
|
|
|
testdir.tmpdir.join("hello.py").write(py.code.Source("""
|
|
|
|
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")
|
|
|
|
result.stdout.fnmatch_lines([
|
|
|
|
"*>>> import hello",
|
|
|
|
"*UNEXPECTED*ImportError*",
|
|
|
|
"*import asdals*",
|
|
|
|
])
|
|
|
|
|
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")
|
|
|
|
p.join("__init__.py").write(py.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("""
|
|
|
|
class MyClass:
|
|
|
|
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("""
|
|
|
|
class MyClass:
|
|
|
|
'''
|
|
|
|
>>> 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("""
|
|
|
|
class MyClass:
|
|
|
|
'''
|
|
|
|
>>> 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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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*')
|
|
|
|
|