root / PyCon07 / material / doctest.txt
| Revision 63:e6fd368e8f33, 492 bytes (checked in by Tarek Ziad?? <tarek@…>, 18 months ago) |
|---|
| Line | |
|---|---|
| 1 | =============== |
| 2 | doctest example |
| 3 | =============== |
| 4 | |
| 5 | You can create documents with code examples. They look like prompt extracts. |
| 6 | Python provides a module called `doctest` that knows how to extract code |
| 7 | examples out of a text file, to execute them. |
| 8 | |
| 9 | For example, this piece of code:: |
| 10 | |
| 11 | >>> 1 + 1 |
| 12 | 2 |
| 13 | |
| 14 | Can be interpreted by `doctest`, to ensure that the result is 2. |
| 15 | |
| 16 | When an example fails, `doctest` raises an assertion error, like |
| 17 | a classical unit test would do:: |
| 18 | |
| 19 | >>> 1 + 1 |
| 20 | 2 |
| 21 | |
| 22 | |
| 23 |
Note: See TracBrowser
for help on using the browser.
