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