##// END OF EJS Templates
update 'tools and libraries available...' example in doc
MinRK -
Show More
@@ -1,368 +1,368 b''
1 1 .. _testing:
2 2
3 3 ==========================================
4 4 Testing IPython for users and developers
5 5 ==========================================
6 6
7 7 Overview
8 8 ========
9 9
10 10 It is extremely important that all code contributed to IPython has tests.
11 11 Tests should be written as unittests, doctests or other entities that the
12 12 IPython test system can detect. See below for more details on this.
13 13
14 14 Each subpackage in IPython should have its own :file:`tests` directory that
15 15 contains all of the tests for that subpackage. All of the files in the
16 16 :file:`tests` directory should have the word "tests" in them to enable
17 17 the testing framework to find them.
18 18
19 19 In docstrings, examples (either using IPython prompts like ``In [1]:`` or
20 20 'classic' python ``>>>`` ones) can and should be included. The testing system
21 21 will detect them as doctests and will run them; it offers control to skip parts
22 22 or all of a specific doctest if the example is meant to be informative but
23 23 shows non-reproducible information (like filesystem data).
24 24
25 25 If a subpackage has any dependencies beyond the Python standard library, the
26 26 tests for that subpackage should be skipped if the dependencies are not found.
27 27 This is very important so users don't get tests failing simply because they
28 28 don't have dependencies.
29 29
30 30 The testing system we use is an extension of the nose_ test runner.
31 31 In particular we've
32 32 developed a nose plugin that allows us to paste verbatim IPython sessions and
33 33 test them as doctests, which is extremely important for us.
34 34
35 35 .. _nose: http://code.google.com/p/python-nose
36 36
37 37
38 38 For the impatient: running the tests
39 39 ====================================
40 40
41 41 You can run IPython from the source download directory without even installing
42 42 it system-wide or having configure anything, by typing at the terminal:
43 43
44 44 .. code-block:: bash
45 45
46 46 python ipython.py
47 47
48 48 In order to run the test suite, you must at least be able to import IPython,
49 49 even if you haven't fully installed the user-facing scripts yet (common in a
50 50 development environment). You can then run the tests with:
51 51
52 52 .. code-block:: bash
53 53
54 54 python -c "import IPython; IPython.test()"
55 55
56 56 Once you have installed IPython either via a full install or using:
57 57
58 58 .. code-block:: bash
59 59
60 60 python setup.py develop
61 61
62 62 you will have available a system-wide script called :file:`iptest` that runs
63 63 the full test suite. You can then run the suite with:
64 64
65 65 .. code-block:: bash
66 66
67 67 iptest [args]
68 68
69 69
70 70 Regardless of how you run things, you should eventually see something like:
71 71
72 72 .. code-block:: bash
73 73
74 74 **********************************************************************
75 75 Test suite completed for system with the following information:
76 76 {'commit_hash': '144fdae',
77 77 'commit_source': 'repository',
78 78 'ipython_path': '/home/fperez/usr/lib/python2.6/site-packages/IPython',
79 79 'ipython_version': '0.11.dev',
80 80 'os_name': 'posix',
81 81 'platform': 'Linux-2.6.35-22-generic-i686-with-Ubuntu-10.10-maverick',
82 82 'sys_executable': '/usr/bin/python',
83 83 'sys_platform': 'linux2',
84 84 'sys_version': '2.6.6 (r266:84292, Sep 15 2010, 15:52:39) \n[GCC 4.4.5]'}
85 85
86 86 Tools and libraries available at test time:
87 curses foolscap gobject gtk pexpect twisted wx wx.aui zope.interface
87 curses matplotlib pymongo qt sqlite3 tornado wx wx.aui zmq
88 88
89 89 Ran 9 test groups in 67.213s
90 90
91 91 Status:
92 92 OK
93 93
94 94
95 95 If not, there will be a message indicating which test group failed and how to
96 96 rerun that group individually. For example, this tests the
97 97 :mod:`IPython.utils` subpackage, the :option:`-v` option shows progress
98 98 indicators:
99 99
100 100 .. code-block:: bash
101 101
102 102 $ iptest -v IPython.utils
103 103 ..........................SS..SSS............................S.S...
104 104 .........................................................
105 105 ----------------------------------------------------------------------
106 106 Ran 125 tests in 0.119s
107 107
108 108 OK (SKIP=7)
109 109
110 110
111 111 Because the IPython test machinery is based on nose, you can use all nose
112 112 options and syntax, typing ``iptest -h`` shows all available options. For
113 113 example, this lets you run the specific test :func:`test_rehashx` inside the
114 114 :mod:`test_magic` module:
115 115
116 116 .. code-block:: bash
117 117
118 118 $ iptest -vv IPython.core.tests.test_magic:test_rehashx
119 119 IPython.core.tests.test_magic.test_rehashx(True,) ... ok
120 120 IPython.core.tests.test_magic.test_rehashx(True,) ... ok
121 121
122 122 ----------------------------------------------------------------------
123 123 Ran 2 tests in 0.100s
124 124
125 125 OK
126 126
127 127 When developing, the :option:`--pdb` and :option:`--pdb-failures` of nose are
128 128 particularly useful, these drop you into an interactive pdb session at the
129 129 point of the error or failure respectively.
130 130
131 131 .. note::
132 132
133 133 The system information summary printed above is accessible from the top
134 134 level package. If you encounter a problem with IPython, it's useful to
135 135 include this information when reporting on the mailing list; use::
136 136
137 137 from IPython import sys_info
138 138 print sys_info()
139 139
140 140 and include the resulting information in your query.
141 141
142 142 For developers: writing tests
143 143 =============================
144 144
145 145 By now IPython has a reasonable test suite, so the best way to see what's
146 146 available is to look at the :file:`tests` directory in most subpackages. But
147 147 here are a few pointers to make the process easier.
148 148
149 149
150 150 Main tools: :mod:`IPython.testing`
151 151 ----------------------------------
152 152
153 153 The :mod:`IPython.testing` package is where all of the machinery to test
154 154 IPython (rather than the tests for its various parts) lives. In particular,
155 155 the :mod:`iptest` module in there has all the smarts to control the test
156 156 process. In there, the :func:`make_exclude` function is used to build a
157 157 blacklist of exclusions, these are modules that do not get even imported for
158 158 tests. This is important so that things that would fail to even import because
159 159 of missing dependencies don't give errors to end users, as we stated above.
160 160
161 161 The :mod:`decorators` module contains a lot of useful decorators, especially
162 162 useful to mark individual tests that should be skipped under certain conditions
163 163 (rather than blacklisting the package altogether because of a missing major
164 164 dependency).
165 165
166 166 Our nose plugin for doctests
167 167 ----------------------------
168 168
169 169 The :mod:`plugin` subpackage in testing contains a nose plugin called
170 170 :mod:`ipdoctest` that teaches nose about IPython syntax, so you can write
171 171 doctests with IPython prompts. You can also mark doctest output with ``#
172 172 random`` for the output corresponding to a single input to be ignored (stronger
173 173 than using ellipsis and useful to keep it as an example). If you want the
174 174 entire docstring to be executed but none of the output from any input to be
175 175 checked, you can use the ``# all-random`` marker. The
176 176 :mod:`IPython.testing.plugin.dtexample` module contains examples of how to use
177 177 these; for reference here is how to use ``# random``::
178 178
179 179 def ranfunc():
180 180 """A function with some random output.
181 181
182 182 Normal examples are verified as usual:
183 183 >>> 1+3
184 184 4
185 185
186 186 But if you put '# random' in the output, it is ignored:
187 187 >>> 1+3
188 188 junk goes here... # random
189 189
190 190 >>> 1+2
191 191 again, anything goes #random
192 192 if multiline, the random mark is only needed once.
193 193
194 194 >>> 1+2
195 195 You can also put the random marker at the end:
196 196 # random
197 197
198 198 >>> 1+2
199 199 # random
200 200 .. or at the beginning.
201 201
202 202 More correct input is properly verified:
203 203 >>> ranfunc()
204 204 'ranfunc'
205 205 """
206 206 return 'ranfunc'
207 207
208 208 and an example of ``# all-random``::
209 209
210 210 def random_all():
211 211 """A function where we ignore the output of ALL examples.
212 212
213 213 Examples:
214 214
215 215 # all-random
216 216
217 217 This mark tells the testing machinery that all subsequent examples
218 218 should be treated as random (ignoring their output). They are still
219 219 executed, so if a they raise an error, it will be detected as such,
220 220 but their output is completely ignored.
221 221
222 222 >>> 1+3
223 223 junk goes here...
224 224
225 225 >>> 1+3
226 226 klasdfj;
227 227
228 228 In [8]: print 'hello'
229 229 world # random
230 230
231 231 In [9]: iprand()
232 232 Out[9]: 'iprand'
233 233 """
234 234 return 'iprand'
235 235
236 236
237 237 When writing docstrings, you can use the ``@skip_doctest`` decorator to
238 238 indicate that a docstring should *not* be treated as a doctest at all. The
239 239 difference between ``# all-random`` and ``@skip_doctest`` is that the former
240 240 executes the example but ignores output, while the latter doesn't execute any
241 241 code. ``@skip_doctest`` should be used for docstrings whose examples are
242 242 purely informational.
243 243
244 244 If a given docstring fails under certain conditions but otherwise is a good
245 245 doctest, you can use code like the following, that relies on the 'null'
246 246 decorator to leave the docstring intact where it works as a test::
247 247
248 248 # The docstring for full_path doctests differently on win32 (different path
249 249 # separator) so just skip the doctest there, and use a null decorator
250 250 # elsewhere:
251 251
252 252 doctest_deco = dec.skip_doctest if sys.platform == 'win32' else dec.null_deco
253 253
254 254 @doctest_deco
255 255 def full_path(startPath,files):
256 256 """Make full paths for all the listed files, based on startPath..."""
257 257
258 258 # function body follows...
259 259
260 260 With our nose plugin that understands IPython syntax, an extremely effective
261 261 way to write tests is to simply copy and paste an interactive session into a
262 262 docstring. You can writing this type of test, where your docstring is meant
263 263 *only* as a test, by prefixing the function name with ``doctest_`` and leaving
264 264 its body *absolutely empty* other than the docstring. In
265 265 :mod:`IPython.core.tests.test_magic` you can find several examples of this, but
266 266 for completeness sake, your code should look like this (a simple case)::
267 267
268 268 def doctest_time():
269 269 """
270 270 In [10]: %time None
271 271 CPU times: user 0.00 s, sys: 0.00 s, total: 0.00 s
272 272 Wall time: 0.00 s
273 273 """
274 274
275 275 This function is only analyzed for its docstring but it is not considered a
276 276 separate test, which is why its body should be empty.
277 277
278 278
279 279 Parametric tests done right
280 280 ---------------------------
281 281
282 282 If you need to run multiple tests inside the same standalone function or method
283 283 of a :class:`unittest.TestCase` subclass, IPython provides the ``parametric``
284 284 decorator for this purpose. This is superior to how test generators work in
285 285 nose, because IPython's keeps intact your stack, which makes debugging vastly
286 286 easier. For example, these are some parametric tests both in class form and as
287 287 a standalone function (choose in each situation the style that best fits the
288 288 problem at hand, since both work)::
289 289
290 290 from IPython.testing import decorators as dec
291 291
292 292 def is_smaller(i,j):
293 293 assert i<j,"%s !< %s" % (i,j)
294 294
295 295 class Tester(ParametricTestCase):
296 296
297 297 def test_parametric(self):
298 298 yield is_smaller(3, 4)
299 299 x, y = 1, 2
300 300 yield is_smaller(x, y)
301 301
302 302 @dec.parametric
303 303 def test_par_standalone():
304 304 yield is_smaller(3, 4)
305 305 x, y = 1, 2
306 306 yield is_smaller(x, y)
307 307
308 308
309 309 Design requirements
310 310 ===================
311 311
312 312 This section is a set of notes on the key points of the IPython testing needs,
313 313 that were used when writing the system and should be kept for reference as it
314 314 eveolves.
315 315
316 316 Testing IPython in full requires modifications to the default behavior of nose
317 317 and doctest, because the IPython prompt is not recognized to determine Python
318 318 input, and because IPython admits user input that is not valid Python (things
319 319 like ``%magics`` and ``!system commands``.
320 320
321 321 We basically need to be able to test the following types of code:
322 322
323 323 1. Pure Python files containing normal tests. These are not a problem, since
324 324 Nose will pick them up as long as they conform to the (flexible) conventions
325 325 used by nose to recognize tests.
326 326
327 327 2. Python files containing doctests. Here, we have two possibilities:
328 328
329 329 - The prompts are the usual ``>>>`` and the input is pure Python.
330 330 - The prompts are of the form ``In [1]:`` and the input can contain extended
331 331 IPython expressions.
332 332
333 333 In the first case, Nose will recognize the doctests as long as it is called
334 334 with the ``--with-doctest`` flag. But the second case will likely require
335 335 modifications or the writing of a new doctest plugin for Nose that is
336 336 IPython-aware.
337 337
338 338 3. ReStructuredText files that contain code blocks. For this type of file, we
339 339 have three distinct possibilities for the code blocks:
340 340
341 341 - They use ``>>>`` prompts.
342 342 - They use ``In [1]:`` prompts.
343 343 - They are standalone blocks of pure Python code without any prompts.
344 344
345 345 The first two cases are similar to the situation #2 above, except that in
346 346 this case the doctests must be extracted from input code blocks using
347 347 docutils instead of from the Python docstrings.
348 348
349 349 In the third case, we must have a convention for distinguishing code blocks
350 350 that are meant for execution from others that may be snippets of shell code
351 351 or other examples not meant to be run. One possibility is to assume that
352 352 all indented code blocks are meant for execution, but to have a special
353 353 docutils directive for input that should not be executed.
354 354
355 355 For those code blocks that we will execute, the convention used will simply
356 356 be that they get called and are considered successful if they run to
357 357 completion without raising errors. This is similar to what Nose does for
358 358 standalone test functions, and by putting asserts or other forms of
359 359 exception-raising statements it becomes possible to have literate examples
360 360 that double as lightweight tests.
361 361
362 362 4. Extension modules with doctests in function and method docstrings.
363 363 Currently Nose simply can't find these docstrings correctly, because the
364 364 underlying doctest DocTestFinder object fails there. Similarly to #2 above,
365 365 the docstrings could have either pure python or IPython prompts.
366 366
367 367 Of these, only 3-c (reST with standalone code blocks) is not implemented at
368 368 this point.
General Comments 0
You need to be logged in to leave comments. Login now