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