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