##// END OF EJS Templates
Various fixes for tests in IPython.testing.
Thomas Kluyver -
Show More
@@ -1,162 +1,164 b''
1 1 """Simple example using doctests.
2 2
3 3 This file just contains doctests both using plain python and IPython prompts.
4 4 All tests should be loaded by nose.
5 5 """
6 from IPython.utils.py3compat import doctest_refactor_print
6 7
7 8 def pyfunc():
8 9 """Some pure python tests...
9 10
10 11 >>> pyfunc()
11 12 'pyfunc'
12 13
13 14 >>> import os
14 15
15 16 >>> 2+3
16 17 5
17 18
18 19 >>> for i in range(3):
19 20 ... print i,
20 21 ... print i+1,
21 22 ...
22 23 0 1 1 2 2 3
23 24 """
24 25 return 'pyfunc'
25 26
26
27 @doctest_refactor_print
27 28 def ipfunc():
28 29 """Some ipython tests...
29 30
30 31 In [1]: import os
31 32
32 33 In [3]: 2+3
33 34 Out[3]: 5
34 35
35 36 In [26]: for i in range(3):
36 37 ....: print i,
37 38 ....: print i+1,
38 39 ....:
39 40 0 1 1 2 2 3
40 41
41 42
42 43 Examples that access the operating system work:
43 44
44 45 In [1]: !echo hello
45 46 hello
46 47
47 48 In [2]: !echo hello > /tmp/foo
48 49
49 50 In [3]: !cat /tmp/foo
50 51 hello
51 52
52 53 In [4]: rm -f /tmp/foo
53 54
54 55 It's OK to use '_' for the last result, but do NOT try to use IPython's
55 56 numbered history of _NN outputs, since those won't exist under the
56 57 doctest environment:
57 58
58 59 In [7]: 'hi'
59 60 Out[7]: 'hi'
60 61
61 62 In [8]: print repr(_)
62 63 'hi'
63 64
64 65 In [7]: 3+4
65 66 Out[7]: 7
66 67
67 68 In [8]: _+3
68 69 Out[8]: 10
69 70
70 71 In [9]: ipfunc()
71 72 Out[9]: 'ipfunc'
72 73 """
73 74 return 'ipfunc'
74 75
75 76
76 77 def ranfunc():
77 78 """A function with some random output.
78 79
79 80 Normal examples are verified as usual:
80 81 >>> 1+3
81 82 4
82 83
83 84 But if you put '# random' in the output, it is ignored:
84 85 >>> 1+3
85 86 junk goes here... # random
86 87
87 88 >>> 1+2
88 89 again, anything goes #random
89 90 if multiline, the random mark is only needed once.
90 91
91 92 >>> 1+2
92 93 You can also put the random marker at the end:
93 94 # random
94 95
95 96 >>> 1+2
96 97 # random
97 98 .. or at the beginning.
98 99
99 100 More correct input is properly verified:
100 101 >>> ranfunc()
101 102 'ranfunc'
102 103 """
103 104 return 'ranfunc'
104 105
105 106
106 107 def random_all():
107 108 """A function where we ignore the output of ALL examples.
108 109
109 110 Examples:
110 111
111 112 # all-random
112 113
113 114 This mark tells the testing machinery that all subsequent examples should
114 115 be treated as random (ignoring their output). They are still executed,
115 116 so if a they raise an error, it will be detected as such, but their
116 117 output is completely ignored.
117 118
118 119 >>> 1+3
119 120 junk goes here...
120 121
121 122 >>> 1+3
122 123 klasdfj;
123 124
124 125 >>> 1+2
125 126 again, anything goes
126 127 blah...
127 128 """
128 129 pass
129 130
130
131 @doctest_refactor_print
131 132 def iprand():
132 133 """Some ipython tests with random output.
133 134
134 135 In [7]: 3+4
135 136 Out[7]: 7
136 137
137 138 In [8]: print 'hello'
138 139 world # random
139 140
140 141 In [9]: iprand()
141 142 Out[9]: 'iprand'
142 143 """
143 144 return 'iprand'
144 145
145
146 @doctest_refactor_print
146 147 def iprand_all():
147 148 """Some ipython tests with fully random output.
148 149
149 150 # all-random
150 151
151 152 In [7]: 1
152 153 Out[7]: 99
153 154
154 155 In [8]: print 'hello'
155 156 world
156 157
157 158 In [9]: iprand_all()
158 159 Out[9]: 'junk'
159 160 """
160 161 return 'iprand_all'
161 162
162 163
164
@@ -1,75 +1,80 b''
1 1 """Tests for the ipdoctest machinery itself.
2 2
3 3 Note: in a file named test_X, functions whose only test is their docstring (as
4 4 a doctest) and which have no test functionality of their own, should be called
5 5 'doctest_foo' instead of 'test_foo', otherwise they get double-counted (the
6 6 empty function call is counted as a test, which just inflates tests numbers
7 7 artificially).
8 8 """
9 from IPython.utils.py3compat import doctest_refactor_print
9 10
11 @doctest_refactor_print
10 12 def doctest_simple():
11 13 """ipdoctest must handle simple inputs
12 14
13 15 In [1]: 1
14 16 Out[1]: 1
15 17
16 18 In [2]: print 1
17 19 1
18 20 """
19 21
20
22 @doctest_refactor_print
21 23 def doctest_multiline1():
22 24 """The ipdoctest machinery must handle multiline examples gracefully.
23 25
24 In [2]: for i in range(10):
25 ...: print i,
26 In [2]: for i in range(4):
27 ...: print i
26 28 ...:
27 0 1 2 3 4 5 6 7 8 9
29 0
30 1
31 2
32 3
28 33 """
29 34
30
35 @doctest_refactor_print
31 36 def doctest_multiline2():
32 37 """Multiline examples that define functions and print output.
33 38
34 39 In [7]: def f(x):
35 40 ...: return x+1
36 41 ...:
37 42
38 43 In [8]: f(1)
39 44 Out[8]: 2
40 45
41 46 In [9]: def g(x):
42 47 ...: print 'x is:',x
43 48 ...:
44 49
45 50 In [10]: g(1)
46 51 x is: 1
47 52
48 53 In [11]: g('hello')
49 54 x is: hello
50 55 """
51 56
52 57
53 58 def doctest_multiline3():
54 59 """Multiline examples with blank lines.
55 60
56 61 In [12]: def h(x):
57 62 ....: if x>1:
58 63 ....: return x**2
59 64 ....: # To leave a blank line in the input, you must mark it
60 65 ....: # with a comment character:
61 66 ....: #
62 67 ....: # otherwise the doctest parser gets confused.
63 68 ....: else:
64 69 ....: return -1
65 70 ....:
66 71
67 72 In [13]: h(5)
68 73 Out[13]: 25
69 74
70 75 In [14]: h(1)
71 76 Out[14]: -1
72 77
73 78 In [15]: h(0)
74 79 Out[15]: -1
75 80 """
@@ -1,46 +1,46 b''
1 1 """Some simple tests for the plugin while running scripts.
2 2 """
3 3 # Module imports
4 4 # Std lib
5 5 import inspect
6 6
7 7 # Our own
8 8
9 9 #-----------------------------------------------------------------------------
10 10 # Testing functions
11 11
12 12 def test_trivial():
13 13 """A trivial passing test."""
14 14 pass
15 15
16 16 def doctest_run():
17 17 """Test running a trivial script.
18 18
19 19 In [13]: run simplevars.py
20 20 x is: 1
21 21 """
22 22
23 23 def doctest_runvars():
24 24 """Test that variables defined in scripts get loaded correcly via %run.
25 25
26 26 In [13]: run simplevars.py
27 27 x is: 1
28 28
29 29 In [14]: x
30 30 Out[14]: 1
31 31 """
32 32
33 33 def doctest_ivars():
34 34 """Test that variables defined interactively are picked up.
35 35 In [5]: zz=1
36 36
37 37 In [6]: zz
38 38 Out[6]: 1
39 39 """
40 40
41 41 def doctest_refs():
42 42 """DocTest reference holding issues when running scripts.
43 43
44 44 In [32]: run show_refs.py
45 c referrers: [<type 'dict'>]
45 c referrers: [<... 'dict'>]
46 46 """
@@ -1,122 +1,137 b''
1 1 """Tests for IPython's test support utilities.
2 2
3 3 These are decorators that allow standalone functions and docstrings to be seen
4 4 as tests by unittest, replicating some of nose's functionality. Additionally,
5 5 IPython-syntax docstrings can be auto-converted to '>>>' so that ipython
6 6 sessions can be copy-pasted as tests.
7 7
8 8 This file can be run as a script, and it will call unittest.main(). We must
9 9 check that it works with unittest as well as with nose...
10 10
11 11
12 12 Notes:
13 13
14 14 - Using nosetests --with-doctest --doctest-tests testfile.py
15 15 will find docstrings as tests wherever they are, even in methods. But
16 16 if we use ipython syntax in the docstrings, they must be decorated with
17 17 @ipdocstring. This is OK for test-only code, but not for user-facing
18 18 docstrings where we want to keep the ipython syntax.
19 19
20 20 - Using nosetests --with-doctest file.py
21 21 also finds doctests if the file name doesn't have 'test' in it, because it is
22 22 treated like a normal module. But if nose treats the file like a test file,
23 23 then for normal classes to be doctested the extra --doctest-tests is
24 24 necessary.
25 25
26 26 - running this script with python (it has a __main__ section at the end) misses
27 27 one docstring test, the one embedded in the Foo object method. Since our
28 28 approach relies on using decorators that create standalone TestCase
29 29 instances, it can only be used for functions, not for methods of objects.
30 30 Authors
31 31 -------
32 32
33 33 - Fernando Perez <Fernando.Perez@berkeley.edu>
34 34 """
35 35
36 36 #-----------------------------------------------------------------------------
37 37 # Copyright (C) 2009 The IPython Development Team
38 38 #
39 39 # Distributed under the terms of the BSD License. The full license is in
40 40 # the file COPYING, distributed as part of this software.
41 41 #-----------------------------------------------------------------------------
42 42
43 43
44 44 #-----------------------------------------------------------------------------
45 45 # Imports
46 46 #-----------------------------------------------------------------------------
47 47
48 48 from IPython.testing.ipunittest import ipdoctest, ipdocstring
49 from IPython.utils.py3compat import doctest_refactor_print
49 50
50 51 #-----------------------------------------------------------------------------
51 52 # Test classes and functions
52 53 #-----------------------------------------------------------------------------
53 54 @ipdoctest
55 @doctest_refactor_print
54 56 def simple_dt():
55 57 """
56 58 >>> print 1+1
57 59 2
58 60 """
59 61
60 62
61 63 @ipdoctest
64 @doctest_refactor_print
62 65 def ipdt_flush():
63 66 """
64 67 In [20]: print 1
65 68 1
66 69
67 In [26]: for i in range(10):
68 ....: print i,
70 In [26]: for i in range(4):
71 ....: print i
69 72 ....:
70 73 ....:
71 0 1 2 3 4 5 6 7 8 9
74 0
75 1
76 2
77 3
72 78
73 79 In [27]: 3+4
74 80 Out[27]: 7
75 81 """
76 82
77 83
78 84 @ipdoctest
85 @doctest_refactor_print
79 86 def ipdt_indented_test():
80 87 """
81 88 In [20]: print 1
82 89 1
83 90
84 In [26]: for i in range(10):
85 ....: print i,
91 In [26]: for i in range(4):
92 ....: print i
86 93 ....:
87 94 ....:
88 0 1 2 3 4 5 6 7 8 9
95 0
96 1
97 2
98 3
89 99
90 100 In [27]: 3+4
91 101 Out[27]: 7
92 102 """
93 103
94 104
95 105 class Foo(object):
96 106 """For methods, the normal decorator doesn't work.
97 107
98 108 But rewriting the docstring with ip2py does, *but only if using nose
99 109 --with-doctest*. Do we want to have that as a dependency?
100 110 """
101 111
102 112 @ipdocstring
113 @doctest_refactor_print
103 114 def ipdt_method(self):
104 115 """
105 116 In [20]: print 1
106 117 1
107 118
108 In [26]: for i in range(10):
109 ....: print i,
119 In [26]: for i in range(4):
120 ....: print i
110 121 ....:
111 122 ....:
112 0 1 2 3 4 5 6 7 8 9
123 0
124 1
125 2
126 3
113 127
114 128 In [27]: 3+4
115 129 Out[27]: 7
116 130 """
117 131
132 @doctest_refactor_print
118 133 def normaldt_method(self):
119 134 """
120 135 >>> print 1+1
121 136 2
122 137 """
General Comments 0
You need to be logged in to leave comments. Login now