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