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