Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit da07a07

Browse files
committed
Complete first pass on testing system. All tests pass on my box. Whew.
We now have a first cut of a functioning testing system. Many examples had to be disabled (with a proper decorator), but now we can begin building a robust test suite for all of IPython. The big remaining todo is to fix the handling of the ipython/test namespace synchronization, which is still not 100% correct. Once that is done, some of the currently skipped tests will be reactivated.
1 parent 2b832a8 commit da07a07

11 files changed

Lines changed: 187 additions & 94 deletions

File tree

IPython/Magic.py

Lines changed: 89 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@
6161
import IPython.generics
6262
import IPython.ipapi
6363
from IPython.ipapi import UsageError
64+
from IPython.testing import decorators as testdec
65+
6466
#***************************************************************************
6567
# Utility functions
6668
def on_off(tag):
@@ -522,7 +524,7 @@ def magic_automagic(self, parameter_s = ''):
522524
rc.automagic = not rc.automagic
523525
print '\n' + Magic.auto_status[rc.automagic]
524526

525-
527+
@testdec.skip_doctest
526528
def magic_autocall(self, parameter_s = ''):
527529
"""Make functions callable without having to type parentheses.
528530
@@ -551,8 +553,9 @@ def magic_autocall(self, parameter_s = ''):
551553
2 -> Active always. Even if no arguments are present, the callable
552554
object is called:
553555
554-
In [4]: callable
555-
------> callable()
556+
In [2]: float
557+
------> float()
558+
Out[2]: 0.0
556559
557560
Note that even with autocall off, you can still use '/' at the start of
558561
a line to treat the first argument on the command line as a function
@@ -561,6 +564,8 @@ def magic_autocall(self, parameter_s = ''):
561564
In [8]: /str 43
562565
------> str(43)
563566
Out[8]: '43'
567+
568+
# all-random (note for auto-testing)
564569
"""
565570

566571
rc = self.shell.rc
@@ -1243,12 +1248,13 @@ def magic_debug(self, parameter_s=''):
12431248

12441249
self.shell.debugger(force=True)
12451250

1251+
@testdec.skip_doctest
12461252
def magic_prun(self, parameter_s ='',user_mode=1,
12471253
opts=None,arg_lst=None,prog_ns=None):
12481254

12491255
"""Run a statement through the python code profiler.
12501256
1251-
Usage:\\
1257+
Usage:
12521258
%prun [options] statement
12531259
12541260
The given statement (which doesn't require quote marks) is run via the
@@ -1293,16 +1299,16 @@ def magic_prun(self, parameter_s ='',user_mode=1,
12931299
abbreviation is unambiguous. The following are the keys currently
12941300
defined:
12951301
1296-
Valid Arg Meaning\\
1297-
"calls" call count\\
1298-
"cumulative" cumulative time\\
1299-
"file" file name\\
1300-
"module" file name\\
1301-
"pcalls" primitive call count\\
1302-
"line" line number\\
1303-
"name" function name\\
1304-
"nfl" name/file/line\\
1305-
"stdname" standard name\\
1302+
Valid Arg Meaning
1303+
"calls" call count
1304+
"cumulative" cumulative time
1305+
"file" file name
1306+
"module" file name
1307+
"pcalls" primitive call count
1308+
"line" line number
1309+
"name" function name
1310+
"nfl" name/file/line
1311+
"stdname" standard name
13061312
"time" internal time
13071313
13081314
Note that all sorts on statistics are in descending order (placing
@@ -1328,8 +1334,10 @@ def magic_prun(self, parameter_s ='',user_mode=1,
13281334
'%run -p [prof_opts] filename.py [args to program]' where prof_opts
13291335
contains profiler specific options as described here.
13301336
1331-
You can read the complete documentation for the profile module with:\\
1332-
In []: import profile; profile.help() """
1337+
You can read the complete documentation for the profile module with::
1338+
1339+
In [1]: import profile; profile.help()
1340+
"""
13331341

13341342
opts_def = Struct(D=[''],l=[],s=['time'],T=[''])
13351343
# protect user quote marks
@@ -1413,6 +1421,7 @@ def magic_prun(self, parameter_s ='',user_mode=1,
14131421
else:
14141422
return None
14151423

1424+
@testdec.skip_doctest
14161425
def magic_run(self, parameter_s ='',runner=None):
14171426
"""Run the named file inside IPython as a program.
14181427
@@ -1709,6 +1718,7 @@ def magic_runlog(self, parameter_s =''):
17091718
self.shell.safe_execfile(f,self.shell.user_ns,
17101719
self.shell.user_ns,islog=1)
17111720

