Skip to content

Commit cd012f9

Browse files
authored
Added doctest tests (#6598)
1 parent e22091e commit cd012f9

19 files changed

Lines changed: 4554 additions & 1 deletion

Lib/test/test_doctest/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import os
2+
from test.support import load_package_tests
3+
4+
def load_tests(*args):
5+
return load_package_tests(os.path.dirname(__file__), *args)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# This module is used in `doctest_lineno.py`.
2+
import functools
3+
4+
5+
def decorator(f):
6+
@functools.wraps(f)
7+
def inner():
8+
return f()
9+
10+
return inner
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Used by test_doctest.py.
2+
3+
class TwoNames:
4+
'''f() and g() are two names for the same method'''
5+
6+
def f(self):
7+
'''
8+
>>> print(TwoNames().f())
9+
f
10+
'''
11+
return 'f'
12+
13+
g = f # define an alias for f
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# This module is used in `test_doctest`.
2+
# It must not have a docstring.
3+
4+
def func_with_docstring():
5+
"""Some unrelated info."""
6+
7+
8+
def func_without_docstring():
9+
pass
10+
11+
12+
def func_with_doctest():
13+
"""
14+
This function really contains a test case.
15+
16+
>>> func_with_doctest.__name__
17+
'func_with_doctest'
18+
"""
19+
return 3
20+
21+
22+
class ClassWithDocstring:
23+
"""Some unrelated class information."""
24+
25+
26+
class ClassWithoutDocstring:
27+
pass
28+
29+
30+
class ClassWithDoctest:
31+
"""This class really has a test case in it.
32+
33+
>>> ClassWithDoctest.__name__
34+
'ClassWithDoctest'
35+
"""
36+
37+
38+
class MethodWrapper:
39+
def method_with_docstring(self):
40+
"""Method with a docstring."""
41+
42+
def method_without_docstring(self):
43+
pass
44+
45+
def method_with_doctest(self):
46+
"""
47+
This has a doctest!
48+
>>> MethodWrapper.method_with_doctest.__name__
49+
'method_with_doctest'
50+
"""
51+
52+
@classmethod
53+
def classmethod_with_doctest(cls):
54+
"""
55+
This has a doctest!
56+
>>> MethodWrapper.classmethod_with_doctest.__name__
57+
'classmethod_with_doctest'
58+
"""
59+
60+
@property
61+
def property_with_doctest(self):
62+
"""
63+
This has a doctest!
64+
>>> MethodWrapper.property_with_doctest.__name__
65+
'property_with_doctest'
66+
"""
67+
68+
# https://github.com/python/cpython/issues/99433
69+
str_wrapper = object().__str__
70+
71+
72+
# https://github.com/python/cpython/issues/115392
73+
from test.test_doctest.decorator_mod import decorator
74+
75+
@decorator
76+
@decorator
77+
def func_with_docstring_wrapped():
78+
"""Some unrelated info."""
79+
80+
81+
# https://github.com/python/cpython/issues/136914
82+
import functools
83+
84+
85+
@functools.cache
86+
def cached_func_with_doctest(value):
87+
"""
88+
>>> cached_func_with_doctest(1)
89+
-1
90+
"""
91+
return -value
92+
93+
94+
@functools.cache
95+
def cached_func_without_docstring(value):
96+
return value + 1
97+
98+
99+
class ClassWithACachedProperty:
100+
101+
@functools.cached_property
102+
def cached(self):
103+
"""
104+
>>> X().cached
105+
-1
106+
"""
107+
return 0
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
"""This is a sample module that doesn't really test anything all that
2+
interesting.
3+
4+
It simply has a few tests, some of which succeed and some of which fail.
5+
6+
It's important that the numbers remain constant as another test is
7+
testing the running of these tests.
8+
9+
10+
>>> 2+2
11+
4
12+
"""
13+
14+
15+
def foo():
16+
"""
17+
18+
>>> 2+2
19+
5
20+
21+
>>> 2+2
22+
4
23+
"""
24+
25+
def bar():
26+
"""
27+
28+
>>> 2+2
29+
4
30+
"""
31+
32+
def test_silly_setup():
33+
"""
34+
35+
>>> import test.test_doctest.test_doctest
36+
>>> test.test_doctest.test_doctest.sillySetup
37+
True
38+
"""
39+
40+
def w_blank():
41+
"""
42+
>>> if 1:
43+
... print('a')
44+
... print()
45+
... print('b')
46+
a
47+
<BLANKLINE>
48+
b
49+
"""
50+
51+
x = 1
52+
def x_is_one():
53+
"""
54+
>>> x
55+
1
56+
"""
57+
58+
def y_is_one():
59+
"""
60+
>>> y
61+
1
62+
"""
63+
64+
__test__ = {'good': """
65+
>>> 42
66+
42
67+
""",
68+
'bad': """
69+
>>> 42
70+
666
71+
""",
72+
}
73+
74+
def test_suite():
75+
import doctest
76+
return doctest.DocTestSuite()
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""This is a sample module used for testing doctest.
2+
3+
This module includes various scenarios involving errors.
4+
5+
>>> 2 + 2
6+
5
7+
>>> 1/0
8+
1
9+
"""
10+
11+
def g():
12+
[][0] # line 12
13+
14+
def errors():
15+
"""
16+
>>> 2 + 2
17+
5
18+
>>> 1/0
19+
1
20+
>>> def f():
21+
... 2 + '2'
22+
...
23+
>>> f()
24+
1
25+
>>> g()
26+
1
27+
"""
28+
29+
def syntax_error():
30+
"""
31+
>>> 2+*3
32+
5
33+
"""
34+
35+
__test__ = {
36+
'bad': """
37+
>>> 2 + 2
38+
5
39+
>>> 1/0
40+
1
41+
""",
42+
}
43+
44+
def test_suite():
45+
import doctest
46+
return doctest.DocTestSuite()
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This is a sample module used for testing doctest.
2+
#
3+
# This module is for testing how doctest handles a module with no
4+
# docstrings.
5+
6+
7+
class Foo(object):
8+
9+
# A class with no docstring.
10+
11+
def __init__(self):
12+
pass
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""This is a sample module used for testing doctest.
2+
3+
This module is for testing how doctest handles a module with docstrings
4+
but no doctest examples.
5+
6+
"""
7+
8+
9+
class Foo(object):
10+
"""A docstring with no doctest examples.
11+
12+
"""
13+
14+
def __init__(self):
15+
pass
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""This is a sample module used for testing doctest.
2+
3+
This module includes various scenarios involving skips.
4+
"""
5+
6+
def no_skip_pass():
7+
"""
8+
>>> 2 + 2
9+
4
10+
"""
11+
12+
def no_skip_fail():
13+
"""
14+
>>> 2 + 2
15+
5
16+
"""
17+
18+
def single_skip():
19+
"""
20+
>>> 2 + 2 # doctest: +SKIP
21+
4
22+
"""
23+
24+
def double_skip():
25+
"""
26+
>>> 2 + 2 # doctest: +SKIP
27+
4
28+
>>> 3 + 3 # doctest: +SKIP
29+
6
30+
"""
31+
32+
def partial_skip_pass():
33+
"""
34+
>>> 2 + 2 # doctest: +SKIP
35+
4
36+
>>> 3 + 3
37+
6
38+
"""
39+
40+
def partial_skip_fail():
41+
"""
42+
>>> 2 + 2 # doctest: +SKIP
43+
4
44+
>>> 2 + 2
45+
5
46+
"""
47+
48+
def no_examples():
49+
"""A docstring with no examples should not be counted as run or skipped."""

0 commit comments

Comments
 (0)