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