1721+
@testdec.skip_doctest
17121722
def magic_timeit(self, parameter_s =''):
17131723
"""Time execution of a Python statement or expression
17141724
@@ -1736,7 +1746,8 @@ def magic_timeit(self, parameter_s =''):
17361746
Default: 3
17371747
17381748
1739-
Examples:\\
1749+
Examples:
1750+
17401751
In [1]: %timeit pass
17411752
10000000 loops, best of 3: 53.3 ns per loop
17421753
@@ -1820,7 +1831,8 @@ def magic_timeit(self, parameter_s =''):
18201831
units[order])
18211832
if tc > tc_min:
18221833
print "Compiler time: %.2f s" % tc
1823-
1834+
1835+
@testdec.skip_doctest
18241836
def magic_time(self,parameter_s = ''):
18251837
"""Time execution of a Python statement or expression.
18261838
@@ -1912,6 +1924,7 @@ def magic_time(self,parameter_s = ''):
19121924
print "Compiler : %.2f s" % tc
19131925
return out
19141926

1927+
@testdec.skip_doctest
19151928
def magic_macro(self,parameter_s = ''):
19161929
"""Define a set of input lines as a macro for future re-execution.
19171930
@@ -1941,17 +1954,17 @@ def magic_macro(self,parameter_s = ''):
19411954
19421955
For example, if your history contains (%hist prints it):
19431956
1944-
44: x=1\\
1945-
45: y=3\\
1946-
46: z=x+y\\
1947-
47: print x\\
1948-
48: a=5\\
1949-
49: print 'x',x,'y',y\\
1957+
44: x=1
1958+
45: y=3
1959+
46: z=x+y
1960+
47: print x
1961+
48: a=5
1962+
49: print 'x',x,'y',y
19501963
19511964
you can create a macro with lines 44 through 47 (included) and line 49
19521965
called my_macro with:
19531966
1954-
In []: %macro my_macro 44-47 49
1967+
In [55]: %macro my_macro 44-47 49
19551968
19561969
Now, typing `my_macro` (without quotes) will re-execute all this code
19571970
in one pass.
@@ -1972,7 +1985,7 @@ def magic_macro(self,parameter_s = ''):
19721985
can obtain similar results by explicitly executing slices from your
19731986
input history with:
19741987
1975-
In []: exec In[44:48]+In[49]"""
1988+
In [60]: exec In[44:48]+In[49]"""
19761989

19771990
opts,args = self.parse_options(parameter_s,'r',mode='list')
19781991
if not args:
@@ -2043,6 +2056,7 @@ def magic_ed(self,parameter_s=''):
20432056
"""Alias to %edit."""
20442057
return self.magic_edit(parameter_s)
20452058

2059+
@testdec.skip_doctest
20462060
def magic_edit(self,parameter_s='',last_call=['','']):
20472061
"""Bring up an editor and execute the resulting code.
20482062
@@ -2136,47 +2150,47 @@ def magic_edit(self,parameter_s='',last_call=['','']):
21362150
This is an example of creating a simple function inside the editor and
21372151
then modifying it. First, start up the editor:
21382152
2139-
In []: ed\\
2140-
Editing... done. Executing edited code...\\
2141-
Out[]: 'def foo():\\n print "foo() was defined in an editing session"\\n'
2153+
In [1]: ed
2154+
Editing... done. Executing edited code...
2155+
Out[1]: 'def foo():n print "foo() was defined in an editing session"n'
21422156
21432157
We can then call the function foo():
21442158
2145-
In []: foo()\\
2159+
In [2]: foo()
21462160
foo() was defined in an editing session
21472161
21482162
Now we edit foo. IPython automatically loads the editor with the
21492163
(temporary) file where foo() was previously defined:
21502164
2151-
In []: ed foo\\
2165+
In [3]: ed foo
21522166
Editing... done. Executing edited code...
21532167
21542168
And if we call foo() again we get the modified version:
21552169
2156-
In []: foo()\\
2170+
In [4]: foo()
21572171
foo() has now been changed!
21582172
21592173
Here is an example of how to edit a code snippet successive
21602174
times. First we call the editor:
21612175
2162-
In []: ed\\
2163-
Editing... done. Executing edited code...\\
2164-
hello\\
2165-
Out[]: "print 'hello'\\n"
2176+
In [5]: ed
2177+
Editing... done. Executing edited code...
2178+
hello
2179+
Out[5]: "print 'hello'n"
21662180
21672181
Now we call it again with the previous output (stored in _):
21682182
2169-
In []: ed _\\
2170-
Editing... done. Executing edited code...\\
2171-
hello world\\
2172-
Out[]: "print 'hello world'\\n"
2183+
In [6]: ed _
2184+
Editing... done. Executing edited code...
2185+
hello world
2186+
Out[6]: "print 'hello world'n"
21732187
21742188
Now we call it with the output #8 (stored in _8, also as Out[8]):
21752189
2176-
In []: ed _8\\
2177-
Editing... done. Executing edited code...\\
2178-
hello again\\
2179-
Out[]: "print 'hello again'\\n"
2190+
In [7]: ed _8
2191+
Editing... done. Executing edited code...
2192+
hello again
2193+
Out[7]: "print 'hello again'n"
21802194
21812195
21822196
Changing the default editor hook:
@@ -2473,7 +2487,8 @@ def magic_Exit(self, parameter_s=''):
24732487

