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