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