Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1 | """ |
| 2 | Test script for doctest. |
| 3 | """ |
| 4 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 5 | from test import support |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 6 | import doctest |
Tim Peters | a7def72 | 2004-08-23 22:13:22 +0000 | [diff] [blame] | 7 | import warnings |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 8 | |
| 9 | ###################################################################### |
| 10 | ## Sample Objects (used by test cases) |
| 11 | ###################################################################### |
| 12 | |
| 13 | def sample_func(v): |
| 14 | """ |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 15 | Blah blah |
| 16 | |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 17 | >>> print(sample_func(22)) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 18 | 44 |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 19 | |
| 20 | Yee ha! |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 21 | """ |
| 22 | return v+v |
| 23 | |
| 24 | class SampleClass: |
| 25 | """ |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 26 | >>> print(1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 27 | 1 |
Edward Loper | 4ae900f | 2004-09-21 03:20:34 +0000 | [diff] [blame] | 28 | |
| 29 | >>> # comments get ignored. so are empty PS1 and PS2 prompts: |
| 30 | >>> |
| 31 | ... |
| 32 | |
| 33 | Multiline example: |
| 34 | >>> sc = SampleClass(3) |
| 35 | >>> for i in range(10): |
| 36 | ... sc = sc.double() |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 37 | ... print(' ', sc.get(), sep='', end='') |
| 38 | 6 12 24 48 96 192 384 768 1536 3072 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 39 | """ |
| 40 | def __init__(self, val): |
| 41 | """ |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 42 | >>> print(SampleClass(12).get()) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 43 | 12 |
| 44 | """ |
| 45 | self.val = val |
| 46 | |
| 47 | def double(self): |
| 48 | """ |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 49 | >>> print(SampleClass(12).double().get()) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 50 | 24 |
| 51 | """ |
| 52 | return SampleClass(self.val + self.val) |
| 53 | |
| 54 | def get(self): |
| 55 | """ |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 56 | >>> print(SampleClass(-5).get()) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 57 | -5 |
| 58 | """ |
| 59 | return self.val |
| 60 | |
| 61 | def a_staticmethod(v): |
| 62 | """ |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 63 | >>> print(SampleClass.a_staticmethod(10)) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 64 | 11 |
| 65 | """ |
| 66 | return v+1 |
| 67 | a_staticmethod = staticmethod(a_staticmethod) |
| 68 | |
| 69 | def a_classmethod(cls, v): |
| 70 | """ |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 71 | >>> print(SampleClass.a_classmethod(10)) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 72 | 12 |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 73 | >>> print(SampleClass(0).a_classmethod(10)) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 74 | 12 |
| 75 | """ |
| 76 | return v+2 |
| 77 | a_classmethod = classmethod(a_classmethod) |
| 78 | |
| 79 | a_property = property(get, doc=""" |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 80 | >>> print(SampleClass(22).a_property) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 81 | 22 |
| 82 | """) |
| 83 | |
| 84 | class NestedClass: |
| 85 | """ |
| 86 | >>> x = SampleClass.NestedClass(5) |
| 87 | >>> y = x.square() |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 88 | >>> print(y.get()) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 89 | 25 |
| 90 | """ |
| 91 | def __init__(self, val=0): |
| 92 | """ |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 93 | >>> print(SampleClass.NestedClass().get()) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 94 | 0 |
| 95 | """ |
| 96 | self.val = val |
| 97 | def square(self): |
| 98 | return SampleClass.NestedClass(self.val*self.val) |
| 99 | def get(self): |
| 100 | return self.val |
| 101 | |
| 102 | class SampleNewStyleClass(object): |
| 103 | r""" |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 104 | >>> print('1\n2\n3') |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 105 | 1 |
| 106 | 2 |
| 107 | 3 |
| 108 | """ |
| 109 | def __init__(self, val): |
| 110 | """ |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 111 | >>> print(SampleNewStyleClass(12).get()) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 112 | 12 |
| 113 | """ |
| 114 | self.val = val |
| 115 | |
| 116 | def double(self): |
| 117 | """ |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 118 | >>> print(SampleNewStyleClass(12).double().get()) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 119 | 24 |
| 120 | """ |
| 121 | return SampleNewStyleClass(self.val + self.val) |
| 122 | |
| 123 | def get(self): |
| 124 | """ |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 125 | >>> print(SampleNewStyleClass(-5).get()) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 126 | -5 |
| 127 | """ |
| 128 | return self.val |
| 129 | |
| 130 | ###################################################################### |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 131 | ## Fake stdin (for testing interactive debugging) |
| 132 | ###################################################################### |
| 133 | |
| 134 | class _FakeInput: |
| 135 | """ |
| 136 | A fake input stream for pdb's interactive debugger. Whenever a |
| 137 | line is read, print it (to simulate the user typing it), and then |
| 138 | return it. The set of lines to return is specified in the |
| 139 | constructor; they should not have trailing newlines. |
| 140 | """ |
| 141 | def __init__(self, lines): |
| 142 | self.lines = lines |
| 143 | |
| 144 | def readline(self): |
| 145 | line = self.lines.pop(0) |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 146 | print(line) |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 147 | return line+'\n' |
| 148 | |
| 149 | ###################################################################### |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 150 | ## Test Cases |
| 151 | ###################################################################### |
| 152 | |
| 153 | def test_Example(): r""" |
| 154 | Unit tests for the `Example` class. |
| 155 | |
Edward Loper | a6b6832 | 2004-08-26 00:05:43 +0000 | [diff] [blame] | 156 | Example is a simple container class that holds: |
| 157 | - `source`: A source string. |
| 158 | - `want`: An expected output string. |
| 159 | - `exc_msg`: An expected exception message string (or None if no |
| 160 | exception is expected). |
| 161 | - `lineno`: A line number (within the docstring). |
| 162 | - `indent`: The example's indentation in the input string. |
| 163 | - `options`: An option dictionary, mapping option flags to True or |
| 164 | False. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 165 | |
Edward Loper | a6b6832 | 2004-08-26 00:05:43 +0000 | [diff] [blame] | 166 | These attributes are set by the constructor. `source` and `want` are |
| 167 | required; the other attributes all have default values: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 168 | |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 169 | >>> example = doctest.Example('print(1)', '1\n') |
Edward Loper | a6b6832 | 2004-08-26 00:05:43 +0000 | [diff] [blame] | 170 | >>> (example.source, example.want, example.exc_msg, |
| 171 | ... example.lineno, example.indent, example.options) |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 172 | ('print(1)\n', '1\n', None, 0, 0, {}) |
Edward Loper | a6b6832 | 2004-08-26 00:05:43 +0000 | [diff] [blame] | 173 | |
| 174 | The first three attributes (`source`, `want`, and `exc_msg`) may be |
| 175 | specified positionally; the remaining arguments should be specified as |
| 176 | keyword arguments: |
| 177 | |
| 178 | >>> exc_msg = 'IndexError: pop from an empty list' |
| 179 | >>> example = doctest.Example('[].pop()', '', exc_msg, |
| 180 | ... lineno=5, indent=4, |
| 181 | ... options={doctest.ELLIPSIS: True}) |
| 182 | >>> (example.source, example.want, example.exc_msg, |
| 183 | ... example.lineno, example.indent, example.options) |
| 184 | ('[].pop()\n', '', 'IndexError: pop from an empty list\n', 5, 4, {8: True}) |
| 185 | |
| 186 | The constructor normalizes the `source` string to end in a newline: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 187 | |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 188 | Source spans a single line: no terminating newline. |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 189 | >>> e = doctest.Example('print(1)', '1\n') |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 190 | >>> e.source, e.want |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 191 | ('print(1)\n', '1\n') |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 192 | |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 193 | >>> e = doctest.Example('print(1)\n', '1\n') |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 194 | >>> e.source, e.want |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 195 | ('print(1)\n', '1\n') |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 196 | |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 197 | Source spans multiple lines: require terminating newline. |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 198 | >>> e = doctest.Example('print(1);\nprint(2)\n', '1\n2\n') |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 199 | >>> e.source, e.want |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 200 | ('print(1);\nprint(2)\n', '1\n2\n') |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 201 | |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 202 | >>> e = doctest.Example('print(1);\nprint(2)', '1\n2\n') |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 203 | >>> e.source, e.want |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 204 | ('print(1);\nprint(2)\n', '1\n2\n') |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 205 | |
Edward Loper | a6b6832 | 2004-08-26 00:05:43 +0000 | [diff] [blame] | 206 | Empty source string (which should never appear in real examples) |
| 207 | >>> e = doctest.Example('', '') |
| 208 | >>> e.source, e.want |
| 209 | ('\n', '') |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 210 | |
Edward Loper | a6b6832 | 2004-08-26 00:05:43 +0000 | [diff] [blame] | 211 | The constructor normalizes the `want` string to end in a newline, |
| 212 | unless it's the empty string: |
| 213 | |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 214 | >>> e = doctest.Example('print(1)', '1\n') |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 215 | >>> e.source, e.want |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 216 | ('print(1)\n', '1\n') |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 217 | |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 218 | >>> e = doctest.Example('print(1)', '1') |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 219 | >>> e.source, e.want |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 220 | ('print(1)\n', '1\n') |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 221 | |
Edward Loper | a6b6832 | 2004-08-26 00:05:43 +0000 | [diff] [blame] | 222 | >>> e = doctest.Example('print', '') |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 223 | >>> e.source, e.want |
| 224 | ('print\n', '') |
Edward Loper | a6b6832 | 2004-08-26 00:05:43 +0000 | [diff] [blame] | 225 | |
| 226 | The constructor normalizes the `exc_msg` string to end in a newline, |
| 227 | unless it's `None`: |
| 228 | |
| 229 | Message spans one line |
| 230 | >>> exc_msg = 'IndexError: pop from an empty list' |
| 231 | >>> e = doctest.Example('[].pop()', '', exc_msg) |
| 232 | >>> e.exc_msg |
| 233 | 'IndexError: pop from an empty list\n' |
| 234 | |
| 235 | >>> exc_msg = 'IndexError: pop from an empty list\n' |
| 236 | >>> e = doctest.Example('[].pop()', '', exc_msg) |
| 237 | >>> e.exc_msg |
| 238 | 'IndexError: pop from an empty list\n' |
| 239 | |
| 240 | Message spans multiple lines |
| 241 | >>> exc_msg = 'ValueError: 1\n 2' |
| 242 | >>> e = doctest.Example('raise ValueError("1\n 2")', '', exc_msg) |
| 243 | >>> e.exc_msg |
| 244 | 'ValueError: 1\n 2\n' |
| 245 | |
| 246 | >>> exc_msg = 'ValueError: 1\n 2\n' |
| 247 | >>> e = doctest.Example('raise ValueError("1\n 2")', '', exc_msg) |
| 248 | >>> e.exc_msg |
| 249 | 'ValueError: 1\n 2\n' |
| 250 | |
| 251 | Empty (but non-None) exception message (which should never appear |
| 252 | in real examples) |
| 253 | >>> exc_msg = '' |
| 254 | >>> e = doctest.Example('raise X()', '', exc_msg) |
| 255 | >>> e.exc_msg |
| 256 | '\n' |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 257 | """ |
| 258 | |
| 259 | def test_DocTest(): r""" |
| 260 | Unit tests for the `DocTest` class. |
| 261 | |
| 262 | DocTest is a collection of examples, extracted from a docstring, along |
| 263 | with information about where the docstring comes from (a name, |
| 264 | filename, and line number). The docstring is parsed by the `DocTest` |
| 265 | constructor: |
| 266 | |
| 267 | >>> docstring = ''' |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 268 | ... >>> print(12) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 269 | ... 12 |
| 270 | ... |
| 271 | ... Non-example text. |
| 272 | ... |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 273 | ... >>> print('another\example') |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 274 | ... another |
| 275 | ... example |
| 276 | ... ''' |
| 277 | >>> globs = {} # globals to run the test in. |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 278 | >>> parser = doctest.DocTestParser() |
| 279 | >>> test = parser.get_doctest(docstring, globs, 'some_test', |
| 280 | ... 'some_file', 20) |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 281 | >>> print(test) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 282 | <DocTest some_test from some_file:20 (2 examples)> |
| 283 | >>> len(test.examples) |
| 284 | 2 |
| 285 | >>> e1, e2 = test.examples |
| 286 | >>> (e1.source, e1.want, e1.lineno) |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 287 | ('print(12)\n', '12\n', 1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 288 | >>> (e2.source, e2.want, e2.lineno) |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 289 | ("print('another\\example')\n", 'another\nexample\n', 6) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 290 | |
| 291 | Source information (name, filename, and line number) is available as |
| 292 | attributes on the doctest object: |
| 293 | |
| 294 | >>> (test.name, test.filename, test.lineno) |
| 295 | ('some_test', 'some_file', 20) |
| 296 | |
| 297 | The line number of an example within its containing file is found by |
| 298 | adding the line number of the example and the line number of its |
| 299 | containing test: |
| 300 | |
| 301 | >>> test.lineno + e1.lineno |
| 302 | 21 |
| 303 | >>> test.lineno + e2.lineno |
| 304 | 26 |
| 305 | |
| 306 | If the docstring contains inconsistant leading whitespace in the |
| 307 | expected output of an example, then `DocTest` will raise a ValueError: |
| 308 | |
| 309 | >>> docstring = r''' |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 310 | ... >>> print('bad\nindentation') |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 311 | ... bad |
| 312 | ... indentation |
| 313 | ... ''' |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 314 | >>> parser.get_doctest(docstring, globs, 'some_test', 'filename', 0) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 315 | Traceback (most recent call last): |
Edward Loper | 00f8da7 | 2004-08-26 18:05:07 +0000 | [diff] [blame] | 316 | ValueError: line 4 of the docstring for some_test has inconsistent leading whitespace: 'indentation' |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 317 | |
| 318 | If the docstring contains inconsistent leading whitespace on |
| 319 | continuation lines, then `DocTest` will raise a ValueError: |
| 320 | |
| 321 | >>> docstring = r''' |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 322 | ... >>> print(('bad indentation', |
| 323 | ... ... 2)) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 324 | ... ('bad', 'indentation') |
| 325 | ... ''' |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 326 | >>> parser.get_doctest(docstring, globs, 'some_test', 'filename', 0) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 327 | Traceback (most recent call last): |
Guido van Rossum | e0192e5 | 2007-02-09 23:39:59 +0000 | [diff] [blame] | 328 | ValueError: line 2 of the docstring for some_test has inconsistent leading whitespace: '... 2))' |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 329 | |
| 330 | If there's no blank space after a PS1 prompt ('>>>'), then `DocTest` |
| 331 | will raise a ValueError: |
| 332 | |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 333 | >>> docstring = '>>>print(1)\n1' |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 334 | >>> parser.get_doctest(docstring, globs, 'some_test', 'filename', 0) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 335 | Traceback (most recent call last): |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 336 | ValueError: line 1 of the docstring for some_test lacks blank after >>>: '>>>print(1)' |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 337 | |
| 338 | If there's no blank space after a PS2 prompt ('...'), then `DocTest` |
| 339 | will raise a ValueError: |
| 340 | |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 341 | >>> docstring = '>>> if 1:\n...print(1)\n1' |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 342 | >>> parser.get_doctest(docstring, globs, 'some_test', 'filename', 0) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 343 | Traceback (most recent call last): |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 344 | ValueError: line 2 of the docstring for some_test lacks blank after ...: '...print(1)' |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 345 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 346 | """ |
| 347 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 348 | def test_DocTestFinder(): r""" |
| 349 | Unit tests for the `DocTestFinder` class. |
| 350 | |
| 351 | DocTestFinder is used to extract DocTests from an object's docstring |
| 352 | and the docstrings of its contained objects. It can be used with |
| 353 | modules, functions, classes, methods, staticmethods, classmethods, and |
| 354 | properties. |
| 355 | |
| 356 | Finding Tests in Functions |
| 357 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 358 | For a function whose docstring contains examples, DocTestFinder.find() |
| 359 | will return a single test (for that function's docstring): |
| 360 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 361 | >>> finder = doctest.DocTestFinder() |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 362 | |
| 363 | We'll simulate a __file__ attr that ends in pyc: |
| 364 | |
| 365 | >>> import test.test_doctest |
| 366 | >>> old = test.test_doctest.__file__ |
| 367 | >>> test.test_doctest.__file__ = 'test_doctest.pyc' |
| 368 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 369 | >>> tests = finder.find(sample_func) |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 370 | |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 371 | >>> print(tests) # doctest: +ELLIPSIS |
Tim Peters | a7def72 | 2004-08-23 22:13:22 +0000 | [diff] [blame] | 372 | [<DocTest sample_func from ...:13 (1 example)>] |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 373 | |
Tim Peters | 4de7c5c | 2004-08-23 22:38:05 +0000 | [diff] [blame] | 374 | The exact name depends on how test_doctest was invoked, so allow for |
| 375 | leading path components. |
| 376 | |
| 377 | >>> tests[0].filename # doctest: +ELLIPSIS |
| 378 | '...test_doctest.py' |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 379 | |
| 380 | >>> test.test_doctest.__file__ = old |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 381 | |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 382 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 383 | >>> e = tests[0].examples[0] |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 384 | >>> (e.source, e.want, e.lineno) |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 385 | ('print(sample_func(22))\n', '44\n', 3) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 386 | |
Edward Loper | 32ddbf7 | 2004-09-13 05:47:24 +0000 | [diff] [blame] | 387 | By default, tests are created for objects with no docstring: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 388 | |
| 389 | >>> def no_docstring(v): |
| 390 | ... pass |
Tim Peters | 958cc89 | 2004-09-13 14:53:28 +0000 | [diff] [blame] | 391 | >>> finder.find(no_docstring) |
| 392 | [] |
Edward Loper | 32ddbf7 | 2004-09-13 05:47:24 +0000 | [diff] [blame] | 393 | |
| 394 | However, the optional argument `exclude_empty` to the DocTestFinder |
| 395 | constructor can be used to exclude tests for objects with empty |
| 396 | docstrings: |
| 397 | |
| 398 | >>> def no_docstring(v): |
| 399 | ... pass |
| 400 | >>> excl_empty_finder = doctest.DocTestFinder(exclude_empty=True) |
| 401 | >>> excl_empty_finder.find(no_docstring) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 402 | [] |
| 403 | |
| 404 | If the function has a docstring with no examples, then a test with no |
| 405 | examples is returned. (This lets `DocTestRunner` collect statistics |
| 406 | about which functions have no tests -- but is that useful? And should |
| 407 | an empty test also be created when there's no docstring?) |
| 408 | |
| 409 | >>> def no_examples(v): |
| 410 | ... ''' no doctest examples ''' |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 411 | >>> finder.find(no_examples) # doctest: +ELLIPSIS |
| 412 | [<DocTest no_examples from ...:1 (no examples)>] |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 413 | |
| 414 | Finding Tests in Classes |
| 415 | ~~~~~~~~~~~~~~~~~~~~~~~~ |
| 416 | For a class, DocTestFinder will create a test for the class's |
| 417 | docstring, and will recursively explore its contents, including |
| 418 | methods, classmethods, staticmethods, properties, and nested classes. |
| 419 | |
| 420 | >>> finder = doctest.DocTestFinder() |
| 421 | >>> tests = finder.find(SampleClass) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 422 | >>> for t in tests: |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 423 | ... print('%2s %s' % (len(t.examples), t.name)) |
Edward Loper | 4ae900f | 2004-09-21 03:20:34 +0000 | [diff] [blame] | 424 | 3 SampleClass |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 425 | 3 SampleClass.NestedClass |
| 426 | 1 SampleClass.NestedClass.__init__ |
| 427 | 1 SampleClass.__init__ |
| 428 | 2 SampleClass.a_classmethod |
| 429 | 1 SampleClass.a_property |
| 430 | 1 SampleClass.a_staticmethod |
| 431 | 1 SampleClass.double |
| 432 | 1 SampleClass.get |
| 433 | |
| 434 | New-style classes are also supported: |
| 435 | |
| 436 | >>> tests = finder.find(SampleNewStyleClass) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 437 | >>> for t in tests: |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 438 | ... print('%2s %s' % (len(t.examples), t.name)) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 439 | 1 SampleNewStyleClass |
| 440 | 1 SampleNewStyleClass.__init__ |
| 441 | 1 SampleNewStyleClass.double |
| 442 | 1 SampleNewStyleClass.get |
| 443 | |
| 444 | Finding Tests in Modules |
| 445 | ~~~~~~~~~~~~~~~~~~~~~~~~ |
| 446 | For a module, DocTestFinder will create a test for the class's |
| 447 | docstring, and will recursively explore its contents, including |
| 448 | functions, classes, and the `__test__` dictionary, if it exists: |
| 449 | |
| 450 | >>> # A module |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 451 | >>> import types |
| 452 | >>> m = types.ModuleType('some_module') |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 453 | >>> def triple(val): |
| 454 | ... ''' |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 455 | ... >>> print(triple(11)) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 456 | ... 33 |
| 457 | ... ''' |
| 458 | ... return val*3 |
| 459 | >>> m.__dict__.update({ |
| 460 | ... 'sample_func': sample_func, |
| 461 | ... 'SampleClass': SampleClass, |
| 462 | ... '__doc__': ''' |
| 463 | ... Module docstring. |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 464 | ... >>> print('module') |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 465 | ... module |
| 466 | ... ''', |
| 467 | ... '__test__': { |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 468 | ... 'd': '>>> print(6)\n6\n>>> print(7)\n7\n', |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 469 | ... 'c': triple}}) |
| 470 | |
| 471 | >>> finder = doctest.DocTestFinder() |
| 472 | >>> # Use module=test.test_doctest, to prevent doctest from |
| 473 | >>> # ignoring the objects since they weren't defined in m. |
| 474 | >>> import test.test_doctest |
| 475 | >>> tests = finder.find(m, module=test.test_doctest) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 476 | >>> for t in tests: |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 477 | ... print('%2s %s' % (len(t.examples), t.name)) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 478 | 1 some_module |
Edward Loper | 4ae900f | 2004-09-21 03:20:34 +0000 | [diff] [blame] | 479 | 3 some_module.SampleClass |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 480 | 3 some_module.SampleClass.NestedClass |
| 481 | 1 some_module.SampleClass.NestedClass.__init__ |
| 482 | 1 some_module.SampleClass.__init__ |
| 483 | 2 some_module.SampleClass.a_classmethod |
| 484 | 1 some_module.SampleClass.a_property |
| 485 | 1 some_module.SampleClass.a_staticmethod |
| 486 | 1 some_module.SampleClass.double |
| 487 | 1 some_module.SampleClass.get |
Tim Peters | c568478 | 2004-09-13 01:07:12 +0000 | [diff] [blame] | 488 | 1 some_module.__test__.c |
| 489 | 2 some_module.__test__.d |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 490 | 1 some_module.sample_func |
| 491 | |
| 492 | Duplicate Removal |
| 493 | ~~~~~~~~~~~~~~~~~ |
| 494 | If a single object is listed twice (under different names), then tests |
| 495 | will only be generated for it once: |
| 496 | |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 497 | >>> from test import doctest_aliases |
Edward Loper | 32ddbf7 | 2004-09-13 05:47:24 +0000 | [diff] [blame] | 498 | >>> tests = excl_empty_finder.find(doctest_aliases) |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 499 | >>> print(len(tests)) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 500 | 2 |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 501 | >>> print(tests[0].name) |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 502 | test.doctest_aliases.TwoNames |
| 503 | |
| 504 | TwoNames.f and TwoNames.g are bound to the same object. |
| 505 | We can't guess which will be found in doctest's traversal of |
| 506 | TwoNames.__dict__ first, so we have to allow for either. |
| 507 | |
| 508 | >>> tests[1].name.split('.')[-1] in ['f', 'g'] |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 509 | True |
| 510 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 511 | Empty Tests |
| 512 | ~~~~~~~~~~~ |
| 513 | By default, an object with no doctests doesn't create any tests: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 514 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 515 | >>> tests = doctest.DocTestFinder().find(SampleClass) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 516 | >>> for t in tests: |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 517 | ... print('%2s %s' % (len(t.examples), t.name)) |
Edward Loper | 4ae900f | 2004-09-21 03:20:34 +0000 | [diff] [blame] | 518 | 3 SampleClass |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 519 | 3 SampleClass.NestedClass |
| 520 | 1 SampleClass.NestedClass.__init__ |
Tim Peters | 958cc89 | 2004-09-13 14:53:28 +0000 | [diff] [blame] | 521 | 1 SampleClass.__init__ |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 522 | 2 SampleClass.a_classmethod |
| 523 | 1 SampleClass.a_property |
| 524 | 1 SampleClass.a_staticmethod |
Tim Peters | 958cc89 | 2004-09-13 14:53:28 +0000 | [diff] [blame] | 525 | 1 SampleClass.double |
| 526 | 1 SampleClass.get |
| 527 | |
| 528 | By default, that excluded objects with no doctests. exclude_empty=False |
| 529 | tells it to include (empty) tests for objects with no doctests. This feature |
| 530 | is really to support backward compatibility in what doctest.master.summarize() |
| 531 | displays. |
| 532 | |
Thomas Wouters | 73e5a5b | 2006-06-08 15:35:45 +0000 | [diff] [blame] | 533 | >>> tests = doctest.DocTestFinder(exclude_empty=False).find(SampleClass) |
Tim Peters | 958cc89 | 2004-09-13 14:53:28 +0000 | [diff] [blame] | 534 | >>> for t in tests: |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 535 | ... print('%2s %s' % (len(t.examples), t.name)) |
Edward Loper | 4ae900f | 2004-09-21 03:20:34 +0000 | [diff] [blame] | 536 | 3 SampleClass |
Tim Peters | 958cc89 | 2004-09-13 14:53:28 +0000 | [diff] [blame] | 537 | 3 SampleClass.NestedClass |
| 538 | 1 SampleClass.NestedClass.__init__ |
Edward Loper | 32ddbf7 | 2004-09-13 05:47:24 +0000 | [diff] [blame] | 539 | 0 SampleClass.NestedClass.get |
| 540 | 0 SampleClass.NestedClass.square |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 541 | 1 SampleClass.__init__ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 542 | 2 SampleClass.a_classmethod |
| 543 | 1 SampleClass.a_property |
| 544 | 1 SampleClass.a_staticmethod |
| 545 | 1 SampleClass.double |
| 546 | 1 SampleClass.get |
| 547 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 548 | Turning off Recursion |
| 549 | ~~~~~~~~~~~~~~~~~~~~~ |
| 550 | DocTestFinder can be told not to look for tests in contained objects |
| 551 | using the `recurse` flag: |
| 552 | |
| 553 | >>> tests = doctest.DocTestFinder(recurse=False).find(SampleClass) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 554 | >>> for t in tests: |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 555 | ... print('%2s %s' % (len(t.examples), t.name)) |
Edward Loper | 4ae900f | 2004-09-21 03:20:34 +0000 | [diff] [blame] | 556 | 3 SampleClass |
Edward Loper | b51b234 | 2004-08-17 16:37:12 +0000 | [diff] [blame] | 557 | |
| 558 | Line numbers |
| 559 | ~~~~~~~~~~~~ |
| 560 | DocTestFinder finds the line number of each example: |
| 561 | |
| 562 | >>> def f(x): |
| 563 | ... ''' |
| 564 | ... >>> x = 12 |
| 565 | ... |
| 566 | ... some text |
| 567 | ... |
| 568 | ... >>> # examples are not created for comments & bare prompts. |
| 569 | ... >>> |
| 570 | ... ... |
| 571 | ... |
| 572 | ... >>> for x in range(10): |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 573 | ... ... print(x, end=' ') |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 574 | ... 0 1 2 3 4 5 6 7 8 9 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 575 | ... >>> x//2 |
| 576 | ... 6 |
Edward Loper | b51b234 | 2004-08-17 16:37:12 +0000 | [diff] [blame] | 577 | ... ''' |
| 578 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 579 | >>> [e.lineno for e in test.examples] |
| 580 | [1, 9, 12] |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 581 | """ |
| 582 | |
Edward Loper | 00f8da7 | 2004-08-26 18:05:07 +0000 | [diff] [blame] | 583 | def test_DocTestParser(): r""" |
| 584 | Unit tests for the `DocTestParser` class. |
| 585 | |
| 586 | DocTestParser is used to parse docstrings containing doctest examples. |
| 587 | |
| 588 | The `parse` method divides a docstring into examples and intervening |
| 589 | text: |
| 590 | |
| 591 | >>> s = ''' |
| 592 | ... >>> x, y = 2, 3 # no output expected |
| 593 | ... >>> if 1: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 594 | ... ... print(x) |
| 595 | ... ... print(y) |
Edward Loper | 00f8da7 | 2004-08-26 18:05:07 +0000 | [diff] [blame] | 596 | ... 2 |
| 597 | ... 3 |
| 598 | ... |
| 599 | ... Some text. |
| 600 | ... >>> x+y |
| 601 | ... 5 |
| 602 | ... ''' |
| 603 | >>> parser = doctest.DocTestParser() |
| 604 | >>> for piece in parser.parse(s): |
| 605 | ... if isinstance(piece, doctest.Example): |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 606 | ... print('Example:', (piece.source, piece.want, piece.lineno)) |
Edward Loper | 00f8da7 | 2004-08-26 18:05:07 +0000 | [diff] [blame] | 607 | ... else: |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 608 | ... print(' Text:', repr(piece)) |
Edward Loper | 00f8da7 | 2004-08-26 18:05:07 +0000 | [diff] [blame] | 609 | Text: '\n' |
| 610 | Example: ('x, y = 2, 3 # no output expected\n', '', 1) |
| 611 | Text: '' |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 612 | Example: ('if 1:\n print(x)\n print(y)\n', '2\n3\n', 2) |
Edward Loper | 00f8da7 | 2004-08-26 18:05:07 +0000 | [diff] [blame] | 613 | Text: '\nSome text.\n' |
| 614 | Example: ('x+y\n', '5\n', 9) |
| 615 | Text: '' |
| 616 | |
| 617 | The `get_examples` method returns just the examples: |
| 618 | |
| 619 | >>> for piece in parser.get_examples(s): |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 620 | ... print((piece.source, piece.want, piece.lineno)) |
Edward Loper | 00f8da7 | 2004-08-26 18:05:07 +0000 | [diff] [blame] | 621 | ('x, y = 2, 3 # no output expected\n', '', 1) |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 622 | ('if 1:\n print(x)\n print(y)\n', '2\n3\n', 2) |
Edward Loper | 00f8da7 | 2004-08-26 18:05:07 +0000 | [diff] [blame] | 623 | ('x+y\n', '5\n', 9) |
| 624 | |
| 625 | The `get_doctest` method creates a Test from the examples, along with the |
| 626 | given arguments: |
| 627 | |
| 628 | >>> test = parser.get_doctest(s, {}, 'name', 'filename', lineno=5) |
| 629 | >>> (test.name, test.filename, test.lineno) |
| 630 | ('name', 'filename', 5) |
| 631 | >>> for piece in test.examples: |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 632 | ... print((piece.source, piece.want, piece.lineno)) |
Edward Loper | 00f8da7 | 2004-08-26 18:05:07 +0000 | [diff] [blame] | 633 | ('x, y = 2, 3 # no output expected\n', '', 1) |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 634 | ('if 1:\n print(x)\n print(y)\n', '2\n3\n', 2) |
Edward Loper | 00f8da7 | 2004-08-26 18:05:07 +0000 | [diff] [blame] | 635 | ('x+y\n', '5\n', 9) |
| 636 | """ |
| 637 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 638 | class test_DocTestRunner: |
| 639 | def basics(): r""" |
| 640 | Unit tests for the `DocTestRunner` class. |
| 641 | |
| 642 | DocTestRunner is used to run DocTest test cases, and to accumulate |
| 643 | statistics. Here's a simple DocTest case we can use: |
| 644 | |
| 645 | >>> def f(x): |
| 646 | ... ''' |
| 647 | ... >>> x = 12 |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 648 | ... >>> print(x) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 649 | ... 12 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 650 | ... >>> x//2 |
| 651 | ... 6 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 652 | ... ''' |
| 653 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 654 | |
| 655 | The main DocTestRunner interface is the `run` method, which runs a |
| 656 | given DocTest case in a given namespace (globs). It returns a tuple |
| 657 | `(f,t)`, where `f` is the number of failed tests and `t` is the number |
| 658 | of tried tests. |
| 659 | |
| 660 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 661 | TestResults(failed=0, attempted=3) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 662 | |
| 663 | If any example produces incorrect output, then the test runner reports |
| 664 | the failure and proceeds to the next example: |
| 665 | |
| 666 | >>> def f(x): |
| 667 | ... ''' |
| 668 | ... >>> x = 12 |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 669 | ... >>> print(x) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 670 | ... 14 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 671 | ... >>> x//2 |
| 672 | ... 6 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 673 | ... ''' |
| 674 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 675 | >>> doctest.DocTestRunner(verbose=True).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 676 | ... # doctest: +ELLIPSIS |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 677 | Trying: |
| 678 | x = 12 |
| 679 | Expecting nothing |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 680 | ok |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 681 | Trying: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 682 | print(x) |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 683 | Expecting: |
| 684 | 14 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 685 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 686 | File ..., line 4, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 687 | Failed example: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 688 | print(x) |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 689 | Expected: |
| 690 | 14 |
| 691 | Got: |
| 692 | 12 |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 693 | Trying: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 694 | x//2 |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 695 | Expecting: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 696 | 6 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 697 | ok |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 698 | TestResults(failed=1, attempted=3) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 699 | """ |
| 700 | def verbose_flag(): r""" |
| 701 | The `verbose` flag makes the test runner generate more detailed |
| 702 | output: |
| 703 | |
| 704 | >>> def f(x): |
| 705 | ... ''' |
| 706 | ... >>> x = 12 |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 707 | ... >>> print(x) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 708 | ... 12 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 709 | ... >>> x//2 |
| 710 | ... 6 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 711 | ... ''' |
| 712 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 713 | |
| 714 | >>> doctest.DocTestRunner(verbose=True).run(test) |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 715 | Trying: |
| 716 | x = 12 |
| 717 | Expecting nothing |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 718 | ok |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 719 | Trying: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 720 | print(x) |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 721 | Expecting: |
| 722 | 12 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 723 | ok |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 724 | Trying: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 725 | x//2 |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 726 | Expecting: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 727 | 6 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 728 | ok |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 729 | TestResults(failed=0, attempted=3) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 730 | |
| 731 | If the `verbose` flag is unspecified, then the output will be verbose |
| 732 | iff `-v` appears in sys.argv: |
| 733 | |
| 734 | >>> # Save the real sys.argv list. |
| 735 | >>> old_argv = sys.argv |
| 736 | |
| 737 | >>> # If -v does not appear in sys.argv, then output isn't verbose. |
| 738 | >>> sys.argv = ['test'] |
| 739 | >>> doctest.DocTestRunner().run(test) |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 740 | TestResults(failed=0, attempted=3) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 741 | |
| 742 | >>> # If -v does appear in sys.argv, then output is verbose. |
| 743 | >>> sys.argv = ['test', '-v'] |
| 744 | >>> doctest.DocTestRunner().run(test) |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 745 | Trying: |
| 746 | x = 12 |
| 747 | Expecting nothing |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 748 | ok |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 749 | Trying: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 750 | print(x) |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 751 | Expecting: |
| 752 | 12 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 753 | ok |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 754 | Trying: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 755 | x//2 |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 756 | Expecting: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 757 | 6 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 758 | ok |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 759 | TestResults(failed=0, attempted=3) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 760 | |
| 761 | >>> # Restore sys.argv |
| 762 | >>> sys.argv = old_argv |
| 763 | |
| 764 | In the remaining examples, the test runner's verbosity will be |
| 765 | explicitly set, to ensure that the test behavior is consistent. |
| 766 | """ |
| 767 | def exceptions(): r""" |
| 768 | Tests of `DocTestRunner`'s exception handling. |
| 769 | |
| 770 | An expected exception is specified with a traceback message. The |
| 771 | lines between the first line and the type/value may be omitted or |
| 772 | replaced with any other string: |
| 773 | |
| 774 | >>> def f(x): |
| 775 | ... ''' |
| 776 | ... >>> x = 12 |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 777 | ... >>> print(x//0) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 778 | ... Traceback (most recent call last): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 779 | ... ZeroDivisionError: integer division or modulo by zero |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 780 | ... ''' |
| 781 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 782 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 783 | TestResults(failed=0, attempted=2) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 784 | |
Edward Loper | 19b1958 | 2004-08-25 23:07:03 +0000 | [diff] [blame] | 785 | An example may not generate output before it raises an exception; if |
| 786 | it does, then the traceback message will not be recognized as |
| 787 | signaling an expected exception, so the example will be reported as an |
| 788 | unexpected exception: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 789 | |
| 790 | >>> def f(x): |
| 791 | ... ''' |
| 792 | ... >>> x = 12 |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 793 | ... >>> print('pre-exception output', x//0) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 794 | ... pre-exception output |
| 795 | ... Traceback (most recent call last): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 796 | ... ZeroDivisionError: integer division or modulo by zero |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 797 | ... ''' |
| 798 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 799 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Edward Loper | 19b1958 | 2004-08-25 23:07:03 +0000 | [diff] [blame] | 800 | ... # doctest: +ELLIPSIS |
| 801 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 802 | File ..., line 4, in f |
Edward Loper | 19b1958 | 2004-08-25 23:07:03 +0000 | [diff] [blame] | 803 | Failed example: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 804 | print('pre-exception output', x//0) |
Edward Loper | 19b1958 | 2004-08-25 23:07:03 +0000 | [diff] [blame] | 805 | Exception raised: |
| 806 | ... |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 807 | ZeroDivisionError: integer division or modulo by zero |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 808 | TestResults(failed=1, attempted=2) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 809 | |
| 810 | Exception messages may contain newlines: |
| 811 | |
| 812 | >>> def f(x): |
| 813 | ... r''' |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 814 | ... >>> raise ValueError('multi\nline\nmessage') |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 815 | ... Traceback (most recent call last): |
| 816 | ... ValueError: multi |
| 817 | ... line |
| 818 | ... message |
| 819 | ... ''' |
| 820 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 821 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 822 | TestResults(failed=0, attempted=1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 823 | |
| 824 | If an exception is expected, but an exception with the wrong type or |
| 825 | message is raised, then it is reported as a failure: |
| 826 | |
| 827 | >>> def f(x): |
| 828 | ... r''' |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 829 | ... >>> raise ValueError('message') |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 830 | ... Traceback (most recent call last): |
| 831 | ... ValueError: wrong message |
| 832 | ... ''' |
| 833 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 834 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 835 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 836 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 837 | File ..., line 3, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 838 | Failed example: |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 839 | raise ValueError('message') |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 840 | Expected: |
| 841 | Traceback (most recent call last): |
| 842 | ValueError: wrong message |
| 843 | Got: |
| 844 | Traceback (most recent call last): |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 845 | ... |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 846 | ValueError: message |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 847 | TestResults(failed=1, attempted=1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 848 | |
Tim Peters | 1fbf9c5 | 2004-09-04 17:21:02 +0000 | [diff] [blame] | 849 | However, IGNORE_EXCEPTION_DETAIL can be used to allow a mismatch in the |
| 850 | detail: |
| 851 | |
| 852 | >>> def f(x): |
| 853 | ... r''' |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 854 | ... >>> raise ValueError('message') #doctest: +IGNORE_EXCEPTION_DETAIL |
Tim Peters | 1fbf9c5 | 2004-09-04 17:21:02 +0000 | [diff] [blame] | 855 | ... Traceback (most recent call last): |
| 856 | ... ValueError: wrong message |
| 857 | ... ''' |
| 858 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 859 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 860 | TestResults(failed=0, attempted=1) |
Tim Peters | 1fbf9c5 | 2004-09-04 17:21:02 +0000 | [diff] [blame] | 861 | |
| 862 | But IGNORE_EXCEPTION_DETAIL does not allow a mismatch in the exception type: |
| 863 | |
| 864 | >>> def f(x): |
| 865 | ... r''' |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 866 | ... >>> raise ValueError('message') #doctest: +IGNORE_EXCEPTION_DETAIL |
Tim Peters | 1fbf9c5 | 2004-09-04 17:21:02 +0000 | [diff] [blame] | 867 | ... Traceback (most recent call last): |
| 868 | ... TypeError: wrong type |
| 869 | ... ''' |
| 870 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 871 | >>> doctest.DocTestRunner(verbose=False).run(test) |
| 872 | ... # doctest: +ELLIPSIS |
| 873 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 874 | File ..., line 3, in f |
Tim Peters | 1fbf9c5 | 2004-09-04 17:21:02 +0000 | [diff] [blame] | 875 | Failed example: |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 876 | raise ValueError('message') #doctest: +IGNORE_EXCEPTION_DETAIL |
Tim Peters | 1fbf9c5 | 2004-09-04 17:21:02 +0000 | [diff] [blame] | 877 | Expected: |
| 878 | Traceback (most recent call last): |
| 879 | TypeError: wrong type |
| 880 | Got: |
| 881 | Traceback (most recent call last): |
| 882 | ... |
| 883 | ValueError: message |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 884 | TestResults(failed=1, attempted=1) |
Tim Peters | 1fbf9c5 | 2004-09-04 17:21:02 +0000 | [diff] [blame] | 885 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 886 | If an exception is raised but not expected, then it is reported as an |
| 887 | unexpected exception: |
| 888 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 889 | >>> def f(x): |
| 890 | ... r''' |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 891 | ... >>> 1//0 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 892 | ... 0 |
| 893 | ... ''' |
| 894 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 895 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 896 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 897 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 898 | File ..., line 3, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 899 | Failed example: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 900 | 1//0 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 901 | Exception raised: |
| 902 | Traceback (most recent call last): |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 903 | ... |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 904 | ZeroDivisionError: integer division or modulo by zero |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 905 | TestResults(failed=1, attempted=1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 906 | """ |
| 907 | def optionflags(): r""" |
| 908 | Tests of `DocTestRunner`'s option flag handling. |
| 909 | |
| 910 | Several option flags can be used to customize the behavior of the test |
| 911 | runner. These are defined as module constants in doctest, and passed |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 912 | to the DocTestRunner constructor (multiple constants should be ORed |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 913 | together). |
| 914 | |
| 915 | The DONT_ACCEPT_TRUE_FOR_1 flag disables matches between True/False |
| 916 | and 1/0: |
| 917 | |
| 918 | >>> def f(x): |
| 919 | ... '>>> True\n1\n' |
| 920 | |
| 921 | >>> # Without the flag: |
| 922 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 923 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 924 | TestResults(failed=0, attempted=1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 925 | |
| 926 | >>> # With the flag: |
| 927 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 928 | >>> flags = doctest.DONT_ACCEPT_TRUE_FOR_1 |
| 929 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 930 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 931 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 932 | File ..., line 2, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 933 | Failed example: |
| 934 | True |
| 935 | Expected: |
| 936 | 1 |
| 937 | Got: |
| 938 | True |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 939 | TestResults(failed=1, attempted=1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 940 | |
| 941 | The DONT_ACCEPT_BLANKLINE flag disables the match between blank lines |
| 942 | and the '<BLANKLINE>' marker: |
| 943 | |
| 944 | >>> def f(x): |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 945 | ... '>>> print("a\\n\\nb")\na\n<BLANKLINE>\nb\n' |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 946 | |
| 947 | >>> # Without the flag: |
| 948 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 949 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 950 | TestResults(failed=0, attempted=1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 951 | |
| 952 | >>> # With the flag: |
| 953 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 954 | >>> flags = doctest.DONT_ACCEPT_BLANKLINE |
| 955 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 956 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 957 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 958 | File ..., line 2, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 959 | Failed example: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 960 | print("a\n\nb") |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 961 | Expected: |
| 962 | a |
| 963 | <BLANKLINE> |
| 964 | b |
| 965 | Got: |
| 966 | a |
| 967 | <BLANKLINE> |
| 968 | b |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 969 | TestResults(failed=1, attempted=1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 970 | |
| 971 | The NORMALIZE_WHITESPACE flag causes all sequences of whitespace to be |
| 972 | treated as equal: |
| 973 | |
| 974 | >>> def f(x): |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 975 | ... '>>> print(1, 2, 3)\n 1 2\n 3' |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 976 | |
| 977 | >>> # Without the flag: |
| 978 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 979 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 980 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 981 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 982 | File ..., line 2, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 983 | Failed example: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 984 | print(1, 2, 3) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 985 | Expected: |
| 986 | 1 2 |
| 987 | 3 |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 988 | Got: |
| 989 | 1 2 3 |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 990 | TestResults(failed=1, attempted=1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 991 | |
| 992 | >>> # With the flag: |
| 993 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 994 | >>> flags = doctest.NORMALIZE_WHITESPACE |
| 995 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 996 | TestResults(failed=0, attempted=1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 997 | |
Tim Peters | 026f8dc | 2004-08-19 16:38:58 +0000 | [diff] [blame] | 998 | An example from the docs: |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 999 | >>> print(list(range(20))) #doctest: +NORMALIZE_WHITESPACE |
Tim Peters | 026f8dc | 2004-08-19 16:38:58 +0000 | [diff] [blame] | 1000 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, |
| 1001 | 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] |
| 1002 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1003 | The ELLIPSIS flag causes ellipsis marker ("...") in the expected |
| 1004 | output to match any substring in the actual output: |
| 1005 | |
| 1006 | >>> def f(x): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1007 | ... '>>> print(list(range(15)))\n[0, 1, 2, ..., 14]\n' |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1008 | |
| 1009 | >>> # Without the flag: |
| 1010 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1011 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1012 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1013 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1014 | File ..., line 2, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1015 | Failed example: |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1016 | print(list(range(15))) |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1017 | Expected: |
| 1018 | [0, 1, 2, ..., 14] |
| 1019 | Got: |
| 1020 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1021 | TestResults(failed=1, attempted=1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1022 | |
| 1023 | >>> # With the flag: |
| 1024 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1025 | >>> flags = doctest.ELLIPSIS |
| 1026 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1027 | TestResults(failed=0, attempted=1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1028 | |
Tim Peters | e594bee | 2004-08-22 01:47:51 +0000 | [diff] [blame] | 1029 | ... also matches nothing: |
Tim Peters | 1cf3aa6 | 2004-08-19 06:49:33 +0000 | [diff] [blame] | 1030 | |
Guido van Rossum | e0192e5 | 2007-02-09 23:39:59 +0000 | [diff] [blame] | 1031 | >>> if 1: |
| 1032 | ... for i in range(100): |
| 1033 | ... print(i**2, end=' ') #doctest: +ELLIPSIS |
| 1034 | ... print('!') |
| 1035 | 0 1...4...9 16 ... 36 49 64 ... 9801 ! |
Tim Peters | 1cf3aa6 | 2004-08-19 06:49:33 +0000 | [diff] [blame] | 1036 | |
Tim Peters | 026f8dc | 2004-08-19 16:38:58 +0000 | [diff] [blame] | 1037 | ... can be surprising; e.g., this test passes: |
Tim Peters | 26b3ebb | 2004-08-19 08:10:08 +0000 | [diff] [blame] | 1038 | |
Guido van Rossum | e0192e5 | 2007-02-09 23:39:59 +0000 | [diff] [blame] | 1039 | >>> if 1: #doctest: +ELLIPSIS |
| 1040 | ... for i in range(20): |
| 1041 | ... print(i, end=' ') |
| 1042 | ... print(20) |
Tim Peters | e594bee | 2004-08-22 01:47:51 +0000 | [diff] [blame] | 1043 | 0 1 2 ...1...2...0 |
Tim Peters | 26b3ebb | 2004-08-19 08:10:08 +0000 | [diff] [blame] | 1044 | |
Tim Peters | 026f8dc | 2004-08-19 16:38:58 +0000 | [diff] [blame] | 1045 | Examples from the docs: |
| 1046 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1047 | >>> print(list(range(20))) # doctest:+ELLIPSIS |
Tim Peters | 026f8dc | 2004-08-19 16:38:58 +0000 | [diff] [blame] | 1048 | [0, 1, ..., 18, 19] |
| 1049 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1050 | >>> print(list(range(20))) # doctest: +ELLIPSIS |
Tim Peters | 026f8dc | 2004-08-19 16:38:58 +0000 | [diff] [blame] | 1051 | ... # doctest: +NORMALIZE_WHITESPACE |
| 1052 | [0, 1, ..., 18, 19] |
| 1053 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1054 | The SKIP flag causes an example to be skipped entirely. I.e., the |
| 1055 | example is not run. It can be useful in contexts where doctest |
| 1056 | examples serve as both documentation and test cases, and an example |
| 1057 | should be included for documentation purposes, but should not be |
| 1058 | checked (e.g., because its output is random, or depends on resources |
| 1059 | which would be unavailable.) The SKIP flag can also be used for |
| 1060 | 'commenting out' broken examples. |
| 1061 | |
| 1062 | >>> import unavailable_resource # doctest: +SKIP |
| 1063 | >>> unavailable_resource.do_something() # doctest: +SKIP |
| 1064 | >>> unavailable_resource.blow_up() # doctest: +SKIP |
| 1065 | Traceback (most recent call last): |
| 1066 | ... |
| 1067 | UncheckedBlowUpError: Nobody checks me. |
| 1068 | |
| 1069 | >>> import random |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 1070 | >>> print(random.random()) # doctest: +SKIP |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1071 | 0.721216923889 |
| 1072 | |
Edward Loper | 71f55af | 2004-08-26 01:41:51 +0000 | [diff] [blame] | 1073 | The REPORT_UDIFF flag causes failures that involve multi-line expected |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1074 | and actual outputs to be displayed using a unified diff: |
| 1075 | |
| 1076 | >>> def f(x): |
| 1077 | ... r''' |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1078 | ... >>> print('\n'.join('abcdefg')) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1079 | ... a |
| 1080 | ... B |
| 1081 | ... c |
| 1082 | ... d |
| 1083 | ... f |
| 1084 | ... g |
| 1085 | ... h |
| 1086 | ... ''' |
| 1087 | |
| 1088 | >>> # Without the flag: |
| 1089 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1090 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1091 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1092 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1093 | File ..., line 3, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1094 | Failed example: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1095 | print('\n'.join('abcdefg')) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1096 | Expected: |
| 1097 | a |
| 1098 | B |
| 1099 | c |
| 1100 | d |
| 1101 | f |
| 1102 | g |
| 1103 | h |
| 1104 | Got: |
| 1105 | a |
| 1106 | b |
| 1107 | c |
| 1108 | d |
| 1109 | e |
| 1110 | f |
| 1111 | g |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1112 | TestResults(failed=1, attempted=1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1113 | |
| 1114 | >>> # With the flag: |
| 1115 | >>> test = doctest.DocTestFinder().find(f)[0] |
Edward Loper | 71f55af | 2004-08-26 01:41:51 +0000 | [diff] [blame] | 1116 | >>> flags = doctest.REPORT_UDIFF |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1117 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1118 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1119 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1120 | File ..., line 3, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1121 | Failed example: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1122 | print('\n'.join('abcdefg')) |
Edward Loper | 5662929 | 2004-08-26 01:31:56 +0000 | [diff] [blame] | 1123 | Differences (unified diff with -expected +actual): |
Tim Peters | e7edcb8 | 2004-08-26 05:44:27 +0000 | [diff] [blame] | 1124 | @@ -1,7 +1,7 @@ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1125 | a |
| 1126 | -B |
| 1127 | +b |
| 1128 | c |
| 1129 | d |
| 1130 | +e |
| 1131 | f |
| 1132 | g |
| 1133 | -h |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1134 | TestResults(failed=1, attempted=1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1135 | |
Edward Loper | 71f55af | 2004-08-26 01:41:51 +0000 | [diff] [blame] | 1136 | The REPORT_CDIFF flag causes failures that involve multi-line expected |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1137 | and actual outputs to be displayed using a context diff: |
| 1138 | |
Edward Loper | 71f55af | 2004-08-26 01:41:51 +0000 | [diff] [blame] | 1139 | >>> # Reuse f() from the REPORT_UDIFF example, above. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1140 | >>> test = doctest.DocTestFinder().find(f)[0] |
Edward Loper | 71f55af | 2004-08-26 01:41:51 +0000 | [diff] [blame] | 1141 | >>> flags = doctest.REPORT_CDIFF |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1142 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1143 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1144 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1145 | File ..., line 3, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1146 | Failed example: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1147 | print('\n'.join('abcdefg')) |
Edward Loper | 5662929 | 2004-08-26 01:31:56 +0000 | [diff] [blame] | 1148 | Differences (context diff with expected followed by actual): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1149 | *************** |
Tim Peters | e7edcb8 | 2004-08-26 05:44:27 +0000 | [diff] [blame] | 1150 | *** 1,7 **** |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1151 | a |
| 1152 | ! B |
| 1153 | c |
| 1154 | d |
| 1155 | f |
| 1156 | g |
| 1157 | - h |
Tim Peters | e7edcb8 | 2004-08-26 05:44:27 +0000 | [diff] [blame] | 1158 | --- 1,7 ---- |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1159 | a |
| 1160 | ! b |
| 1161 | c |
| 1162 | d |
| 1163 | + e |
| 1164 | f |
| 1165 | g |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1166 | TestResults(failed=1, attempted=1) |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 1167 | |
| 1168 | |
Edward Loper | 71f55af | 2004-08-26 01:41:51 +0000 | [diff] [blame] | 1169 | The REPORT_NDIFF flag causes failures to use the difflib.Differ algorithm |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 1170 | used by the popular ndiff.py utility. This does intraline difference |
| 1171 | marking, as well as interline differences. |
| 1172 | |
| 1173 | >>> def f(x): |
| 1174 | ... r''' |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1175 | ... >>> print("a b c d e f g h i j k l m") |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 1176 | ... a b c d e f g h i j k 1 m |
| 1177 | ... ''' |
| 1178 | >>> test = doctest.DocTestFinder().find(f)[0] |
Edward Loper | 71f55af | 2004-08-26 01:41:51 +0000 | [diff] [blame] | 1179 | >>> flags = doctest.REPORT_NDIFF |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 1180 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1181 | ... # doctest: +ELLIPSIS |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 1182 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1183 | File ..., line 3, in f |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 1184 | Failed example: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1185 | print("a b c d e f g h i j k l m") |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 1186 | Differences (ndiff with -expected +actual): |
| 1187 | - a b c d e f g h i j k 1 m |
| 1188 | ? ^ |
| 1189 | + a b c d e f g h i j k l m |
| 1190 | ? + ++ ^ |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1191 | TestResults(failed=1, attempted=1) |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1192 | |
| 1193 | The REPORT_ONLY_FIRST_FAILURE supresses result output after the first |
| 1194 | failing example: |
| 1195 | |
| 1196 | >>> def f(x): |
| 1197 | ... r''' |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1198 | ... >>> print(1) # first success |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1199 | ... 1 |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1200 | ... >>> print(2) # first failure |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1201 | ... 200 |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1202 | ... >>> print(3) # second failure |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1203 | ... 300 |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1204 | ... >>> print(4) # second success |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1205 | ... 4 |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1206 | ... >>> print(5) # third failure |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1207 | ... 500 |
| 1208 | ... ''' |
| 1209 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1210 | >>> flags = doctest.REPORT_ONLY_FIRST_FAILURE |
| 1211 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1212 | ... # doctest: +ELLIPSIS |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1213 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1214 | File ..., line 5, in f |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1215 | Failed example: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1216 | print(2) # first failure |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1217 | Expected: |
| 1218 | 200 |
| 1219 | Got: |
| 1220 | 2 |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1221 | TestResults(failed=3, attempted=5) |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1222 | |
| 1223 | However, output from `report_start` is not supressed: |
| 1224 | |
| 1225 | >>> doctest.DocTestRunner(verbose=True, optionflags=flags).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1226 | ... # doctest: +ELLIPSIS |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1227 | Trying: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1228 | print(1) # first success |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1229 | Expecting: |
| 1230 | 1 |
| 1231 | ok |
| 1232 | Trying: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1233 | print(2) # first failure |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1234 | Expecting: |
| 1235 | 200 |
| 1236 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1237 | File ..., line 5, in f |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1238 | Failed example: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1239 | print(2) # first failure |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1240 | Expected: |
| 1241 | 200 |
| 1242 | Got: |
| 1243 | 2 |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1244 | TestResults(failed=3, attempted=5) |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1245 | |
| 1246 | For the purposes of REPORT_ONLY_FIRST_FAILURE, unexpected exceptions |
| 1247 | count as failures: |
| 1248 | |
| 1249 | >>> def f(x): |
| 1250 | ... r''' |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1251 | ... >>> print(1) # first success |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1252 | ... 1 |
| 1253 | ... >>> raise ValueError(2) # first failure |
| 1254 | ... 200 |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1255 | ... >>> print(3) # second failure |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1256 | ... 300 |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1257 | ... >>> print(4) # second success |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1258 | ... 4 |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1259 | ... >>> print(5) # third failure |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1260 | ... 500 |
| 1261 | ... ''' |
| 1262 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1263 | >>> flags = doctest.REPORT_ONLY_FIRST_FAILURE |
| 1264 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
| 1265 | ... # doctest: +ELLIPSIS |
| 1266 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1267 | File ..., line 5, in f |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1268 | Failed example: |
| 1269 | raise ValueError(2) # first failure |
| 1270 | Exception raised: |
| 1271 | ... |
| 1272 | ValueError: 2 |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1273 | TestResults(failed=3, attempted=5) |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1274 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1275 | New option flags can also be registered, via register_optionflag(). Here |
| 1276 | we reach into doctest's internals a bit. |
| 1277 | |
| 1278 | >>> unlikely = "UNLIKELY_OPTION_NAME" |
| 1279 | >>> unlikely in doctest.OPTIONFLAGS_BY_NAME |
| 1280 | False |
| 1281 | >>> new_flag_value = doctest.register_optionflag(unlikely) |
| 1282 | >>> unlikely in doctest.OPTIONFLAGS_BY_NAME |
| 1283 | True |
| 1284 | |
| 1285 | Before 2.4.4/2.5, registering a name more than once erroneously created |
| 1286 | more than one flag value. Here we verify that's fixed: |
| 1287 | |
| 1288 | >>> redundant_flag_value = doctest.register_optionflag(unlikely) |
| 1289 | >>> redundant_flag_value == new_flag_value |
| 1290 | True |
| 1291 | |
| 1292 | Clean up. |
| 1293 | >>> del doctest.OPTIONFLAGS_BY_NAME[unlikely] |
| 1294 | |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 1295 | """ |
| 1296 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1297 | def option_directives(): r""" |
| 1298 | Tests of `DocTestRunner`'s option directive mechanism. |
| 1299 | |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1300 | Option directives can be used to turn option flags on or off for a |
| 1301 | single example. To turn an option on for an example, follow that |
| 1302 | example with a comment of the form ``# doctest: +OPTION``: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1303 | |
| 1304 | >>> def f(x): r''' |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1305 | ... >>> print(list(range(10))) # should fail: no ellipsis |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1306 | ... [0, 1, ..., 9] |
| 1307 | ... |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1308 | ... >>> print(list(range(10))) # doctest: +ELLIPSIS |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1309 | ... [0, 1, ..., 9] |
| 1310 | ... ''' |
| 1311 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1312 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1313 | ... # doctest: +ELLIPSIS |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1314 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1315 | File ..., line 2, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1316 | Failed example: |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1317 | print(list(range(10))) # should fail: no ellipsis |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1318 | Expected: |
| 1319 | [0, 1, ..., 9] |
| 1320 | Got: |
| 1321 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1322 | TestResults(failed=1, attempted=2) |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1323 | |
| 1324 | To turn an option off for an example, follow that example with a |
| 1325 | comment of the form ``# doctest: -OPTION``: |
| 1326 | |
| 1327 | >>> def f(x): r''' |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1328 | ... >>> print(list(range(10))) |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1329 | ... [0, 1, ..., 9] |
| 1330 | ... |
| 1331 | ... >>> # should fail: no ellipsis |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1332 | ... >>> print(list(range(10))) # doctest: -ELLIPSIS |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1333 | ... [0, 1, ..., 9] |
| 1334 | ... ''' |
| 1335 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1336 | >>> doctest.DocTestRunner(verbose=False, |
| 1337 | ... optionflags=doctest.ELLIPSIS).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1338 | ... # doctest: +ELLIPSIS |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1339 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1340 | File ..., line 6, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1341 | Failed example: |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1342 | print(list(range(10))) # doctest: -ELLIPSIS |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1343 | Expected: |
| 1344 | [0, 1, ..., 9] |
| 1345 | Got: |
| 1346 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1347 | TestResults(failed=1, attempted=2) |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1348 | |
| 1349 | Option directives affect only the example that they appear with; they |
| 1350 | do not change the options for surrounding examples: |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 1351 | |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1352 | >>> def f(x): r''' |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1353 | ... >>> print(list(range(10))) # Should fail: no ellipsis |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1354 | ... [0, 1, ..., 9] |
| 1355 | ... |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1356 | ... >>> print(list(range(10))) # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1357 | ... [0, 1, ..., 9] |
| 1358 | ... |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1359 | ... >>> print(list(range(10))) # Should fail: no ellipsis |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1360 | ... [0, 1, ..., 9] |
| 1361 | ... ''' |
| 1362 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1363 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1364 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1365 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1366 | File ..., line 2, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1367 | Failed example: |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1368 | print(list(range(10))) # Should fail: no ellipsis |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1369 | Expected: |
| 1370 | [0, 1, ..., 9] |
| 1371 | Got: |
| 1372 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1373 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1374 | File ..., line 8, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1375 | Failed example: |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1376 | print(list(range(10))) # Should fail: no ellipsis |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1377 | Expected: |
| 1378 | [0, 1, ..., 9] |
| 1379 | Got: |
| 1380 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1381 | TestResults(failed=2, attempted=3) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1382 | |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1383 | Multiple options may be modified by a single option directive. They |
| 1384 | may be separated by whitespace, commas, or both: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1385 | |
| 1386 | >>> def f(x): r''' |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1387 | ... >>> print(list(range(10))) # Should fail |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1388 | ... [0, 1, ..., 9] |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1389 | ... >>> print(list(range(10))) # Should succeed |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1390 | ... ... # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1391 | ... [0, 1, ..., 9] |
| 1392 | ... ''' |
| 1393 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1394 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1395 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1396 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1397 | File ..., line 2, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1398 | Failed example: |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1399 | print(list(range(10))) # Should fail |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1400 | Expected: |
| 1401 | [0, 1, ..., 9] |
| 1402 | Got: |
| 1403 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1404 | TestResults(failed=1, attempted=2) |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1405 | |
| 1406 | >>> def f(x): r''' |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1407 | ... >>> print(list(range(10))) # Should fail |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1408 | ... [0, 1, ..., 9] |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1409 | ... >>> print(list(range(10))) # Should succeed |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1410 | ... ... # doctest: +ELLIPSIS,+NORMALIZE_WHITESPACE |
| 1411 | ... [0, 1, ..., 9] |
| 1412 | ... ''' |
| 1413 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1414 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1415 | ... # doctest: +ELLIPSIS |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1416 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1417 | File ..., line 2, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1418 | Failed example: |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1419 | print(list(range(10))) # Should fail |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1420 | Expected: |
| 1421 | [0, 1, ..., 9] |
| 1422 | Got: |
| 1423 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1424 | TestResults(failed=1, attempted=2) |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1425 | |
| 1426 | >>> def f(x): r''' |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1427 | ... >>> print(list(range(10))) # Should fail |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1428 | ... [0, 1, ..., 9] |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1429 | ... >>> print(list(range(10))) # Should succeed |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1430 | ... ... # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 1431 | ... [0, 1, ..., 9] |
| 1432 | ... ''' |
| 1433 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1434 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1435 | ... # doctest: +ELLIPSIS |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1436 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1437 | File ..., line 2, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1438 | Failed example: |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1439 | print(list(range(10))) # Should fail |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1440 | Expected: |
| 1441 | [0, 1, ..., 9] |
| 1442 | Got: |
| 1443 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1444 | TestResults(failed=1, attempted=2) |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1445 | |
| 1446 | The option directive may be put on the line following the source, as |
| 1447 | long as a continuation prompt is used: |
| 1448 | |
| 1449 | >>> def f(x): r''' |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1450 | ... >>> print(list(range(10))) |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1451 | ... ... # doctest: +ELLIPSIS |
| 1452 | ... [0, 1, ..., 9] |
| 1453 | ... ''' |
| 1454 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1455 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1456 | TestResults(failed=0, attempted=1) |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 1457 | |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1458 | For examples with multi-line source, the option directive may appear |
| 1459 | at the end of any line: |
| 1460 | |
| 1461 | >>> def f(x): r''' |
| 1462 | ... >>> for x in range(10): # doctest: +ELLIPSIS |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1463 | ... ... print(' ', x, end='', sep='') |
| 1464 | ... 0 1 2 ... 9 |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1465 | ... |
| 1466 | ... >>> for x in range(10): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1467 | ... ... print(' ', x, end='', sep='') # doctest: +ELLIPSIS |
| 1468 | ... 0 1 2 ... 9 |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1469 | ... ''' |
| 1470 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1471 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1472 | TestResults(failed=0, attempted=2) |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1473 | |
| 1474 | If more than one line of an example with multi-line source has an |
| 1475 | option directive, then they are combined: |
| 1476 | |
| 1477 | >>> def f(x): r''' |
| 1478 | ... Should fail (option directive not on the last line): |
| 1479 | ... >>> for x in range(10): # doctest: +ELLIPSIS |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1480 | ... ... print(x, end=' ') # doctest: +NORMALIZE_WHITESPACE |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1481 | ... 0 1 2...9 |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1482 | ... ''' |
| 1483 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1484 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1485 | TestResults(failed=0, attempted=1) |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1486 | |
| 1487 | It is an error to have a comment of the form ``# doctest:`` that is |
| 1488 | *not* followed by words of the form ``+OPTION`` or ``-OPTION``, where |
| 1489 | ``OPTION`` is an option that has been registered with |
| 1490 | `register_option`: |
| 1491 | |
| 1492 | >>> # Error: Option not registered |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1493 | >>> s = '>>> print(12) #doctest: +BADOPTION' |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1494 | >>> test = doctest.DocTestParser().get_doctest(s, {}, 's', 's.py', 0) |
| 1495 | Traceback (most recent call last): |
| 1496 | ValueError: line 1 of the doctest for s has an invalid option: '+BADOPTION' |
| 1497 | |
| 1498 | >>> # Error: No + or - prefix |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1499 | >>> s = '>>> print(12) #doctest: ELLIPSIS' |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1500 | >>> test = doctest.DocTestParser().get_doctest(s, {}, 's', 's.py', 0) |
| 1501 | Traceback (most recent call last): |
| 1502 | ValueError: line 1 of the doctest for s has an invalid option: 'ELLIPSIS' |
| 1503 | |
| 1504 | It is an error to use an option directive on a line that contains no |
| 1505 | source: |
| 1506 | |
| 1507 | >>> s = '>>> # doctest: +ELLIPSIS' |
| 1508 | >>> test = doctest.DocTestParser().get_doctest(s, {}, 's', 's.py', 0) |
| 1509 | Traceback (most recent call last): |
| 1510 | ValueError: line 0 of the doctest for s has an option directive on a line with no example: '# doctest: +ELLIPSIS' |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1511 | """ |
| 1512 | |
| 1513 | def test_testsource(): r""" |
| 1514 | Unit tests for `testsource()`. |
| 1515 | |
| 1516 | The testsource() function takes a module and a name, finds the (first) |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1517 | test with that name in that module, and converts it to a script. The |
| 1518 | example code is converted to regular Python code. The surrounding |
| 1519 | words and expected output are converted to comments: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1520 | |
| 1521 | >>> import test.test_doctest |
| 1522 | >>> name = 'test.test_doctest.sample_func' |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 1523 | >>> print(doctest.testsource(test.test_doctest, name)) |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 1524 | # Blah blah |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1525 | # |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1526 | print(sample_func(22)) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1527 | # Expected: |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 1528 | ## 44 |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1529 | # |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 1530 | # Yee ha! |
Georg Brandl | ecf93c7 | 2005-06-26 23:09:51 +0000 | [diff] [blame] | 1531 | <BLANKLINE> |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1532 | |
| 1533 | >>> name = 'test.test_doctest.SampleNewStyleClass' |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 1534 | >>> print(doctest.testsource(test.test_doctest, name)) |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1535 | print('1\n2\n3') |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1536 | # Expected: |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 1537 | ## 1 |
| 1538 | ## 2 |
| 1539 | ## 3 |
Georg Brandl | ecf93c7 | 2005-06-26 23:09:51 +0000 | [diff] [blame] | 1540 | <BLANKLINE> |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1541 | |
| 1542 | >>> name = 'test.test_doctest.SampleClass.a_classmethod' |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 1543 | >>> print(doctest.testsource(test.test_doctest, name)) |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1544 | print(SampleClass.a_classmethod(10)) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1545 | # Expected: |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 1546 | ## 12 |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1547 | print(SampleClass(0).a_classmethod(10)) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1548 | # Expected: |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 1549 | ## 12 |
Georg Brandl | ecf93c7 | 2005-06-26 23:09:51 +0000 | [diff] [blame] | 1550 | <BLANKLINE> |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1551 | """ |
| 1552 | |
| 1553 | def test_debug(): r""" |
| 1554 | |
| 1555 | Create a docstring that we want to debug: |
| 1556 | |
| 1557 | >>> s = ''' |
| 1558 | ... >>> x = 12 |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1559 | ... >>> print(x) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1560 | ... 12 |
| 1561 | ... ''' |
| 1562 | |
| 1563 | Create some fake stdin input, to feed to the debugger: |
| 1564 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1565 | >>> real_stdin = sys.stdin |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1566 | >>> sys.stdin = _FakeInput(['next', 'print(x)', 'continue']) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1567 | |
| 1568 | Run the debugger on the docstring, and then restore sys.stdin. |
| 1569 | |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1570 | >>> try: doctest.debug_src(s) |
| 1571 | ... finally: sys.stdin = real_stdin |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1572 | > <string>(1)<module>() |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1573 | (Pdb) next |
| 1574 | 12 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1575 | --Return-- |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1576 | > <string>(1)<module>()->None |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1577 | (Pdb) print(x) |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1578 | 12 |
| 1579 | (Pdb) continue |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1580 | |
| 1581 | """ |
| 1582 | |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1583 | def test_pdb_set_trace(): |
Tim Peters | 50c6bdb | 2004-11-08 22:07:37 +0000 | [diff] [blame] | 1584 | """Using pdb.set_trace from a doctest. |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1585 | |
Tim Peters | 413ced6 | 2004-08-09 15:43:47 +0000 | [diff] [blame] | 1586 | You can use pdb.set_trace from a doctest. To do so, you must |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1587 | retrieve the set_trace function from the pdb module at the time |
Tim Peters | 413ced6 | 2004-08-09 15:43:47 +0000 | [diff] [blame] | 1588 | you use it. The doctest module changes sys.stdout so that it can |
| 1589 | capture program output. It also temporarily replaces pdb.set_trace |
| 1590 | with a version that restores stdout. This is necessary for you to |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1591 | see debugger output. |
| 1592 | |
| 1593 | >>> doc = ''' |
| 1594 | ... >>> x = 42 |
| 1595 | ... >>> import pdb; pdb.set_trace() |
| 1596 | ... ''' |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 1597 | >>> parser = doctest.DocTestParser() |
| 1598 | >>> test = parser.get_doctest(doc, {}, "foo", "foo.py", 0) |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1599 | >>> runner = doctest.DocTestRunner(verbose=False) |
| 1600 | |
| 1601 | To demonstrate this, we'll create a fake standard input that |
| 1602 | captures our debugger input: |
| 1603 | |
| 1604 | >>> import tempfile |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1605 | >>> real_stdin = sys.stdin |
| 1606 | >>> sys.stdin = _FakeInput([ |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1607 | ... 'print(x)', # print data defined by the example |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1608 | ... 'continue', # stop debugging |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1609 | ... '']) |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1610 | |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1611 | >>> try: runner.run(test) |
| 1612 | ... finally: sys.stdin = real_stdin |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1613 | --Return-- |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1614 | > <doctest foo[1]>(1)<module>()->None |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1615 | -> import pdb; pdb.set_trace() |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1616 | (Pdb) print(x) |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1617 | 42 |
| 1618 | (Pdb) continue |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1619 | TestResults(failed=0, attempted=2) |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1620 | |
| 1621 | You can also put pdb.set_trace in a function called from a test: |
| 1622 | |
| 1623 | >>> def calls_set_trace(): |
| 1624 | ... y=2 |
| 1625 | ... import pdb; pdb.set_trace() |
| 1626 | |
| 1627 | >>> doc = ''' |
| 1628 | ... >>> x=1 |
| 1629 | ... >>> calls_set_trace() |
| 1630 | ... ''' |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 1631 | >>> test = parser.get_doctest(doc, globals(), "foo", "foo.py", 0) |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1632 | >>> real_stdin = sys.stdin |
| 1633 | >>> sys.stdin = _FakeInput([ |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1634 | ... 'print(y)', # print data defined in the function |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1635 | ... 'up', # out of function |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1636 | ... 'print(x)', # print data defined by the example |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1637 | ... 'continue', # stop debugging |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1638 | ... '']) |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1639 | |
Tim Peters | 50c6bdb | 2004-11-08 22:07:37 +0000 | [diff] [blame] | 1640 | >>> try: |
| 1641 | ... runner.run(test) |
| 1642 | ... finally: |
| 1643 | ... sys.stdin = real_stdin |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1644 | --Return-- |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1645 | > <doctest test.test_doctest.test_pdb_set_trace[8]>(3)calls_set_trace()->None |
| 1646 | -> import pdb; pdb.set_trace() |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1647 | (Pdb) print(y) |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1648 | 2 |
| 1649 | (Pdb) up |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1650 | > <doctest foo[1]>(1)<module>() |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1651 | -> calls_set_trace() |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1652 | (Pdb) print(x) |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1653 | 1 |
| 1654 | (Pdb) continue |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1655 | TestResults(failed=0, attempted=2) |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1656 | |
| 1657 | During interactive debugging, source code is shown, even for |
| 1658 | doctest examples: |
| 1659 | |
| 1660 | >>> doc = ''' |
| 1661 | ... >>> def f(x): |
| 1662 | ... ... g(x*2) |
| 1663 | ... >>> def g(x): |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1664 | ... ... print(x+3) |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1665 | ... ... import pdb; pdb.set_trace() |
| 1666 | ... >>> f(3) |
| 1667 | ... ''' |
| 1668 | >>> test = parser.get_doctest(doc, globals(), "foo", "foo.py", 0) |
| 1669 | >>> real_stdin = sys.stdin |
| 1670 | >>> sys.stdin = _FakeInput([ |
| 1671 | ... 'list', # list source from example 2 |
| 1672 | ... 'next', # return from g() |
| 1673 | ... 'list', # list source from example 1 |
| 1674 | ... 'next', # return from f() |
| 1675 | ... 'list', # list source from example 3 |
| 1676 | ... 'continue', # stop debugging |
| 1677 | ... '']) |
| 1678 | >>> try: runner.run(test) |
| 1679 | ... finally: sys.stdin = real_stdin |
| 1680 | ... # doctest: +NORMALIZE_WHITESPACE |
| 1681 | --Return-- |
| 1682 | > <doctest foo[1]>(3)g()->None |
| 1683 | -> import pdb; pdb.set_trace() |
| 1684 | (Pdb) list |
| 1685 | 1 def g(x): |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1686 | 2 print(x+3) |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1687 | 3 -> import pdb; pdb.set_trace() |
| 1688 | [EOF] |
| 1689 | (Pdb) next |
| 1690 | --Return-- |
| 1691 | > <doctest foo[0]>(2)f()->None |
| 1692 | -> g(x*2) |
| 1693 | (Pdb) list |
| 1694 | 1 def f(x): |
| 1695 | 2 -> g(x*2) |
| 1696 | [EOF] |
| 1697 | (Pdb) next |
| 1698 | --Return-- |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1699 | > <doctest foo[2]>(1)<module>()->None |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1700 | -> f(3) |
| 1701 | (Pdb) list |
| 1702 | 1 -> f(3) |
| 1703 | [EOF] |
| 1704 | (Pdb) continue |
| 1705 | ********************************************************************** |
| 1706 | File "foo.py", line 7, in foo |
| 1707 | Failed example: |
| 1708 | f(3) |
| 1709 | Expected nothing |
| 1710 | Got: |
| 1711 | 9 |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1712 | TestResults(failed=1, attempted=3) |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1713 | """ |
| 1714 | |
Tim Peters | 50c6bdb | 2004-11-08 22:07:37 +0000 | [diff] [blame] | 1715 | def test_pdb_set_trace_nested(): |
| 1716 | """This illustrates more-demanding use of set_trace with nested functions. |
| 1717 | |
| 1718 | >>> class C(object): |
| 1719 | ... def calls_set_trace(self): |
| 1720 | ... y = 1 |
| 1721 | ... import pdb; pdb.set_trace() |
| 1722 | ... self.f1() |
| 1723 | ... y = 2 |
| 1724 | ... def f1(self): |
| 1725 | ... x = 1 |
| 1726 | ... self.f2() |
| 1727 | ... x = 2 |
| 1728 | ... def f2(self): |
| 1729 | ... z = 1 |
| 1730 | ... z = 2 |
| 1731 | |
| 1732 | >>> calls_set_trace = C().calls_set_trace |
| 1733 | |
| 1734 | >>> doc = ''' |
| 1735 | ... >>> a = 1 |
| 1736 | ... >>> calls_set_trace() |
| 1737 | ... ''' |
| 1738 | >>> parser = doctest.DocTestParser() |
| 1739 | >>> runner = doctest.DocTestRunner(verbose=False) |
| 1740 | >>> test = parser.get_doctest(doc, globals(), "foo", "foo.py", 0) |
| 1741 | >>> real_stdin = sys.stdin |
| 1742 | >>> sys.stdin = _FakeInput([ |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1743 | ... 'print(y)', # print data defined in the function |
| 1744 | ... 'step', 'step', 'step', 'step', 'step', 'step', 'print(z)', |
| 1745 | ... 'up', 'print(x)', |
| 1746 | ... 'up', 'print(y)', |
| 1747 | ... 'up', 'print(foo)', |
Tim Peters | 50c6bdb | 2004-11-08 22:07:37 +0000 | [diff] [blame] | 1748 | ... 'continue', # stop debugging |
| 1749 | ... '']) |
| 1750 | |
| 1751 | >>> try: |
| 1752 | ... runner.run(test) |
| 1753 | ... finally: |
| 1754 | ... sys.stdin = real_stdin |
Guido van Rossum | 4a625c3 | 2007-09-08 16:05:25 +0000 | [diff] [blame] | 1755 | ... # doctest: +REPORT_NDIFF |
Tim Peters | 50c6bdb | 2004-11-08 22:07:37 +0000 | [diff] [blame] | 1756 | > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(5)calls_set_trace() |
| 1757 | -> self.f1() |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1758 | (Pdb) print(y) |
Tim Peters | 50c6bdb | 2004-11-08 22:07:37 +0000 | [diff] [blame] | 1759 | 1 |
| 1760 | (Pdb) step |
| 1761 | --Call-- |
| 1762 | > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(7)f1() |
| 1763 | -> def f1(self): |
| 1764 | (Pdb) step |
| 1765 | > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(8)f1() |
| 1766 | -> x = 1 |
| 1767 | (Pdb) step |
| 1768 | > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(9)f1() |
| 1769 | -> self.f2() |
| 1770 | (Pdb) step |
| 1771 | --Call-- |
| 1772 | > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(11)f2() |
| 1773 | -> def f2(self): |
| 1774 | (Pdb) step |
| 1775 | > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(12)f2() |
| 1776 | -> z = 1 |
| 1777 | (Pdb) step |
| 1778 | > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(13)f2() |
| 1779 | -> z = 2 |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1780 | (Pdb) print(z) |
Tim Peters | 50c6bdb | 2004-11-08 22:07:37 +0000 | [diff] [blame] | 1781 | 1 |
| 1782 | (Pdb) up |
| 1783 | > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(9)f1() |
| 1784 | -> self.f2() |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1785 | (Pdb) print(x) |
Tim Peters | 50c6bdb | 2004-11-08 22:07:37 +0000 | [diff] [blame] | 1786 | 1 |
| 1787 | (Pdb) up |
| 1788 | > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(5)calls_set_trace() |
| 1789 | -> self.f1() |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1790 | (Pdb) print(y) |
Tim Peters | 50c6bdb | 2004-11-08 22:07:37 +0000 | [diff] [blame] | 1791 | 1 |
| 1792 | (Pdb) up |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1793 | > <doctest foo[1]>(1)<module>() |
Tim Peters | 50c6bdb | 2004-11-08 22:07:37 +0000 | [diff] [blame] | 1794 | -> calls_set_trace() |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1795 | (Pdb) print(foo) |
Guido van Rossum | fd4a7de | 2007-09-05 03:26:38 +0000 | [diff] [blame] | 1796 | *** NameError: NameError("name 'foo' is not defined",) |
Tim Peters | 50c6bdb | 2004-11-08 22:07:37 +0000 | [diff] [blame] | 1797 | (Pdb) continue |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1798 | TestResults(failed=0, attempted=2) |
Tim Peters | 50c6bdb | 2004-11-08 22:07:37 +0000 | [diff] [blame] | 1799 | """ |
| 1800 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1801 | def test_DocTestSuite(): |
Tim Peters | 1e277ee | 2004-08-07 05:37:52 +0000 | [diff] [blame] | 1802 | """DocTestSuite creates a unittest test suite from a doctest. |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1803 | |
| 1804 | We create a Suite by providing a module. A module can be provided |
| 1805 | by passing a module object: |
| 1806 | |
| 1807 | >>> import unittest |
| 1808 | >>> import test.sample_doctest |
| 1809 | >>> suite = doctest.DocTestSuite(test.sample_doctest) |
| 1810 | >>> suite.run(unittest.TestResult()) |
Tim Peters | 1e277ee | 2004-08-07 05:37:52 +0000 | [diff] [blame] | 1811 | <unittest.TestResult run=9 errors=0 failures=4> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1812 | |
| 1813 | We can also supply the module by name: |
| 1814 | |
| 1815 | >>> suite = doctest.DocTestSuite('test.sample_doctest') |
| 1816 | >>> suite.run(unittest.TestResult()) |
Tim Peters | 1e277ee | 2004-08-07 05:37:52 +0000 | [diff] [blame] | 1817 | <unittest.TestResult run=9 errors=0 failures=4> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1818 | |
| 1819 | We can use the current module: |
| 1820 | |
| 1821 | >>> suite = test.sample_doctest.test_suite() |
| 1822 | >>> suite.run(unittest.TestResult()) |
Tim Peters | 1e277ee | 2004-08-07 05:37:52 +0000 | [diff] [blame] | 1823 | <unittest.TestResult run=9 errors=0 failures=4> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1824 | |
| 1825 | We can supply global variables. If we pass globs, they will be |
| 1826 | used instead of the module globals. Here we'll pass an empty |
| 1827 | globals, triggering an extra error: |
| 1828 | |
| 1829 | >>> suite = doctest.DocTestSuite('test.sample_doctest', globs={}) |
| 1830 | >>> suite.run(unittest.TestResult()) |
Tim Peters | 1e277ee | 2004-08-07 05:37:52 +0000 | [diff] [blame] | 1831 | <unittest.TestResult run=9 errors=0 failures=5> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1832 | |
| 1833 | Alternatively, we can provide extra globals. Here we'll make an |
| 1834 | error go away by providing an extra global variable: |
| 1835 | |
| 1836 | >>> suite = doctest.DocTestSuite('test.sample_doctest', |
| 1837 | ... extraglobs={'y': 1}) |
| 1838 | >>> suite.run(unittest.TestResult()) |
Tim Peters | 1e277ee | 2004-08-07 05:37:52 +0000 | [diff] [blame] | 1839 | <unittest.TestResult run=9 errors=0 failures=3> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1840 | |
| 1841 | You can pass option flags. Here we'll cause an extra error |
| 1842 | by disabling the blank-line feature: |
| 1843 | |
| 1844 | >>> suite = doctest.DocTestSuite('test.sample_doctest', |
Tim Peters | 1e277ee | 2004-08-07 05:37:52 +0000 | [diff] [blame] | 1845 | ... optionflags=doctest.DONT_ACCEPT_BLANKLINE) |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1846 | >>> suite.run(unittest.TestResult()) |
Tim Peters | 1e277ee | 2004-08-07 05:37:52 +0000 | [diff] [blame] | 1847 | <unittest.TestResult run=9 errors=0 failures=5> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1848 | |
Tim Peters | 1e277ee | 2004-08-07 05:37:52 +0000 | [diff] [blame] | 1849 | You can supply setUp and tearDown functions: |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1850 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 1851 | >>> def setUp(t): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1852 | ... import test.test_doctest |
| 1853 | ... test.test_doctest.sillySetup = True |
| 1854 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 1855 | >>> def tearDown(t): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1856 | ... import test.test_doctest |
| 1857 | ... del test.test_doctest.sillySetup |
| 1858 | |
| 1859 | Here, we installed a silly variable that the test expects: |
| 1860 | |
| 1861 | >>> suite = doctest.DocTestSuite('test.sample_doctest', |
| 1862 | ... setUp=setUp, tearDown=tearDown) |
| 1863 | >>> suite.run(unittest.TestResult()) |
Tim Peters | 1e277ee | 2004-08-07 05:37:52 +0000 | [diff] [blame] | 1864 | <unittest.TestResult run=9 errors=0 failures=3> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1865 | |
| 1866 | But the tearDown restores sanity: |
| 1867 | |
| 1868 | >>> import test.test_doctest |
| 1869 | >>> test.test_doctest.sillySetup |
| 1870 | Traceback (most recent call last): |
| 1871 | ... |
| 1872 | AttributeError: 'module' object has no attribute 'sillySetup' |
| 1873 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 1874 | The setUp and tearDown funtions are passed test objects. Here |
| 1875 | we'll use the setUp function to supply the missing variable y: |
| 1876 | |
| 1877 | >>> def setUp(test): |
| 1878 | ... test.globs['y'] = 1 |
| 1879 | |
| 1880 | >>> suite = doctest.DocTestSuite('test.sample_doctest', setUp=setUp) |
| 1881 | >>> suite.run(unittest.TestResult()) |
| 1882 | <unittest.TestResult run=9 errors=0 failures=3> |
| 1883 | |
| 1884 | Here, we didn't need to use a tearDown function because we |
| 1885 | modified the test globals, which are a copy of the |
| 1886 | sample_doctest module dictionary. The test globals are |
| 1887 | automatically cleared for us after a test. |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1888 | """ |
| 1889 | |
| 1890 | def test_DocFileSuite(): |
| 1891 | """We can test tests found in text files using a DocFileSuite. |
| 1892 | |
| 1893 | We create a suite by providing the names of one or more text |
| 1894 | files that include examples: |
| 1895 | |
| 1896 | >>> import unittest |
| 1897 | >>> suite = doctest.DocFileSuite('test_doctest.txt', |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1898 | ... 'test_doctest2.txt', |
| 1899 | ... 'test_doctest4.txt') |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1900 | >>> suite.run(unittest.TestResult()) |
Martin v. Löwis | b1a9f27 | 2007-07-20 07:13:39 +0000 | [diff] [blame] | 1901 | <unittest.TestResult run=3 errors=0 failures=2> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1902 | |
| 1903 | The test files are looked for in the directory containing the |
| 1904 | calling module. A package keyword argument can be provided to |
| 1905 | specify a different relative location. |
| 1906 | |
| 1907 | >>> import unittest |
| 1908 | >>> suite = doctest.DocFileSuite('test_doctest.txt', |
| 1909 | ... 'test_doctest2.txt', |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1910 | ... 'test_doctest4.txt', |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1911 | ... package='test') |
| 1912 | >>> suite.run(unittest.TestResult()) |
Martin v. Löwis | b1a9f27 | 2007-07-20 07:13:39 +0000 | [diff] [blame] | 1913 | <unittest.TestResult run=3 errors=0 failures=2> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1914 | |
Guido van Rossum | cd4d452 | 2007-11-22 00:30:02 +0000 | [diff] [blame] | 1915 | Support for using a package's __loader__.get_data() is also |
| 1916 | provided. |
| 1917 | |
| 1918 | >>> import unittest, pkgutil, test |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 1919 | >>> added_loader = False |
Guido van Rossum | cd4d452 | 2007-11-22 00:30:02 +0000 | [diff] [blame] | 1920 | >>> if not hasattr(test, '__loader__'): |
| 1921 | ... test.__loader__ = pkgutil.get_loader(test) |
| 1922 | ... added_loader = True |
| 1923 | >>> try: |
| 1924 | ... suite = doctest.DocFileSuite('test_doctest.txt', |
| 1925 | ... 'test_doctest2.txt', |
| 1926 | ... 'test_doctest4.txt', |
| 1927 | ... package='test') |
| 1928 | ... suite.run(unittest.TestResult()) |
| 1929 | ... finally: |
| 1930 | ... if added_loader: |
| 1931 | ... del test.__loader__ |
| 1932 | <unittest.TestResult run=3 errors=0 failures=2> |
| 1933 | |
Edward Loper | 0273f5b | 2004-09-18 20:27:04 +0000 | [diff] [blame] | 1934 | '/' should be used as a path separator. It will be converted |
| 1935 | to a native separator at run time: |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1936 | |
| 1937 | >>> suite = doctest.DocFileSuite('../test/test_doctest.txt') |
| 1938 | >>> suite.run(unittest.TestResult()) |
| 1939 | <unittest.TestResult run=1 errors=0 failures=1> |
| 1940 | |
Edward Loper | 0273f5b | 2004-09-18 20:27:04 +0000 | [diff] [blame] | 1941 | If DocFileSuite is used from an interactive session, then files |
| 1942 | are resolved relative to the directory of sys.argv[0]: |
| 1943 | |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 1944 | >>> import types, os.path, test.test_doctest |
Edward Loper | 0273f5b | 2004-09-18 20:27:04 +0000 | [diff] [blame] | 1945 | >>> save_argv = sys.argv |
| 1946 | >>> sys.argv = [test.test_doctest.__file__] |
| 1947 | >>> suite = doctest.DocFileSuite('test_doctest.txt', |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 1948 | ... package=types.ModuleType('__main__')) |
Edward Loper | 0273f5b | 2004-09-18 20:27:04 +0000 | [diff] [blame] | 1949 | >>> sys.argv = save_argv |
| 1950 | |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 1951 | By setting `module_relative=False`, os-specific paths may be |
| 1952 | used (including absolute paths and paths relative to the |
| 1953 | working directory): |
Edward Loper | 0273f5b | 2004-09-18 20:27:04 +0000 | [diff] [blame] | 1954 | |
| 1955 | >>> # Get the absolute path of the test package. |
| 1956 | >>> test_doctest_path = os.path.abspath(test.test_doctest.__file__) |
| 1957 | >>> test_pkg_path = os.path.split(test_doctest_path)[0] |
| 1958 | |
| 1959 | >>> # Use it to find the absolute path of test_doctest.txt. |
| 1960 | >>> test_file = os.path.join(test_pkg_path, 'test_doctest.txt') |
| 1961 | |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 1962 | >>> suite = doctest.DocFileSuite(test_file, module_relative=False) |
Edward Loper | 0273f5b | 2004-09-18 20:27:04 +0000 | [diff] [blame] | 1963 | >>> suite.run(unittest.TestResult()) |
| 1964 | <unittest.TestResult run=1 errors=0 failures=1> |
| 1965 | |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 1966 | It is an error to specify `package` when `module_relative=False`: |
| 1967 | |
| 1968 | >>> suite = doctest.DocFileSuite(test_file, module_relative=False, |
| 1969 | ... package='test') |
| 1970 | Traceback (most recent call last): |
| 1971 | ValueError: Package may only be specified for module-relative paths. |
| 1972 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1973 | You can specify initial global variables: |
| 1974 | |
| 1975 | >>> suite = doctest.DocFileSuite('test_doctest.txt', |
| 1976 | ... 'test_doctest2.txt', |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1977 | ... 'test_doctest4.txt', |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1978 | ... globs={'favorite_color': 'blue'}) |
| 1979 | >>> suite.run(unittest.TestResult()) |
Martin v. Löwis | b1a9f27 | 2007-07-20 07:13:39 +0000 | [diff] [blame] | 1980 | <unittest.TestResult run=3 errors=0 failures=1> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1981 | |
| 1982 | In this case, we supplied a missing favorite color. You can |
| 1983 | provide doctest options: |
| 1984 | |
| 1985 | >>> suite = doctest.DocFileSuite('test_doctest.txt', |
| 1986 | ... 'test_doctest2.txt', |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 1987 | ... 'test_doctest4.txt', |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1988 | ... optionflags=doctest.DONT_ACCEPT_BLANKLINE, |
| 1989 | ... globs={'favorite_color': 'blue'}) |
| 1990 | >>> suite.run(unittest.TestResult()) |
Martin v. Löwis | b1a9f27 | 2007-07-20 07:13:39 +0000 | [diff] [blame] | 1991 | <unittest.TestResult run=3 errors=0 failures=2> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1992 | |
| 1993 | And, you can provide setUp and tearDown functions: |
| 1994 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 1995 | >>> def setUp(t): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1996 | ... import test.test_doctest |
| 1997 | ... test.test_doctest.sillySetup = True |
| 1998 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 1999 | >>> def tearDown(t): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2000 | ... import test.test_doctest |
| 2001 | ... del test.test_doctest.sillySetup |
| 2002 | |
| 2003 | Here, we installed a silly variable that the test expects: |
| 2004 | |
| 2005 | >>> suite = doctest.DocFileSuite('test_doctest.txt', |
| 2006 | ... 'test_doctest2.txt', |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2007 | ... 'test_doctest4.txt', |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2008 | ... setUp=setUp, tearDown=tearDown) |
| 2009 | >>> suite.run(unittest.TestResult()) |
Martin v. Löwis | b1a9f27 | 2007-07-20 07:13:39 +0000 | [diff] [blame] | 2010 | <unittest.TestResult run=3 errors=0 failures=1> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2011 | |
| 2012 | But the tearDown restores sanity: |
| 2013 | |
| 2014 | >>> import test.test_doctest |
| 2015 | >>> test.test_doctest.sillySetup |
| 2016 | Traceback (most recent call last): |
| 2017 | ... |
| 2018 | AttributeError: 'module' object has no attribute 'sillySetup' |
| 2019 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2020 | The setUp and tearDown funtions are passed test objects. |
| 2021 | Here, we'll use a setUp function to set the favorite color in |
| 2022 | test_doctest.txt: |
| 2023 | |
| 2024 | >>> def setUp(test): |
| 2025 | ... test.globs['favorite_color'] = 'blue' |
| 2026 | |
| 2027 | >>> suite = doctest.DocFileSuite('test_doctest.txt', setUp=setUp) |
| 2028 | >>> suite.run(unittest.TestResult()) |
| 2029 | <unittest.TestResult run=1 errors=0 failures=0> |
| 2030 | |
| 2031 | Here, we didn't need to use a tearDown function because we |
| 2032 | modified the test globals. The test globals are |
| 2033 | automatically cleared for us after a test. |
Tim Peters | df7a208 | 2004-08-29 00:38:17 +0000 | [diff] [blame] | 2034 | |
Fred Drake | 7c404a4 | 2004-12-21 23:46:34 +0000 | [diff] [blame] | 2035 | Tests in a file run using `DocFileSuite` can also access the |
| 2036 | `__file__` global, which is set to the name of the file |
| 2037 | containing the tests: |
| 2038 | |
| 2039 | >>> suite = doctest.DocFileSuite('test_doctest3.txt') |
| 2040 | >>> suite.run(unittest.TestResult()) |
| 2041 | <unittest.TestResult run=1 errors=0 failures=0> |
| 2042 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2043 | If the tests contain non-ASCII characters, we have to specify which |
| 2044 | encoding the file is encoded with. We do so by using the `encoding` |
| 2045 | parameter: |
| 2046 | |
| 2047 | >>> suite = doctest.DocFileSuite('test_doctest.txt', |
| 2048 | ... 'test_doctest2.txt', |
| 2049 | ... 'test_doctest4.txt', |
| 2050 | ... encoding='utf-8') |
| 2051 | >>> suite.run(unittest.TestResult()) |
| 2052 | <unittest.TestResult run=3 errors=0 failures=2> |
| 2053 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2054 | """ |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2055 | |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 2056 | def test_trailing_space_in_test(): |
| 2057 | """ |
Tim Peters | a7def72 | 2004-08-23 22:13:22 +0000 | [diff] [blame] | 2058 | Trailing spaces in expected output are significant: |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 2059 | |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 2060 | >>> x, y = 'foo', '' |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 2061 | >>> print(x, y) |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 2062 | foo \n |
| 2063 | """ |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2064 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2065 | |
| 2066 | def test_unittest_reportflags(): |
| 2067 | """Default unittest reporting flags can be set to control reporting |
| 2068 | |
| 2069 | Here, we'll set the REPORT_ONLY_FIRST_FAILURE option so we see |
| 2070 | only the first failure of each test. First, we'll look at the |
| 2071 | output without the flag. The file test_doctest.txt file has two |
| 2072 | tests. They both fail if blank lines are disabled: |
| 2073 | |
| 2074 | >>> suite = doctest.DocFileSuite('test_doctest.txt', |
| 2075 | ... optionflags=doctest.DONT_ACCEPT_BLANKLINE) |
| 2076 | >>> import unittest |
| 2077 | >>> result = suite.run(unittest.TestResult()) |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 2078 | >>> print(result.failures[0][1]) # doctest: +ELLIPSIS |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2079 | Traceback ... |
| 2080 | Failed example: |
| 2081 | favorite_color |
| 2082 | ... |
| 2083 | Failed example: |
| 2084 | if 1: |
| 2085 | ... |
| 2086 | |
| 2087 | Note that we see both failures displayed. |
| 2088 | |
| 2089 | >>> old = doctest.set_unittest_reportflags( |
| 2090 | ... doctest.REPORT_ONLY_FIRST_FAILURE) |
| 2091 | |
| 2092 | Now, when we run the test: |
| 2093 | |
| 2094 | >>> result = suite.run(unittest.TestResult()) |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 2095 | >>> print(result.failures[0][1]) # doctest: +ELLIPSIS |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2096 | Traceback ... |
| 2097 | Failed example: |
| 2098 | favorite_color |
| 2099 | Exception raised: |
| 2100 | ... |
| 2101 | NameError: name 'favorite_color' is not defined |
| 2102 | <BLANKLINE> |
| 2103 | <BLANKLINE> |
Tim Peters | df7a208 | 2004-08-29 00:38:17 +0000 | [diff] [blame] | 2104 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2105 | We get only the first failure. |
| 2106 | |
| 2107 | If we give any reporting options when we set up the tests, |
| 2108 | however: |
| 2109 | |
| 2110 | >>> suite = doctest.DocFileSuite('test_doctest.txt', |
| 2111 | ... optionflags=doctest.DONT_ACCEPT_BLANKLINE | doctest.REPORT_NDIFF) |
| 2112 | |
| 2113 | Then the default eporting options are ignored: |
| 2114 | |
| 2115 | >>> result = suite.run(unittest.TestResult()) |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 2116 | >>> print(result.failures[0][1]) # doctest: +ELLIPSIS |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2117 | Traceback ... |
| 2118 | Failed example: |
| 2119 | favorite_color |
| 2120 | ... |
| 2121 | Failed example: |
| 2122 | if 1: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 2123 | print('a') |
| 2124 | print() |
| 2125 | print('b') |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2126 | Differences (ndiff with -expected +actual): |
| 2127 | a |
| 2128 | - <BLANKLINE> |
| 2129 | + |
| 2130 | b |
| 2131 | <BLANKLINE> |
| 2132 | <BLANKLINE> |
| 2133 | |
| 2134 | |
| 2135 | Test runners can restore the formatting flags after they run: |
| 2136 | |
| 2137 | >>> ignored = doctest.set_unittest_reportflags(old) |
| 2138 | |
| 2139 | """ |
| 2140 | |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2141 | def test_testfile(): r""" |
| 2142 | Tests for the `testfile()` function. This function runs all the |
| 2143 | doctest examples in a given file. In its simple invokation, it is |
| 2144 | called with the name of a file, which is taken to be relative to the |
| 2145 | calling module. The return value is (#failures, #tests). |
| 2146 | |
| 2147 | >>> doctest.testfile('test_doctest.txt') # doctest: +ELLIPSIS |
| 2148 | ********************************************************************** |
| 2149 | File "...", line 6, in test_doctest.txt |
| 2150 | Failed example: |
| 2151 | favorite_color |
| 2152 | Exception raised: |
| 2153 | ... |
| 2154 | NameError: name 'favorite_color' is not defined |
| 2155 | ********************************************************************** |
| 2156 | 1 items had failures: |
| 2157 | 1 of 2 in test_doctest.txt |
| 2158 | ***Test Failed*** 1 failures. |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 2159 | TestResults(failed=1, attempted=2) |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2160 | >>> doctest.master = None # Reset master. |
| 2161 | |
| 2162 | (Note: we'll be clearing doctest.master after each call to |
| 2163 | `doctest.testfile`, to supress warnings about multiple tests with the |
| 2164 | same name.) |
| 2165 | |
| 2166 | Globals may be specified with the `globs` and `extraglobs` parameters: |
| 2167 | |
| 2168 | >>> globs = {'favorite_color': 'blue'} |
| 2169 | >>> doctest.testfile('test_doctest.txt', globs=globs) |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 2170 | TestResults(failed=0, attempted=2) |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2171 | >>> doctest.master = None # Reset master. |
| 2172 | |
| 2173 | >>> extraglobs = {'favorite_color': 'red'} |
| 2174 | >>> doctest.testfile('test_doctest.txt', globs=globs, |
| 2175 | ... extraglobs=extraglobs) # doctest: +ELLIPSIS |
| 2176 | ********************************************************************** |
| 2177 | File "...", line 6, in test_doctest.txt |
| 2178 | Failed example: |
| 2179 | favorite_color |
| 2180 | Expected: |
| 2181 | 'blue' |
| 2182 | Got: |
| 2183 | 'red' |
| 2184 | ********************************************************************** |
| 2185 | 1 items had failures: |
| 2186 | 1 of 2 in test_doctest.txt |
| 2187 | ***Test Failed*** 1 failures. |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 2188 | TestResults(failed=1, attempted=2) |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2189 | >>> doctest.master = None # Reset master. |
| 2190 | |
| 2191 | The file may be made relative to a given module or package, using the |
| 2192 | optional `module_relative` parameter: |
| 2193 | |
| 2194 | >>> doctest.testfile('test_doctest.txt', globs=globs, |
| 2195 | ... module_relative='test') |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 2196 | TestResults(failed=0, attempted=2) |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2197 | >>> doctest.master = None # Reset master. |
| 2198 | |
| 2199 | Verbosity can be increased with the optional `verbose` paremter: |
| 2200 | |
| 2201 | >>> doctest.testfile('test_doctest.txt', globs=globs, verbose=True) |
| 2202 | Trying: |
| 2203 | favorite_color |
| 2204 | Expecting: |
| 2205 | 'blue' |
| 2206 | ok |
| 2207 | Trying: |
| 2208 | if 1: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 2209 | print('a') |
| 2210 | print() |
| 2211 | print('b') |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2212 | Expecting: |
| 2213 | a |
| 2214 | <BLANKLINE> |
| 2215 | b |
| 2216 | ok |
| 2217 | 1 items passed all tests: |
| 2218 | 2 tests in test_doctest.txt |
| 2219 | 2 tests in 1 items. |
| 2220 | 2 passed and 0 failed. |
| 2221 | Test passed. |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 2222 | TestResults(failed=0, attempted=2) |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2223 | >>> doctest.master = None # Reset master. |
| 2224 | |
| 2225 | The name of the test may be specified with the optional `name` |
| 2226 | parameter: |
| 2227 | |
| 2228 | >>> doctest.testfile('test_doctest.txt', name='newname') |
| 2229 | ... # doctest: +ELLIPSIS |
| 2230 | ********************************************************************** |
| 2231 | File "...", line 6, in newname |
| 2232 | ... |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 2233 | TestResults(failed=1, attempted=2) |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2234 | >>> doctest.master = None # Reset master. |
| 2235 | |
| 2236 | The summary report may be supressed with the optional `report` |
| 2237 | parameter: |
| 2238 | |
| 2239 | >>> doctest.testfile('test_doctest.txt', report=False) |
| 2240 | ... # doctest: +ELLIPSIS |
| 2241 | ********************************************************************** |
| 2242 | File "...", line 6, in test_doctest.txt |
| 2243 | Failed example: |
| 2244 | favorite_color |
| 2245 | Exception raised: |
| 2246 | ... |
| 2247 | NameError: name 'favorite_color' is not defined |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 2248 | TestResults(failed=1, attempted=2) |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2249 | >>> doctest.master = None # Reset master. |
| 2250 | |
| 2251 | The optional keyword argument `raise_on_error` can be used to raise an |
| 2252 | exception on the first error (which may be useful for postmortem |
| 2253 | debugging): |
| 2254 | |
| 2255 | >>> doctest.testfile('test_doctest.txt', raise_on_error=True) |
| 2256 | ... # doctest: +ELLIPSIS |
| 2257 | Traceback (most recent call last): |
Guido van Rossum | 6a2a2a0 | 2006-08-26 20:37:44 +0000 | [diff] [blame] | 2258 | doctest.UnexpectedException: ... |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2259 | >>> doctest.master = None # Reset master. |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2260 | |
| 2261 | If the tests contain non-ASCII characters, the tests might fail, since |
| 2262 | it's unknown which encoding is used. The encoding can be specified |
| 2263 | using the optional keyword argument `encoding`: |
| 2264 | |
Martin v. Löwis | b1a9f27 | 2007-07-20 07:13:39 +0000 | [diff] [blame] | 2265 | >>> doctest.testfile('test_doctest4.txt', encoding='latin-1') # doctest: +ELLIPSIS |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2266 | ********************************************************************** |
| 2267 | File "...", line 7, in test_doctest4.txt |
| 2268 | Failed example: |
Martin v. Löwis | b1a9f27 | 2007-07-20 07:13:39 +0000 | [diff] [blame] | 2269 | '...' |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2270 | Expected: |
Martin v. Löwis | b1a9f27 | 2007-07-20 07:13:39 +0000 | [diff] [blame] | 2271 | 'f\xf6\xf6' |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2272 | Got: |
Martin v. Löwis | b1a9f27 | 2007-07-20 07:13:39 +0000 | [diff] [blame] | 2273 | 'f\xc3\xb6\xc3\xb6' |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2274 | ********************************************************************** |
| 2275 | ... |
| 2276 | ********************************************************************** |
| 2277 | 1 items had failures: |
Martin v. Löwis | b1a9f27 | 2007-07-20 07:13:39 +0000 | [diff] [blame] | 2278 | 2 of 2 in test_doctest4.txt |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2279 | ***Test Failed*** 2 failures. |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 2280 | TestResults(failed=2, attempted=2) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2281 | >>> doctest.master = None # Reset master. |
| 2282 | |
| 2283 | >>> doctest.testfile('test_doctest4.txt', encoding='utf-8') |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 2284 | TestResults(failed=0, attempted=2) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2285 | >>> doctest.master = None # Reset master. |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2286 | """ |
| 2287 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2288 | ###################################################################### |
| 2289 | ## Main |
| 2290 | ###################################################################### |
| 2291 | |
| 2292 | def test_main(): |
| 2293 | # Check the doctest cases in doctest itself: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2294 | support.run_doctest(doctest, verbosity=True) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2295 | # Check the doctest cases defined here: |
| 2296 | from test import test_doctest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2297 | support.run_doctest(test_doctest, verbosity=True) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2298 | |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 2299 | import trace, sys, re, io |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2300 | def test_coverage(coverdir): |
| 2301 | tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,], |
| 2302 | trace=0, count=1) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 2303 | tracer.run('test_main()') |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2304 | r = tracer.results() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 2305 | print('Writing coverage results...') |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2306 | r.write_results(show_missing=True, summary=True, |
| 2307 | coverdir=coverdir) |
| 2308 | |
| 2309 | if __name__ == '__main__': |
| 2310 | if '-c' in sys.argv: |
| 2311 | test_coverage('/tmp/doctest.cover') |
| 2312 | else: |
| 2313 | test_main() |