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