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