| 1 |
Testing the doctest extension |
| 2 |
============================= |
| 3 |
|
| 4 |
Simple doctest blocks |
| 5 |
--------------------- |
| 6 |
|
| 7 |
>>> 1+1 |
| 8 |
2 |
| 9 |
>>> 1/0 |
| 10 |
Traceback (most recent call last): |
| 11 |
... |
| 12 |
ZeroDivisionError: integer division or modulo by zero |
| 13 |
|
| 14 |
|
| 15 |
Special directives |
| 16 |
------------------ |
| 17 |
|
| 18 |
* doctest |
| 19 |
|
| 20 |
.. doctest:: |
| 21 |
|
| 22 |
>>> 1+1 |
| 23 |
2 |
| 24 |
>>> 1/0 |
| 25 |
Traceback (most recent call last): |
| 26 |
... |
| 27 |
ZeroDivisionError: integer division or modulo by zero |
| 28 |
|
| 29 |
* testcode/testoutput |
| 30 |
|
| 31 |
.. testcode:: |
| 32 |
|
| 33 |
print(1+1) |
| 34 |
|
| 35 |
.. testoutput:: |
| 36 |
|
| 37 |
2 |
| 38 |
|
| 39 |
.. testcode:: |
| 40 |
|
| 41 |
1/0 |
| 42 |
|
| 43 |
.. testoutput:: |
| 44 |
|
| 45 |
Traceback (most recent call last): |
| 46 |
... |
| 47 |
ZeroDivisionError: integer division or modulo by zero |
| 48 |
|
| 49 |
* testsetup |
| 50 |
|
| 51 |
.. testsetup:: * |
| 52 |
|
| 53 |
def squared(x): |
| 54 |
return x * x |
| 55 |
|
| 56 |
.. doctest:: |
| 57 |
|
| 58 |
>>> squared(2) |
| 59 |
4 |
| 60 |
|
| 61 |
.. testcode:: |
| 62 |
|
| 63 |
print(squared(2)) |
| 64 |
|
| 65 |
.. testoutput:: |
| 66 |
|
| 67 |
4 |
| 68 |
|
| 69 |
>>> squared(2) |
| 70 |
4 |
| 71 |
|
| 72 |
* options for doctest/testcode/testoutput blocks |
| 73 |
|
| 74 |
.. testcode:: |
| 75 |
:hide: |
| 76 |
|
| 77 |
print('Output text.') |
| 78 |
|
| 79 |
.. testoutput:: |
| 80 |
:hide: |
| 81 |
:options: +NORMALIZE_WHITESPACE |
| 82 |
|
| 83 |
Output text. |
| 84 |
|
| 85 |
.. doctest:: |
| 86 |
:pyversion: >= 2.0 |
| 87 |
|
| 88 |
>>> a = 3 |
| 89 |
>>> a |
| 90 |
3 |
| 91 |
|
| 92 |
.. doctest:: |
| 93 |
:pyversion: < 2.0 |
| 94 |
|
| 95 |
>>> a = 3 |
| 96 |
>>> a |
| 97 |
4 |
| 98 |
|
| 99 |
* grouping |
| 100 |
|
| 101 |
.. testsetup:: group1 |
| 102 |
|
| 103 |
def add(x, y): |
| 104 |
return x + y |
| 105 |
|
| 106 |
|
| 107 |
``add`` is now known in "group1", but not in others. |
| 108 |
|
| 109 |
.. doctest:: group1 |
| 110 |
|
| 111 |
>>> add(1, 1) |
| 112 |
2 |
| 113 |
|
| 114 |
.. doctest:: group2 |
| 115 |
|
| 116 |
>>> add(1, 1) |
| 117 |
Traceback (most recent call last): |
| 118 |
... |
| 119 |
NameError: name 'add' is not defined |
| 120 |
|
| 121 |
Interleaving testcode/testoutput: |
| 122 |
|
| 123 |
.. testcode:: group1 |
| 124 |
|
| 125 |
print(squared(3)) |
| 126 |
|
| 127 |
.. testcode:: group2 |
| 128 |
|
| 129 |
print(squared(4)) |
| 130 |
|
| 131 |
.. testoutput:: group1 |
| 132 |
|
| 133 |
9 |
| 134 |
|
| 135 |
.. testoutput:: group2 |
| 136 |
|
| 137 |
16 |
| 138 |
|
| 139 |
|
| 140 |
.. testcleanup:: * |
| 141 |
|
| 142 |
from tests.test_extensions import test_ext_doctest |
| 143 |
test_ext_doctest.cleanup_call() |
| 144 |
|
| 145 |
non-ASCII result |
| 146 |
---------------- |
| 147 |
|
| 148 |
>>> print('umlauts: äöü.') |
| 149 |
umlauts: äöü. |
| 150 |
>>> print('Japanese: 日本語') |
| 151 |
Japanese: 日本語 |
| 152 |
|
| 153 |
keep control char in raw string |
| 154 |
------------------------------- |
| 155 |
|
| 156 |
.. doctest:: |
| 157 |
|
| 158 |
>>> print('one\ntwo') |
| 159 |
one |
| 160 |
two |
| 161 |
>>> print(r'one\ntwo') |
| 162 |
one\ntwo |
| 163 |
|