| 1 |
"""Simple example using doctests. |
| 2 |
|
| 3 |
This file just contains doctests both using plain python and IPython prompts. |
| 4 |
All tests should be loaded by nose. |
| 5 |
""" |
| 6 |
|
| 7 |
import os |
| 8 |
|
| 9 |
|
| 10 |
def pyfunc(): |
| 11 |
"""Some pure python tests... |
| 12 |
|
| 13 |
>>> pyfunc() |
| 14 |
'pyfunc' |
| 15 |
|
| 16 |
>>> import os |
| 17 |
|
| 18 |
>>> 2+3 |
| 19 |
5 |
| 20 |
|
| 21 |
>>> for i in range(3): |
| 22 |
... print(i, end=' ') |
| 23 |
... print(i+1, end=' ') |
| 24 |
... |
| 25 |
0 1 1 2 2 3 |
| 26 |
""" |
| 27 |
return 'pyfunc' |
| 28 |
|
| 29 |
def ipfunc(): |
| 30 |
"""Some ipython tests... |
| 31 |
|
| 32 |
In [1]: import os |
| 33 |
|
| 34 |
In [3]: 2+3 |
| 35 |
Out[3]: 5 |
| 36 |
|
| 37 |
In [26]: for i in range(3): |
| 38 |
....: print(i, end=' ') |
| 39 |
....: print(i+1, end=' ') |
| 40 |
....: |
| 41 |
0 1 1 2 2 3 |
| 42 |
|
| 43 |
|
| 44 |
It's OK to use '_' for the last result, but do NOT try to use IPython's |
| 45 |
numbered history of _NN outputs, since those won't exist under the |
| 46 |
doctest environment: |
| 47 |
|
| 48 |
In [7]: 'hi' |
| 49 |
Out[7]: 'hi' |
| 50 |
|
| 51 |
In [8]: print(repr(_)) |
| 52 |
'hi' |
| 53 |
|
| 54 |
In [7]: 3+4 |
| 55 |
Out[7]: 7 |
| 56 |
|
| 57 |
In [8]: _+3 |
| 58 |
Out[8]: 10 |
| 59 |
|
| 60 |
In [9]: ipfunc() |
| 61 |
Out[9]: 'ipfunc' |
| 62 |
""" |
| 63 |
return "ipfunc" |
| 64 |
|
| 65 |
|
| 66 |
def ipos(): |
| 67 |
"""Examples that access the operating system work: |
| 68 |
|
| 69 |
In [1]: !echo hello |
| 70 |
hello |
| 71 |
|
| 72 |
In [2]: !echo hello > /tmp/foo_iptest |
| 73 |
|
| 74 |
In [3]: !cat /tmp/foo_iptest |
| 75 |
hello |
| 76 |
|
| 77 |
In [4]: rm -f /tmp/foo_iptest |
| 78 |
""" |
| 79 |
pass |
| 80 |
|
| 81 |
|
| 82 |
ipos.__skip_doctest__ = os.name == "nt" |
| 83 |
|
| 84 |
|
| 85 |
def ranfunc(): |
| 86 |
"""A function with some random output. |
| 87 |
|
| 88 |
Normal examples are verified as usual: |
| 89 |
>>> 1+3 |
| 90 |
4 |
| 91 |
|
| 92 |
But if you put '# random' in the output, it is ignored: |
| 93 |
>>> 1+3 |
| 94 |
junk goes here... # random |
| 95 |
|
| 96 |
>>> 1+2 |
| 97 |
again, anything goes #random |
| 98 |
if multiline, the random mark is only needed once. |
| 99 |
|
| 100 |
>>> 1+2 |
| 101 |
You can also put the random marker at the end: |
| 102 |
# random |
| 103 |
|
| 104 |
>>> 1+2 |
| 105 |
# random |
| 106 |
.. or at the beginning. |
| 107 |
|
| 108 |
More correct input is properly verified: |
| 109 |
>>> ranfunc() |
| 110 |
'ranfunc' |
| 111 |
""" |
| 112 |
return 'ranfunc' |
| 113 |
|
| 114 |
|
| 115 |
def random_all(): |
| 116 |
"""A function where we ignore the output of ALL examples. |
| 117 |
|
| 118 |
Examples: |
| 119 |
|
| 120 |
# all-random |
| 121 |
|
| 122 |
This mark tells the testing machinery that all subsequent examples should |
| 123 |
be treated as random (ignoring their output). They are still executed, |
| 124 |
so if a they raise an error, it will be detected as such, but their |
| 125 |
output is completely ignored. |
| 126 |
|
| 127 |
>>> 1+3 |
| 128 |
junk goes here... |
| 129 |
|
| 130 |
>>> 1+3 |
| 131 |
klasdfj; |
| 132 |
|
| 133 |
>>> 1+2 |
| 134 |
again, anything goes |
| 135 |
blah... |
| 136 |
""" |
| 137 |
pass |
| 138 |
|
| 139 |
def iprand(): |
| 140 |
"""Some ipython tests with random output. |
| 141 |
|
| 142 |
In [7]: 3+4 |
| 143 |
Out[7]: 7 |
| 144 |
|
| 145 |
In [8]: print('hello') |
| 146 |
world # random |
| 147 |
|
| 148 |
In [9]: iprand() |
| 149 |
Out[9]: 'iprand' |
| 150 |
""" |
| 151 |
return 'iprand' |
| 152 |
|
| 153 |
def iprand_all(): |
| 154 |
"""Some ipython tests with fully random output. |
| 155 |
|
| 156 |
# all-random |
| 157 |
|
| 158 |
In [7]: 1 |
| 159 |
Out[7]: 99 |
| 160 |
|
| 161 |
In [8]: print('hello') |
| 162 |
world |
| 163 |
|
| 164 |
In [9]: iprand_all() |
| 165 |
Out[9]: 'junk' |
| 166 |
""" |
| 167 |
return 'iprand_all' |