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