24742488
#......................................................................
24752489
# Functions to implement unix shell-type things
2476-
2490+
2491+
@testdec.skip_doctest
24772492
def magic_alias(self, parameter_s = ''):
24782493
"""Define an alias for a system command.
24792494
@@ -2489,18 +2504,18 @@ def magic_alias(self, parameter_s = ''):
24892504
You can use the %l specifier in an alias definition to represent the
24902505
whole line when the alias is called. For example:
24912506
2492-
In [2]: alias all echo "Input in brackets: <%l>"\\
2493-
In [3]: all hello world\\
2507+
In [2]: alias all echo "Input in brackets: <%l>"
2508+
In [3]: all hello world
24942509
Input in brackets: <hello world>
24952510
24962511
You can also define aliases with parameters using %s specifiers (one
24972512
per parameter):
24982513
2499-
In [1]: alias parts echo first %s second %s\\
2500-
In [2]: %parts A B\\
2501-
first A second B\\
2502-
In [3]: %parts A\\
2503-
Incorrect number of arguments: 2 expected.\\
2514+
In [1]: alias parts echo first %s second %s
2515+
In [2]: %parts A B
2516+
first A second B
2517+
In [3]: %parts A
2518+
Incorrect number of arguments: 2 expected.
25042519
parts is an alias to: 'echo first %s second %s'
25052520
25062521
Note that %l and %s are mutually exclusive. You can only use one or
@@ -2513,11 +2528,11 @@ def magic_alias(self, parameter_s = ''):
25132528
IPython for variable expansion. If you want to access a true shell
25142529
variable, an extra $ is necessary to prevent its expansion by IPython:
25152530
2516-
In [6]: alias show echo\\
2517-
In [7]: PATH='A Python string'\\
2518-
In [8]: show $PATH\\
2519-
A Python string\\
2520-
In [9]: show $$PATH\\
2531+
In [6]: alias show echo
2532+
In [7]: PATH='A Python string'
2533+
In [8]: show $PATH
2534+
A Python string
2535+
In [9]: show $$PATH
25212536
/usr/local/lf9560/bin:/usr/local/intel/compiler70/ia32/bin:...
25222537
25232538
You can use the alias facility to acess all of $PATH. See the %rehash
@@ -2832,7 +2847,7 @@ def magic_dhist(self, parameter_s=''):
28322847
header = 'Directory history (kept in _dh)',
28332848
start=ini,stop=fin)
28342849

2835-
2850+
@testdec.skip_doctest
28362851
def magic_sc(self, parameter_s=''):
28372852
"""Shell capture - execute a shell command and capture its output.
28382853
@@ -2876,29 +2891,31 @@ def magic_sc(self, parameter_s=''):
28762891
28772892
For example:
28782893
2894+
# all-random
2895+
28792896
# Capture into variable a
2880-
In []: sc a=ls *py
2897+
In [1]: sc a=ls *py
28812898
28822899
# a is a string with embedded newlines
2883-
In []: a
2884-
Out[]: 'setup.py\nwin32_manual_post_install.py'
2900+
In [2]: a
2901+
Out[2]: 'setup.py\\nwin32_manual_post_install.py'
28852902
28862903
# which can be seen as a list:
2887-
In []: a.l
2888-
Out[]: ['setup.py', 'win32_manual_post_install.py']
2904+
In [3]: a.l
2905+
Out[3]: ['setup.py', 'win32_manual_post_install.py']
28892906
28902907
# or as a whitespace-separated string:
2891-
In []: a.s
2892-
Out[]: 'setup.py win32_manual_post_install.py'
2908+
In [4]: a.s
2909+
Out[4]: 'setup.py win32_manual_post_install.py'
28932910
28942911
# a.s is useful to pass as a single command line:
2895-
In []: !wc -l $a.s
2912+
In [5]: !wc -l $a.s
28962913
146 setup.py
28972914
130 win32_manual_post_install.py
28982915
276 total
28992916
29002917
# while the list form is useful to loop over:
2901-
In []: for f in a.l:
2918+
In [6]: for f in a.l:
29022919
...: !wc -l $f
29032920
...:
29042921
146 setup.py
@@ -2908,13 +2925,13 @@ def magic_sc(self, parameter_s=''):
29082925
the sense that you can equally invoke the .s attribute on them to
29092926
automatically get a whitespace-separated string from their contents:
29102927
2911-
In []: sc -l b=ls *py
2928+
In [7]: sc -l b=ls *py
29122929
2913-
In []: b
2914-
Out[]: ['setup.py', 'win32_manual_post_install.py']
2930+
In [8]: b
2931+
Out[8]: ['setup.py', 'win32_manual_post_install.py']
29152932
2916-
In []: b.s
2917-
Out[]: 'setup.py win32_manual_post_install.py'
2933+
In [9]: b.s
2934+
Out[9]: 'setup.py win32_manual_post_install.py'
29182935
29192936
In summary, both the lists and strings used for ouptut capture have
29202937
the following special attributes:

IPython/iplib.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1010,7 +1010,7 @@ def complete(self,text):
10101010
hello
10111011
10121012
In [10]: _ip.IP.complete('x.l')
1013-
Out[10]: ['x.ljust', 'x.lower', 'x.lstrip']
1013+
Out[10]: ['x.ljust', 'x.lower', 'x.lstrip'] # random
10141014
"""
10151015

10161016
complete = self.Completer.complete

IPython/ipstruct.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ class Struct:
8888
initialization): keys can't be numbers. But numeric keys can be used and
8989
accessed using the dictionary syntax. Again, an example:
9090
91-
This doesn't work:
92-
py> s=Struct(4='hi') #doctest: +IGNORE_EXCEPTION_DETAIL
91+
This doesn't work (prompt changed to avoid confusing the test system):
92+
->> s=Struct(4='hi')
9393
Traceback (most recent call last):
9494
...
9595
SyntaxError: keyword can't be an expression

IPython/kernel/tests/test_contexts.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import with_statement
2+
13
#def test_simple():
24
if 0:
35

0 commit comments

Comments
 (0)