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