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