Debian Codegraph @

tests/test_ipunittest.py

raw
1 """Tests for IPython's test support utilities.
2
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,
5 IPython-syntax docstrings can be auto-converted to '>>>' so that ipython
6 sessions can be copy-pasted as tests.
7
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...
10
11
12 Notes:
13
14 - Using nosetests --with-doctest --doctest-tests testfile.py
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
17 @ipdocstring. This is OK for test-only code, but not for user-facing
18 docstrings where we want to keep the ipython syntax.
19
20 - Using nosetests --with-doctest file.py
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,
23 then for normal classes to be doctested the extra --doctest-tests is
24 necessary.
25
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
28 approach relies on using decorators that create standalone TestCase
29 instances, it can only be used for functions, not for methods of objects.
30 Authors
31 -------
32
33 - Fernando Perez <[email protected]>
34 """
35
36 # -----------------------------------------------------------------------------
37 # Copyright (C) 2009-2011 The IPython Development Team
38 #
39 # Distributed under the terms of the BSD License. The full license is in
40 # the file COPYING, distributed as part of this software.
41 # -----------------------------------------------------------------------------
42
43
44 # -----------------------------------------------------------------------------
45 # Imports
46 # -----------------------------------------------------------------------------
47
48 from IPython.testing.ipunittest import ipdoctest, ipdocstring
49
50 # -----------------------------------------------------------------------------
51 # Test classes and functions
52 # -----------------------------------------------------------------------------
53
54 # we do not want docstring reformatting here
55 # fmt: off
56
57 @ipdoctest
58 def simple_dt():
59 """
60 >>> print(1+1)
61 2
62 """
63
64
65 @ipdoctest
66 def ipdt_flush():
67 """
68 In [20]: print(1)
69 1
70
71 In [26]: for i in range(4):
72 ....: print(i)
73 ....:
74 ....:
75 0
76 1
77 2
78 3
79
80 In [27]: 3+4
81 Out[27]: 7
82 """
83
84
85 @ipdoctest
86 def ipdt_indented_test():
87 """
88 In [20]: print(1)
89 1
90
91 In [26]: for i in range(4):
92 ....: print(i)
93 ....:
94 ....:
95 0
96 1
97 2
98 3
99
100 In [27]: 3+4
101 Out[27]: 7
102 """
103
104
105 class Foo(object):
106 """For methods, the normal decorator doesn't work.
107
108 But rewriting the docstring with ip2py does, *but only if using nose
109 --with-doctest*. Do we want to have that as a dependency?
110 """
111
112 @ipdocstring
113 def ipdt_method(self):
114 """
115 In [20]: print(1)
116 1
117
118 In [26]: for i in range(4):
119 ....: print(i)
120 ....:
121 ....:
122 0
123 1
124 2
125 3
126
127 In [27]: 3+4
128 Out[27]: 7
129 """
130
131 def normaldt_method(self):
132 """
133 >>> print(1+1)
134 2
135 """