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 |
Miss Islington (bot) | 5a0c398 | 2018-03-06 07:16:11 -0800 | [diff] [blame^] | 10 | import importlib |
| 11 | import unittest |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 12 | |
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 |
Miss Islington (bot) | 5a0c398 | 2018-03-06 07:16:11 -0800 | [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 |
Zachary Ware | a4b7a75 | 2013-11-24 01:19:09 -0600 | [diff] [blame] | 668 | 8 |
| 669 | >>> for t in real_tests: |
| 670 | ... print('{} {}'.format(len(t.examples), t.name)) |
| 671 | ... |
| 672 | 1 builtins.bin |
| 673 | 3 builtins.float.as_integer_ratio |
| 674 | 2 builtins.float.fromhex |
| 675 | 2 builtins.float.hex |
| 676 | 1 builtins.hex |
| 677 | 1 builtins.int |
| 678 | 2 builtins.int.bit_length |
| 679 | 1 builtins.oct |
| 680 | |
| 681 | Note here that 'bin', 'oct', and 'hex' are functions; 'float.as_integer_ratio', |
| 682 | 'float.hex', and 'int.bit_length' are methods; 'float.fromhex' is a classmethod, |
| 683 | and 'int' is a type. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 684 | """ |
| 685 | |
Miss Islington (bot) | 5a0c398 | 2018-03-06 07:16:11 -0800 | [diff] [blame^] | 686 | |
| 687 | class TestDocTestFinder(unittest.TestCase): |
| 688 | |
| 689 | def test_empty_namespace_package(self): |
| 690 | pkg_name = 'doctest_empty_pkg' |
| 691 | os.mkdir(pkg_name) |
| 692 | mod = importlib.import_module(pkg_name) |
| 693 | assert doctest.DocTestFinder().find(mod) == [] |
| 694 | os.rmdir(pkg_name) |
| 695 | |
| 696 | |
Edward Loper | 00f8da7 | 2004-08-26 18:05:07 +0000 | [diff] [blame] | 697 | def test_DocTestParser(): r""" |
| 698 | Unit tests for the `DocTestParser` class. |
| 699 | |
| 700 | DocTestParser is used to parse docstrings containing doctest examples. |
| 701 | |
| 702 | The `parse` method divides a docstring into examples and intervening |
| 703 | text: |
| 704 | |
| 705 | >>> s = ''' |
| 706 | ... >>> x, y = 2, 3 # no output expected |
| 707 | ... >>> if 1: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 708 | ... ... print(x) |
| 709 | ... ... print(y) |
Edward Loper | 00f8da7 | 2004-08-26 18:05:07 +0000 | [diff] [blame] | 710 | ... 2 |
| 711 | ... 3 |
| 712 | ... |
| 713 | ... Some text. |
| 714 | ... >>> x+y |
| 715 | ... 5 |
| 716 | ... ''' |
| 717 | >>> parser = doctest.DocTestParser() |
| 718 | >>> for piece in parser.parse(s): |
| 719 | ... if isinstance(piece, doctest.Example): |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 720 | ... print('Example:', (piece.source, piece.want, piece.lineno)) |
Edward Loper | 00f8da7 | 2004-08-26 18:05:07 +0000 | [diff] [blame] | 721 | ... else: |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 722 | ... print(' Text:', repr(piece)) |
Edward Loper | 00f8da7 | 2004-08-26 18:05:07 +0000 | [diff] [blame] | 723 | Text: '\n' |
| 724 | Example: ('x, y = 2, 3 # no output expected\n', '', 1) |
| 725 | Text: '' |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 726 | 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] | 727 | Text: '\nSome text.\n' |
| 728 | Example: ('x+y\n', '5\n', 9) |
| 729 | Text: '' |
| 730 | |
| 731 | The `get_examples` method returns just the examples: |
| 732 | |
| 733 | >>> for piece in parser.get_examples(s): |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 734 | ... print((piece.source, piece.want, piece.lineno)) |
Edward Loper | 00f8da7 | 2004-08-26 18:05:07 +0000 | [diff] [blame] | 735 | ('x, y = 2, 3 # no output expected\n', '', 1) |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 736 | ('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] | 737 | ('x+y\n', '5\n', 9) |
| 738 | |
| 739 | The `get_doctest` method creates a Test from the examples, along with the |
| 740 | given arguments: |
| 741 | |
| 742 | >>> test = parser.get_doctest(s, {}, 'name', 'filename', lineno=5) |
| 743 | >>> (test.name, test.filename, test.lineno) |
| 744 | ('name', 'filename', 5) |
| 745 | >>> for piece in test.examples: |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 746 | ... print((piece.source, piece.want, piece.lineno)) |
Edward Loper | 00f8da7 | 2004-08-26 18:05:07 +0000 | [diff] [blame] | 747 | ('x, y = 2, 3 # no output expected\n', '', 1) |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 748 | ('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] | 749 | ('x+y\n', '5\n', 9) |
| 750 | """ |
| 751 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 752 | class test_DocTestRunner: |
| 753 | def basics(): r""" |
| 754 | Unit tests for the `DocTestRunner` class. |
| 755 | |
| 756 | DocTestRunner is used to run DocTest test cases, and to accumulate |
| 757 | statistics. Here's a simple DocTest case we can use: |
| 758 | |
| 759 | >>> def f(x): |
| 760 | ... ''' |
| 761 | ... >>> x = 12 |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 762 | ... >>> print(x) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 763 | ... 12 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 764 | ... >>> x//2 |
| 765 | ... 6 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 766 | ... ''' |
| 767 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 768 | |
| 769 | The main DocTestRunner interface is the `run` method, which runs a |
| 770 | given DocTest case in a given namespace (globs). It returns a tuple |
| 771 | `(f,t)`, where `f` is the number of failed tests and `t` is the number |
| 772 | of tried tests. |
| 773 | |
| 774 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 775 | TestResults(failed=0, attempted=3) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 776 | |
| 777 | If any example produces incorrect output, then the test runner reports |
| 778 | the failure and proceeds to the next example: |
| 779 | |
| 780 | >>> def f(x): |
| 781 | ... ''' |
| 782 | ... >>> x = 12 |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 783 | ... >>> print(x) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 784 | ... 14 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 785 | ... >>> x//2 |
| 786 | ... 6 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 787 | ... ''' |
| 788 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 789 | >>> doctest.DocTestRunner(verbose=True).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 790 | ... # doctest: +ELLIPSIS |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 791 | Trying: |
| 792 | x = 12 |
| 793 | Expecting nothing |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 794 | ok |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 795 | Trying: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 796 | print(x) |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 797 | Expecting: |
| 798 | 14 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 799 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 800 | File ..., line 4, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 801 | Failed example: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 802 | print(x) |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 803 | Expected: |
| 804 | 14 |
| 805 | Got: |
| 806 | 12 |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 807 | Trying: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 808 | x//2 |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 809 | Expecting: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 810 | 6 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 811 | ok |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 812 | TestResults(failed=1, attempted=3) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 813 | """ |
| 814 | def verbose_flag(): r""" |
| 815 | The `verbose` flag makes the test runner generate more detailed |
| 816 | output: |
| 817 | |
| 818 | >>> def f(x): |
| 819 | ... ''' |
| 820 | ... >>> x = 12 |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 821 | ... >>> print(x) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 822 | ... 12 |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 823 | ... >>> x//2 |
| 824 | ... 6 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 825 | ... ''' |
| 826 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 827 | |
| 828 | >>> doctest.DocTestRunner(verbose=True).run(test) |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 829 | Trying: |
| 830 | x = 12 |
| 831 | Expecting nothing |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 832 | ok |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 833 | Trying: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 834 | print(x) |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 835 | Expecting: |
| 836 | 12 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 837 | ok |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 838 | Trying: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 839 | x//2 |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 840 | Expecting: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 841 | 6 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 842 | ok |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 843 | TestResults(failed=0, attempted=3) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 844 | |
| 845 | If the `verbose` flag is unspecified, then the output will be verbose |
| 846 | iff `-v` appears in sys.argv: |
| 847 | |
| 848 | >>> # Save the real sys.argv list. |
| 849 | >>> old_argv = sys.argv |
| 850 | |
| 851 | >>> # If -v does not appear in sys.argv, then output isn't verbose. |
| 852 | >>> sys.argv = ['test'] |
| 853 | >>> doctest.DocTestRunner().run(test) |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 854 | TestResults(failed=0, attempted=3) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 855 | |
| 856 | >>> # If -v does appear in sys.argv, then output is verbose. |
| 857 | >>> sys.argv = ['test', '-v'] |
| 858 | >>> doctest.DocTestRunner().run(test) |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 859 | Trying: |
| 860 | x = 12 |
| 861 | Expecting nothing |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 862 | ok |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 863 | Trying: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 864 | print(x) |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 865 | Expecting: |
| 866 | 12 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 867 | ok |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 868 | Trying: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 869 | x//2 |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 870 | Expecting: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 871 | 6 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 872 | ok |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 873 | TestResults(failed=0, attempted=3) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 874 | |
| 875 | >>> # Restore sys.argv |
| 876 | >>> sys.argv = old_argv |
| 877 | |
| 878 | In the remaining examples, the test runner's verbosity will be |
| 879 | explicitly set, to ensure that the test behavior is consistent. |
| 880 | """ |
| 881 | def exceptions(): r""" |
| 882 | Tests of `DocTestRunner`'s exception handling. |
| 883 | |
| 884 | An expected exception is specified with a traceback message. The |
| 885 | lines between the first line and the type/value may be omitted or |
| 886 | replaced with any other string: |
| 887 | |
| 888 | >>> def f(x): |
| 889 | ... ''' |
| 890 | ... >>> x = 12 |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 891 | ... >>> print(x//0) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 892 | ... Traceback (most recent call last): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 893 | ... ZeroDivisionError: integer division or modulo by zero |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 894 | ... ''' |
| 895 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 896 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 897 | TestResults(failed=0, attempted=2) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 898 | |
Edward Loper | 19b1958 | 2004-08-25 23:07:03 +0000 | [diff] [blame] | 899 | An example may not generate output before it raises an exception; if |
| 900 | it does, then the traceback message will not be recognized as |
| 901 | signaling an expected exception, so the example will be reported as an |
| 902 | unexpected exception: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 903 | |
| 904 | >>> def f(x): |
| 905 | ... ''' |
| 906 | ... >>> x = 12 |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 907 | ... >>> print('pre-exception output', x//0) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 908 | ... pre-exception output |
| 909 | ... Traceback (most recent call last): |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 910 | ... ZeroDivisionError: integer division or modulo by zero |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 911 | ... ''' |
| 912 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 913 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Edward Loper | 19b1958 | 2004-08-25 23:07:03 +0000 | [diff] [blame] | 914 | ... # doctest: +ELLIPSIS |
| 915 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 916 | File ..., line 4, in f |
Edward Loper | 19b1958 | 2004-08-25 23:07:03 +0000 | [diff] [blame] | 917 | Failed example: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 918 | print('pre-exception output', x//0) |
Edward Loper | 19b1958 | 2004-08-25 23:07:03 +0000 | [diff] [blame] | 919 | Exception raised: |
| 920 | ... |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 921 | ZeroDivisionError: integer division or modulo by zero |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 922 | TestResults(failed=1, attempted=2) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 923 | |
| 924 | Exception messages may contain newlines: |
| 925 | |
| 926 | >>> def f(x): |
| 927 | ... r''' |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 928 | ... >>> raise ValueError('multi\nline\nmessage') |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 929 | ... Traceback (most recent call last): |
| 930 | ... ValueError: multi |
| 931 | ... line |
| 932 | ... message |
| 933 | ... ''' |
| 934 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 935 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 936 | TestResults(failed=0, attempted=1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 937 | |
| 938 | If an exception is expected, but an exception with the wrong type or |
| 939 | message is raised, then it is reported as a failure: |
| 940 | |
| 941 | >>> def f(x): |
| 942 | ... r''' |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 943 | ... >>> raise ValueError('message') |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 944 | ... Traceback (most recent call last): |
| 945 | ... ValueError: wrong message |
| 946 | ... ''' |
| 947 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 948 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 949 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 950 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 951 | File ..., line 3, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 952 | Failed example: |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 953 | raise ValueError('message') |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 954 | Expected: |
| 955 | Traceback (most recent call last): |
| 956 | ValueError: wrong message |
| 957 | Got: |
| 958 | Traceback (most recent call last): |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 959 | ... |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 960 | ValueError: message |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 961 | TestResults(failed=1, attempted=1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 962 | |
Tim Peters | 1fbf9c5 | 2004-09-04 17:21:02 +0000 | [diff] [blame] | 963 | However, IGNORE_EXCEPTION_DETAIL can be used to allow a mismatch in the |
| 964 | detail: |
| 965 | |
| 966 | >>> def f(x): |
| 967 | ... r''' |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 968 | ... >>> raise ValueError('message') #doctest: +IGNORE_EXCEPTION_DETAIL |
Tim Peters | 1fbf9c5 | 2004-09-04 17:21:02 +0000 | [diff] [blame] | 969 | ... Traceback (most recent call last): |
| 970 | ... ValueError: wrong message |
| 971 | ... ''' |
| 972 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 973 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 974 | TestResults(failed=0, attempted=1) |
Tim Peters | 1fbf9c5 | 2004-09-04 17:21:02 +0000 | [diff] [blame] | 975 | |
Nick Coghlan | 5e76e94 | 2010-06-12 13:42:46 +0000 | [diff] [blame] | 976 | IGNORE_EXCEPTION_DETAIL also ignores difference in exception formatting |
| 977 | between Python versions. For example, in Python 2.x, the module path of |
| 978 | the exception is not in the output, but this will fail under Python 3: |
| 979 | |
| 980 | >>> def f(x): |
| 981 | ... r''' |
| 982 | ... >>> from http.client import HTTPException |
| 983 | ... >>> raise HTTPException('message') |
| 984 | ... Traceback (most recent call last): |
| 985 | ... HTTPException: message |
| 986 | ... ''' |
| 987 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 988 | >>> doctest.DocTestRunner(verbose=False).run(test) |
| 989 | ... # doctest: +ELLIPSIS |
| 990 | ********************************************************************** |
| 991 | File ..., line 4, in f |
| 992 | Failed example: |
| 993 | raise HTTPException('message') |
| 994 | Expected: |
| 995 | Traceback (most recent call last): |
| 996 | HTTPException: message |
| 997 | Got: |
| 998 | Traceback (most recent call last): |
| 999 | ... |
| 1000 | http.client.HTTPException: message |
| 1001 | TestResults(failed=1, attempted=2) |
| 1002 | |
| 1003 | But in Python 3 the module path is included, and therefore a test must look |
| 1004 | like the following test to succeed in Python 3. But that test will fail under |
| 1005 | Python 2. |
| 1006 | |
| 1007 | >>> def f(x): |
| 1008 | ... r''' |
| 1009 | ... >>> from http.client import HTTPException |
| 1010 | ... >>> raise HTTPException('message') |
| 1011 | ... Traceback (most recent call last): |
| 1012 | ... http.client.HTTPException: message |
| 1013 | ... ''' |
| 1014 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1015 | >>> doctest.DocTestRunner(verbose=False).run(test) |
| 1016 | TestResults(failed=0, attempted=2) |
| 1017 | |
| 1018 | However, with IGNORE_EXCEPTION_DETAIL, the module name of the exception |
| 1019 | (or its unexpected absence) will be ignored: |
| 1020 | |
| 1021 | >>> def f(x): |
| 1022 | ... r''' |
| 1023 | ... >>> from http.client import HTTPException |
| 1024 | ... >>> raise HTTPException('message') #doctest: +IGNORE_EXCEPTION_DETAIL |
| 1025 | ... Traceback (most recent call last): |
| 1026 | ... 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 | The module path will be completely ignored, so two different module paths will |
| 1033 | still pass if IGNORE_EXCEPTION_DETAIL is given. This is intentional, so it can |
| 1034 | be used when exceptions have changed module. |
| 1035 | |
| 1036 | >>> def f(x): |
| 1037 | ... r''' |
| 1038 | ... >>> from http.client import HTTPException |
| 1039 | ... >>> raise HTTPException('message') #doctest: +IGNORE_EXCEPTION_DETAIL |
| 1040 | ... Traceback (most recent call last): |
| 1041 | ... foo.bar.HTTPException: message |
| 1042 | ... ''' |
| 1043 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1044 | >>> doctest.DocTestRunner(verbose=False).run(test) |
| 1045 | TestResults(failed=0, attempted=2) |
| 1046 | |
Tim Peters | 1fbf9c5 | 2004-09-04 17:21:02 +0000 | [diff] [blame] | 1047 | But IGNORE_EXCEPTION_DETAIL does not allow a mismatch in the exception type: |
| 1048 | |
| 1049 | >>> def f(x): |
| 1050 | ... r''' |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 1051 | ... >>> raise ValueError('message') #doctest: +IGNORE_EXCEPTION_DETAIL |
Tim Peters | 1fbf9c5 | 2004-09-04 17:21:02 +0000 | [diff] [blame] | 1052 | ... Traceback (most recent call last): |
| 1053 | ... TypeError: wrong type |
| 1054 | ... ''' |
| 1055 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1056 | >>> doctest.DocTestRunner(verbose=False).run(test) |
| 1057 | ... # doctest: +ELLIPSIS |
| 1058 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1059 | File ..., line 3, in f |
Tim Peters | 1fbf9c5 | 2004-09-04 17:21:02 +0000 | [diff] [blame] | 1060 | Failed example: |
Collin Winter | 3add4d7 | 2007-08-29 23:37:32 +0000 | [diff] [blame] | 1061 | raise ValueError('message') #doctest: +IGNORE_EXCEPTION_DETAIL |
Tim Peters | 1fbf9c5 | 2004-09-04 17:21:02 +0000 | [diff] [blame] | 1062 | Expected: |
| 1063 | Traceback (most recent call last): |
| 1064 | TypeError: wrong type |
| 1065 | Got: |
| 1066 | Traceback (most recent call last): |
| 1067 | ... |
| 1068 | ValueError: message |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1069 | TestResults(failed=1, attempted=1) |
Tim Peters | 1fbf9c5 | 2004-09-04 17:21:02 +0000 | [diff] [blame] | 1070 | |
Tim Peters | f9a07f2 | 2013-12-03 21:02:05 -0600 | [diff] [blame] | 1071 | If the exception does not have a message, you can still use |
| 1072 | IGNORE_EXCEPTION_DETAIL to normalize the modules between Python 2 and 3: |
| 1073 | |
| 1074 | >>> def f(x): |
| 1075 | ... r''' |
| 1076 | ... >>> from http.client import HTTPException |
| 1077 | ... >>> raise HTTPException() #doctest: +IGNORE_EXCEPTION_DETAIL |
| 1078 | ... Traceback (most recent call last): |
| 1079 | ... foo.bar.HTTPException |
| 1080 | ... ''' |
| 1081 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1082 | >>> doctest.DocTestRunner(verbose=False).run(test) |
| 1083 | TestResults(failed=0, attempted=2) |
| 1084 | |
| 1085 | Note that a trailing colon doesn't matter either: |
| 1086 | |
| 1087 | >>> def f(x): |
| 1088 | ... r''' |
| 1089 | ... >>> from http.client import HTTPException |
| 1090 | ... >>> raise HTTPException() #doctest: +IGNORE_EXCEPTION_DETAIL |
| 1091 | ... Traceback (most recent call last): |
| 1092 | ... foo.bar.HTTPException: |
| 1093 | ... ''' |
| 1094 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1095 | >>> doctest.DocTestRunner(verbose=False).run(test) |
| 1096 | TestResults(failed=0, attempted=2) |
| 1097 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1098 | If an exception is raised but not expected, then it is reported as an |
| 1099 | unexpected exception: |
| 1100 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1101 | >>> def f(x): |
| 1102 | ... r''' |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1103 | ... >>> 1//0 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1104 | ... 0 |
| 1105 | ... ''' |
| 1106 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1107 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1108 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1109 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1110 | File ..., line 3, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1111 | Failed example: |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1112 | 1//0 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1113 | Exception raised: |
| 1114 | Traceback (most recent call last): |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1115 | ... |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 1116 | ZeroDivisionError: integer division or modulo by zero |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1117 | TestResults(failed=1, attempted=1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1118 | """ |
Georg Brandl | 25fbb89 | 2010-07-30 09:23:23 +0000 | [diff] [blame] | 1119 | def displayhook(): r""" |
| 1120 | Test that changing sys.displayhook doesn't matter for doctest. |
| 1121 | |
| 1122 | >>> import sys |
| 1123 | >>> orig_displayhook = sys.displayhook |
| 1124 | >>> def my_displayhook(x): |
| 1125 | ... print('hi!') |
| 1126 | >>> sys.displayhook = my_displayhook |
| 1127 | >>> def f(): |
| 1128 | ... ''' |
| 1129 | ... >>> 3 |
| 1130 | ... 3 |
| 1131 | ... ''' |
| 1132 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1133 | >>> r = doctest.DocTestRunner(verbose=False).run(test) |
| 1134 | >>> post_displayhook = sys.displayhook |
| 1135 | |
| 1136 | We need to restore sys.displayhook now, so that we'll be able to test |
| 1137 | results. |
| 1138 | |
| 1139 | >>> sys.displayhook = orig_displayhook |
| 1140 | |
| 1141 | Ok, now we can check that everything is ok. |
| 1142 | |
| 1143 | >>> r |
| 1144 | TestResults(failed=0, attempted=1) |
| 1145 | >>> post_displayhook is my_displayhook |
| 1146 | True |
| 1147 | """ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1148 | def optionflags(): r""" |
| 1149 | Tests of `DocTestRunner`'s option flag handling. |
| 1150 | |
| 1151 | Several option flags can be used to customize the behavior of the test |
| 1152 | runner. These are defined as module constants in doctest, and passed |
Christian Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 1153 | to the DocTestRunner constructor (multiple constants should be ORed |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1154 | together). |
| 1155 | |
| 1156 | The DONT_ACCEPT_TRUE_FOR_1 flag disables matches between True/False |
| 1157 | and 1/0: |
| 1158 | |
| 1159 | >>> def f(x): |
| 1160 | ... '>>> True\n1\n' |
| 1161 | |
| 1162 | >>> # Without the flag: |
| 1163 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1164 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1165 | TestResults(failed=0, attempted=1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1166 | |
| 1167 | >>> # With the flag: |
| 1168 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1169 | >>> flags = doctest.DONT_ACCEPT_TRUE_FOR_1 |
| 1170 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1171 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1172 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1173 | File ..., line 2, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1174 | Failed example: |
| 1175 | True |
| 1176 | Expected: |
| 1177 | 1 |
| 1178 | Got: |
| 1179 | True |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1180 | TestResults(failed=1, attempted=1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1181 | |
| 1182 | The DONT_ACCEPT_BLANKLINE flag disables the match between blank lines |
| 1183 | and the '<BLANKLINE>' marker: |
| 1184 | |
| 1185 | >>> def f(x): |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1186 | ... '>>> print("a\\n\\nb")\na\n<BLANKLINE>\nb\n' |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1187 | |
| 1188 | >>> # Without the flag: |
| 1189 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1190 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1191 | TestResults(failed=0, attempted=1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1192 | |
| 1193 | >>> # With the flag: |
| 1194 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1195 | >>> flags = doctest.DONT_ACCEPT_BLANKLINE |
| 1196 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1197 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1198 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1199 | File ..., line 2, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1200 | Failed example: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1201 | print("a\n\nb") |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1202 | Expected: |
| 1203 | a |
| 1204 | <BLANKLINE> |
| 1205 | b |
| 1206 | Got: |
| 1207 | a |
| 1208 | <BLANKLINE> |
| 1209 | b |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1210 | TestResults(failed=1, attempted=1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1211 | |
| 1212 | The NORMALIZE_WHITESPACE flag causes all sequences of whitespace to be |
| 1213 | treated as equal: |
| 1214 | |
| 1215 | >>> def f(x): |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1216 | ... '>>> print(1, 2, 3)\n 1 2\n 3' |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1217 | |
| 1218 | >>> # Without the flag: |
| 1219 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1220 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1221 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1222 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1223 | File ..., line 2, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1224 | Failed example: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1225 | print(1, 2, 3) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1226 | Expected: |
| 1227 | 1 2 |
| 1228 | 3 |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1229 | Got: |
| 1230 | 1 2 3 |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1231 | TestResults(failed=1, attempted=1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1232 | |
| 1233 | >>> # With the flag: |
| 1234 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1235 | >>> flags = doctest.NORMALIZE_WHITESPACE |
| 1236 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1237 | TestResults(failed=0, attempted=1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1238 | |
Tim Peters | 026f8dc | 2004-08-19 16:38:58 +0000 | [diff] [blame] | 1239 | An example from the docs: |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1240 | >>> print(list(range(20))) #doctest: +NORMALIZE_WHITESPACE |
Tim Peters | 026f8dc | 2004-08-19 16:38:58 +0000 | [diff] [blame] | 1241 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, |
| 1242 | 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] |
| 1243 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1244 | The ELLIPSIS flag causes ellipsis marker ("...") in the expected |
| 1245 | output to match any substring in the actual output: |
| 1246 | |
| 1247 | >>> def f(x): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1248 | ... '>>> print(list(range(15)))\n[0, 1, 2, ..., 14]\n' |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1249 | |
| 1250 | >>> # Without the flag: |
| 1251 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1252 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1253 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1254 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1255 | File ..., line 2, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1256 | Failed example: |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1257 | print(list(range(15))) |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1258 | Expected: |
| 1259 | [0, 1, 2, ..., 14] |
| 1260 | Got: |
| 1261 | [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] | 1262 | TestResults(failed=1, attempted=1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1263 | |
| 1264 | >>> # With the flag: |
| 1265 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1266 | >>> flags = doctest.ELLIPSIS |
| 1267 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1268 | TestResults(failed=0, attempted=1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1269 | |
Tim Peters | e594bee | 2004-08-22 01:47:51 +0000 | [diff] [blame] | 1270 | ... also matches nothing: |
Tim Peters | 1cf3aa6 | 2004-08-19 06:49:33 +0000 | [diff] [blame] | 1271 | |
Guido van Rossum | e0192e5 | 2007-02-09 23:39:59 +0000 | [diff] [blame] | 1272 | >>> if 1: |
| 1273 | ... for i in range(100): |
| 1274 | ... print(i**2, end=' ') #doctest: +ELLIPSIS |
| 1275 | ... print('!') |
| 1276 | 0 1...4...9 16 ... 36 49 64 ... 9801 ! |
Tim Peters | 1cf3aa6 | 2004-08-19 06:49:33 +0000 | [diff] [blame] | 1277 | |
Tim Peters | 026f8dc | 2004-08-19 16:38:58 +0000 | [diff] [blame] | 1278 | ... can be surprising; e.g., this test passes: |
Tim Peters | 26b3ebb | 2004-08-19 08:10:08 +0000 | [diff] [blame] | 1279 | |
Guido van Rossum | e0192e5 | 2007-02-09 23:39:59 +0000 | [diff] [blame] | 1280 | >>> if 1: #doctest: +ELLIPSIS |
| 1281 | ... for i in range(20): |
| 1282 | ... print(i, end=' ') |
| 1283 | ... print(20) |
Tim Peters | e594bee | 2004-08-22 01:47:51 +0000 | [diff] [blame] | 1284 | 0 1 2 ...1...2...0 |
Tim Peters | 26b3ebb | 2004-08-19 08:10:08 +0000 | [diff] [blame] | 1285 | |
Tim Peters | 026f8dc | 2004-08-19 16:38:58 +0000 | [diff] [blame] | 1286 | Examples from the docs: |
| 1287 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1288 | >>> print(list(range(20))) # doctest:+ELLIPSIS |
Tim Peters | 026f8dc | 2004-08-19 16:38:58 +0000 | [diff] [blame] | 1289 | [0, 1, ..., 18, 19] |
| 1290 | |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1291 | >>> print(list(range(20))) # doctest: +ELLIPSIS |
Tim Peters | 026f8dc | 2004-08-19 16:38:58 +0000 | [diff] [blame] | 1292 | ... # doctest: +NORMALIZE_WHITESPACE |
| 1293 | [0, 1, ..., 18, 19] |
| 1294 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1295 | The SKIP flag causes an example to be skipped entirely. I.e., the |
| 1296 | example is not run. It can be useful in contexts where doctest |
| 1297 | examples serve as both documentation and test cases, and an example |
| 1298 | should be included for documentation purposes, but should not be |
| 1299 | checked (e.g., because its output is random, or depends on resources |
| 1300 | which would be unavailable.) The SKIP flag can also be used for |
| 1301 | 'commenting out' broken examples. |
| 1302 | |
| 1303 | >>> import unavailable_resource # doctest: +SKIP |
| 1304 | >>> unavailable_resource.do_something() # doctest: +SKIP |
| 1305 | >>> unavailable_resource.blow_up() # doctest: +SKIP |
| 1306 | Traceback (most recent call last): |
| 1307 | ... |
| 1308 | UncheckedBlowUpError: Nobody checks me. |
| 1309 | |
| 1310 | >>> import random |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 1311 | >>> print(random.random()) # doctest: +SKIP |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1312 | 0.721216923889 |
| 1313 | |
Edward Loper | 71f55af | 2004-08-26 01:41:51 +0000 | [diff] [blame] | 1314 | The REPORT_UDIFF flag causes failures that involve multi-line expected |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1315 | and actual outputs to be displayed using a unified diff: |
| 1316 | |
| 1317 | >>> def f(x): |
| 1318 | ... r''' |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1319 | ... >>> print('\n'.join('abcdefg')) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1320 | ... a |
| 1321 | ... B |
| 1322 | ... c |
| 1323 | ... d |
| 1324 | ... f |
| 1325 | ... g |
| 1326 | ... h |
| 1327 | ... ''' |
| 1328 | |
| 1329 | >>> # Without the flag: |
| 1330 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1331 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1332 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1333 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1334 | File ..., line 3, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1335 | Failed example: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1336 | print('\n'.join('abcdefg')) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1337 | Expected: |
| 1338 | a |
| 1339 | B |
| 1340 | c |
| 1341 | d |
| 1342 | f |
| 1343 | g |
| 1344 | h |
| 1345 | Got: |
| 1346 | a |
| 1347 | b |
| 1348 | c |
| 1349 | d |
| 1350 | e |
| 1351 | f |
| 1352 | g |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1353 | TestResults(failed=1, attempted=1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1354 | |
| 1355 | >>> # With the flag: |
| 1356 | >>> test = doctest.DocTestFinder().find(f)[0] |
Edward Loper | 71f55af | 2004-08-26 01:41:51 +0000 | [diff] [blame] | 1357 | >>> flags = doctest.REPORT_UDIFF |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1358 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1359 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1360 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1361 | File ..., line 3, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1362 | Failed example: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1363 | print('\n'.join('abcdefg')) |
Edward Loper | 5662929 | 2004-08-26 01:31:56 +0000 | [diff] [blame] | 1364 | Differences (unified diff with -expected +actual): |
Tim Peters | e7edcb8 | 2004-08-26 05:44:27 +0000 | [diff] [blame] | 1365 | @@ -1,7 +1,7 @@ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1366 | a |
| 1367 | -B |
| 1368 | +b |
| 1369 | c |
| 1370 | d |
| 1371 | +e |
| 1372 | f |
| 1373 | g |
| 1374 | -h |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1375 | TestResults(failed=1, attempted=1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1376 | |
Edward Loper | 71f55af | 2004-08-26 01:41:51 +0000 | [diff] [blame] | 1377 | The REPORT_CDIFF flag causes failures that involve multi-line expected |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1378 | and actual outputs to be displayed using a context diff: |
| 1379 | |
Edward Loper | 71f55af | 2004-08-26 01:41:51 +0000 | [diff] [blame] | 1380 | >>> # Reuse f() from the REPORT_UDIFF example, above. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1381 | >>> test = doctest.DocTestFinder().find(f)[0] |
Edward Loper | 71f55af | 2004-08-26 01:41:51 +0000 | [diff] [blame] | 1382 | >>> flags = doctest.REPORT_CDIFF |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1383 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1384 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1385 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1386 | File ..., line 3, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1387 | Failed example: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1388 | print('\n'.join('abcdefg')) |
Edward Loper | 5662929 | 2004-08-26 01:31:56 +0000 | [diff] [blame] | 1389 | Differences (context diff with expected followed by actual): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1390 | *************** |
Tim Peters | e7edcb8 | 2004-08-26 05:44:27 +0000 | [diff] [blame] | 1391 | *** 1,7 **** |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1392 | a |
| 1393 | ! B |
| 1394 | c |
| 1395 | d |
| 1396 | f |
| 1397 | g |
| 1398 | - h |
Tim Peters | e7edcb8 | 2004-08-26 05:44:27 +0000 | [diff] [blame] | 1399 | --- 1,7 ---- |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1400 | a |
| 1401 | ! b |
| 1402 | c |
| 1403 | d |
| 1404 | + e |
| 1405 | f |
| 1406 | g |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1407 | TestResults(failed=1, attempted=1) |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 1408 | |
| 1409 | |
Edward Loper | 71f55af | 2004-08-26 01:41:51 +0000 | [diff] [blame] | 1410 | The REPORT_NDIFF flag causes failures to use the difflib.Differ algorithm |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 1411 | used by the popular ndiff.py utility. This does intraline difference |
| 1412 | marking, as well as interline differences. |
| 1413 | |
| 1414 | >>> def f(x): |
| 1415 | ... r''' |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1416 | ... >>> 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] | 1417 | ... a b c d e f g h i j k 1 m |
| 1418 | ... ''' |
| 1419 | >>> test = doctest.DocTestFinder().find(f)[0] |
Edward Loper | 71f55af | 2004-08-26 01:41:51 +0000 | [diff] [blame] | 1420 | >>> flags = doctest.REPORT_NDIFF |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 1421 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1422 | ... # doctest: +ELLIPSIS |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 1423 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1424 | File ..., line 3, in f |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 1425 | Failed example: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1426 | 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] | 1427 | Differences (ndiff with -expected +actual): |
| 1428 | - a b c d e f g h i j k 1 m |
| 1429 | ? ^ |
| 1430 | + a b c d e f g h i j k l m |
| 1431 | ? + ++ ^ |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1432 | TestResults(failed=1, attempted=1) |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1433 | |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 1434 | The REPORT_ONLY_FIRST_FAILURE suppresses result output after the first |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1435 | failing example: |
| 1436 | |
| 1437 | >>> def f(x): |
| 1438 | ... r''' |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1439 | ... >>> print(1) # first success |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1440 | ... 1 |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1441 | ... >>> print(2) # first failure |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1442 | ... 200 |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1443 | ... >>> print(3) # second failure |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1444 | ... 300 |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1445 | ... >>> print(4) # second success |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1446 | ... 4 |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1447 | ... >>> print(5) # third failure |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1448 | ... 500 |
| 1449 | ... ''' |
| 1450 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1451 | >>> flags = doctest.REPORT_ONLY_FIRST_FAILURE |
| 1452 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1453 | ... # doctest: +ELLIPSIS |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1454 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1455 | File ..., line 5, in f |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1456 | Failed example: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1457 | print(2) # first failure |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1458 | Expected: |
| 1459 | 200 |
| 1460 | Got: |
| 1461 | 2 |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1462 | TestResults(failed=3, attempted=5) |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1463 | |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 1464 | However, output from `report_start` is not suppressed: |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1465 | |
| 1466 | >>> doctest.DocTestRunner(verbose=True, 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 | Trying: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1469 | print(1) # first success |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1470 | Expecting: |
| 1471 | 1 |
| 1472 | ok |
| 1473 | Trying: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1474 | print(2) # first failure |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1475 | Expecting: |
| 1476 | 200 |
| 1477 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1478 | File ..., line 5, in f |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1479 | Failed example: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1480 | print(2) # first failure |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1481 | Expected: |
| 1482 | 200 |
| 1483 | Got: |
| 1484 | 2 |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1485 | TestResults(failed=3, attempted=5) |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1486 | |
R David Murray | 5a9d706 | 2012-11-21 15:09:21 -0500 | [diff] [blame] | 1487 | The FAIL_FAST flag causes the runner to exit after the first failing example, |
| 1488 | so subsequent examples are not even attempted: |
| 1489 | |
| 1490 | >>> flags = doctest.FAIL_FAST |
| 1491 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
| 1492 | ... # doctest: +ELLIPSIS |
| 1493 | ********************************************************************** |
| 1494 | File ..., line 5, in f |
| 1495 | Failed example: |
| 1496 | print(2) # first failure |
| 1497 | Expected: |
| 1498 | 200 |
| 1499 | Got: |
| 1500 | 2 |
| 1501 | TestResults(failed=1, attempted=2) |
| 1502 | |
| 1503 | Specifying both FAIL_FAST and REPORT_ONLY_FIRST_FAILURE is equivalent to |
| 1504 | FAIL_FAST only: |
| 1505 | |
| 1506 | >>> flags = doctest.FAIL_FAST | doctest.REPORT_ONLY_FIRST_FAILURE |
| 1507 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
| 1508 | ... # doctest: +ELLIPSIS |
| 1509 | ********************************************************************** |
| 1510 | File ..., line 5, in f |
| 1511 | Failed example: |
| 1512 | print(2) # first failure |
| 1513 | Expected: |
| 1514 | 200 |
| 1515 | Got: |
| 1516 | 2 |
| 1517 | TestResults(failed=1, attempted=2) |
| 1518 | |
| 1519 | For the purposes of both REPORT_ONLY_FIRST_FAILURE and FAIL_FAST, unexpected |
| 1520 | exceptions count as failures: |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1521 | |
| 1522 | >>> def f(x): |
| 1523 | ... r''' |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1524 | ... >>> print(1) # first success |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1525 | ... 1 |
| 1526 | ... >>> raise ValueError(2) # first failure |
| 1527 | ... 200 |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1528 | ... >>> print(3) # second failure |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1529 | ... 300 |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1530 | ... >>> print(4) # second success |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1531 | ... 4 |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1532 | ... >>> print(5) # third failure |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1533 | ... 500 |
| 1534 | ... ''' |
| 1535 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1536 | >>> flags = doctest.REPORT_ONLY_FIRST_FAILURE |
| 1537 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
| 1538 | ... # doctest: +ELLIPSIS |
| 1539 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1540 | File ..., line 5, in f |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1541 | Failed example: |
| 1542 | raise ValueError(2) # first failure |
| 1543 | Exception raised: |
| 1544 | ... |
| 1545 | ValueError: 2 |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1546 | TestResults(failed=3, attempted=5) |
R David Murray | 5a9d706 | 2012-11-21 15:09:21 -0500 | [diff] [blame] | 1547 | >>> flags = doctest.FAIL_FAST |
| 1548 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
| 1549 | ... # doctest: +ELLIPSIS |
| 1550 | ********************************************************************** |
| 1551 | File ..., line 5, in f |
| 1552 | Failed example: |
| 1553 | raise ValueError(2) # first failure |
| 1554 | Exception raised: |
| 1555 | ... |
| 1556 | ValueError: 2 |
| 1557 | TestResults(failed=1, attempted=2) |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1558 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 1559 | New option flags can also be registered, via register_optionflag(). Here |
| 1560 | we reach into doctest's internals a bit. |
| 1561 | |
| 1562 | >>> unlikely = "UNLIKELY_OPTION_NAME" |
| 1563 | >>> unlikely in doctest.OPTIONFLAGS_BY_NAME |
| 1564 | False |
| 1565 | >>> new_flag_value = doctest.register_optionflag(unlikely) |
| 1566 | >>> unlikely in doctest.OPTIONFLAGS_BY_NAME |
| 1567 | True |
| 1568 | |
| 1569 | Before 2.4.4/2.5, registering a name more than once erroneously created |
| 1570 | more than one flag value. Here we verify that's fixed: |
| 1571 | |
| 1572 | >>> redundant_flag_value = doctest.register_optionflag(unlikely) |
| 1573 | >>> redundant_flag_value == new_flag_value |
| 1574 | True |
| 1575 | |
| 1576 | Clean up. |
| 1577 | >>> del doctest.OPTIONFLAGS_BY_NAME[unlikely] |
| 1578 | |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 1579 | """ |
| 1580 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1581 | def option_directives(): r""" |
| 1582 | Tests of `DocTestRunner`'s option directive mechanism. |
| 1583 | |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1584 | Option directives can be used to turn option flags on or off for a |
| 1585 | single example. To turn an option on for an example, follow that |
| 1586 | example with a comment of the form ``# doctest: +OPTION``: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1587 | |
| 1588 | >>> def f(x): r''' |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1589 | ... >>> print(list(range(10))) # should fail: no ellipsis |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1590 | ... [0, 1, ..., 9] |
| 1591 | ... |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1592 | ... >>> print(list(range(10))) # doctest: +ELLIPSIS |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1593 | ... [0, 1, ..., 9] |
| 1594 | ... ''' |
| 1595 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1596 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1597 | ... # doctest: +ELLIPSIS |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1598 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1599 | File ..., line 2, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1600 | Failed example: |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1601 | print(list(range(10))) # should fail: no ellipsis |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1602 | Expected: |
| 1603 | [0, 1, ..., 9] |
| 1604 | Got: |
| 1605 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1606 | TestResults(failed=1, attempted=2) |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1607 | |
| 1608 | To turn an option off for an example, follow that example with a |
| 1609 | comment of the form ``# doctest: -OPTION``: |
| 1610 | |
| 1611 | >>> def f(x): r''' |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1612 | ... >>> print(list(range(10))) |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1613 | ... [0, 1, ..., 9] |
| 1614 | ... |
| 1615 | ... >>> # should fail: no ellipsis |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1616 | ... >>> print(list(range(10))) # doctest: -ELLIPSIS |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1617 | ... [0, 1, ..., 9] |
| 1618 | ... ''' |
| 1619 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1620 | >>> doctest.DocTestRunner(verbose=False, |
| 1621 | ... optionflags=doctest.ELLIPSIS).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1622 | ... # doctest: +ELLIPSIS |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1623 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1624 | File ..., line 6, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1625 | Failed example: |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1626 | print(list(range(10))) # doctest: -ELLIPSIS |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1627 | Expected: |
| 1628 | [0, 1, ..., 9] |
| 1629 | Got: |
| 1630 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1631 | TestResults(failed=1, attempted=2) |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1632 | |
| 1633 | Option directives affect only the example that they appear with; they |
| 1634 | do not change the options for surrounding examples: |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 1635 | |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1636 | >>> def f(x): r''' |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1637 | ... >>> print(list(range(10))) # Should fail: no ellipsis |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1638 | ... [0, 1, ..., 9] |
| 1639 | ... |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1640 | ... >>> print(list(range(10))) # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1641 | ... [0, 1, ..., 9] |
| 1642 | ... |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1643 | ... >>> print(list(range(10))) # Should fail: no ellipsis |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1644 | ... [0, 1, ..., 9] |
| 1645 | ... ''' |
| 1646 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1647 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1648 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1649 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1650 | File ..., line 2, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1651 | Failed example: |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1652 | print(list(range(10))) # Should fail: no ellipsis |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1653 | Expected: |
| 1654 | [0, 1, ..., 9] |
| 1655 | Got: |
| 1656 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1657 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1658 | File ..., line 8, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1659 | Failed example: |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1660 | print(list(range(10))) # Should fail: no ellipsis |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1661 | Expected: |
| 1662 | [0, 1, ..., 9] |
| 1663 | Got: |
| 1664 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1665 | TestResults(failed=2, attempted=3) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1666 | |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1667 | Multiple options may be modified by a single option directive. They |
| 1668 | may be separated by whitespace, commas, or both: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1669 | |
| 1670 | >>> def f(x): r''' |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1671 | ... >>> print(list(range(10))) # Should fail |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1672 | ... [0, 1, ..., 9] |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1673 | ... >>> print(list(range(10))) # Should succeed |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1674 | ... ... # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1675 | ... [0, 1, ..., 9] |
| 1676 | ... ''' |
| 1677 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1678 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1679 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1680 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1681 | File ..., line 2, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1682 | Failed example: |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1683 | print(list(range(10))) # Should fail |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1684 | Expected: |
| 1685 | [0, 1, ..., 9] |
| 1686 | Got: |
| 1687 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1688 | TestResults(failed=1, attempted=2) |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1689 | |
| 1690 | >>> def f(x): r''' |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1691 | ... >>> print(list(range(10))) # Should fail |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1692 | ... [0, 1, ..., 9] |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1693 | ... >>> print(list(range(10))) # Should succeed |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1694 | ... ... # doctest: +ELLIPSIS,+NORMALIZE_WHITESPACE |
| 1695 | ... [0, 1, ..., 9] |
| 1696 | ... ''' |
| 1697 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1698 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1699 | ... # doctest: +ELLIPSIS |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1700 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1701 | File ..., line 2, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1702 | Failed example: |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1703 | print(list(range(10))) # Should fail |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1704 | Expected: |
| 1705 | [0, 1, ..., 9] |
| 1706 | Got: |
| 1707 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1708 | TestResults(failed=1, attempted=2) |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1709 | |
| 1710 | >>> def f(x): r''' |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1711 | ... >>> print(list(range(10))) # Should fail |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1712 | ... [0, 1, ..., 9] |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1713 | ... >>> print(list(range(10))) # Should succeed |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1714 | ... ... # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 1715 | ... [0, 1, ..., 9] |
| 1716 | ... ''' |
| 1717 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1718 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1719 | ... # doctest: +ELLIPSIS |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1720 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1721 | File ..., line 2, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1722 | Failed example: |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1723 | print(list(range(10))) # Should fail |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1724 | Expected: |
| 1725 | [0, 1, ..., 9] |
| 1726 | Got: |
| 1727 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1728 | TestResults(failed=1, attempted=2) |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1729 | |
| 1730 | The option directive may be put on the line following the source, as |
| 1731 | long as a continuation prompt is used: |
| 1732 | |
| 1733 | >>> def f(x): r''' |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1734 | ... >>> print(list(range(10))) |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1735 | ... ... # doctest: +ELLIPSIS |
| 1736 | ... [0, 1, ..., 9] |
| 1737 | ... ''' |
| 1738 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1739 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1740 | TestResults(failed=0, attempted=1) |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 1741 | |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1742 | For examples with multi-line source, the option directive may appear |
| 1743 | at the end of any line: |
| 1744 | |
| 1745 | >>> def f(x): r''' |
| 1746 | ... >>> for x in range(10): # doctest: +ELLIPSIS |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1747 | ... ... print(' ', x, end='', sep='') |
| 1748 | ... 0 1 2 ... 9 |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1749 | ... |
| 1750 | ... >>> for x in range(10): |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 1751 | ... ... print(' ', x, end='', sep='') # doctest: +ELLIPSIS |
| 1752 | ... 0 1 2 ... 9 |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1753 | ... ''' |
| 1754 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1755 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1756 | TestResults(failed=0, attempted=2) |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1757 | |
| 1758 | If more than one line of an example with multi-line source has an |
| 1759 | option directive, then they are combined: |
| 1760 | |
| 1761 | >>> def f(x): r''' |
| 1762 | ... Should fail (option directive not on the last line): |
| 1763 | ... >>> for x in range(10): # doctest: +ELLIPSIS |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1764 | ... ... print(x, end=' ') # doctest: +NORMALIZE_WHITESPACE |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 1765 | ... 0 1 2...9 |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1766 | ... ''' |
| 1767 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1768 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 1769 | TestResults(failed=0, attempted=1) |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1770 | |
| 1771 | It is an error to have a comment of the form ``# doctest:`` that is |
| 1772 | *not* followed by words of the form ``+OPTION`` or ``-OPTION``, where |
| 1773 | ``OPTION`` is an option that has been registered with |
| 1774 | `register_option`: |
| 1775 | |
| 1776 | >>> # Error: Option not registered |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1777 | >>> s = '>>> print(12) #doctest: +BADOPTION' |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1778 | >>> test = doctest.DocTestParser().get_doctest(s, {}, 's', 's.py', 0) |
| 1779 | Traceback (most recent call last): |
| 1780 | ValueError: line 1 of the doctest for s has an invalid option: '+BADOPTION' |
| 1781 | |
| 1782 | >>> # Error: No + or - prefix |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1783 | >>> s = '>>> print(12) #doctest: ELLIPSIS' |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1784 | >>> test = doctest.DocTestParser().get_doctest(s, {}, 's', 's.py', 0) |
| 1785 | Traceback (most recent call last): |
| 1786 | ValueError: line 1 of the doctest for s has an invalid option: 'ELLIPSIS' |
| 1787 | |
| 1788 | It is an error to use an option directive on a line that contains no |
| 1789 | source: |
| 1790 | |
| 1791 | >>> s = '>>> # doctest: +ELLIPSIS' |
| 1792 | >>> test = doctest.DocTestParser().get_doctest(s, {}, 's', 's.py', 0) |
| 1793 | Traceback (most recent call last): |
| 1794 | 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] | 1795 | """ |
| 1796 | |
| 1797 | def test_testsource(): r""" |
| 1798 | Unit tests for `testsource()`. |
| 1799 | |
| 1800 | The testsource() function takes a module and a name, finds the (first) |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1801 | test with that name in that module, and converts it to a script. The |
| 1802 | example code is converted to regular Python code. The surrounding |
| 1803 | words and expected output are converted to comments: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1804 | |
| 1805 | >>> import test.test_doctest |
| 1806 | >>> name = 'test.test_doctest.sample_func' |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 1807 | >>> print(doctest.testsource(test.test_doctest, name)) |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 1808 | # Blah blah |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1809 | # |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1810 | print(sample_func(22)) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1811 | # Expected: |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 1812 | ## 44 |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1813 | # |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 1814 | # Yee ha! |
Georg Brandl | ecf93c7 | 2005-06-26 23:09:51 +0000 | [diff] [blame] | 1815 | <BLANKLINE> |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1816 | |
| 1817 | >>> name = 'test.test_doctest.SampleNewStyleClass' |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 1818 | >>> print(doctest.testsource(test.test_doctest, name)) |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1819 | print('1\n2\n3') |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1820 | # Expected: |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 1821 | ## 1 |
| 1822 | ## 2 |
| 1823 | ## 3 |
Georg Brandl | ecf93c7 | 2005-06-26 23:09:51 +0000 | [diff] [blame] | 1824 | <BLANKLINE> |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1825 | |
| 1826 | >>> name = 'test.test_doctest.SampleClass.a_classmethod' |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 1827 | >>> print(doctest.testsource(test.test_doctest, name)) |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1828 | print(SampleClass.a_classmethod(10)) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1829 | # Expected: |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 1830 | ## 12 |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1831 | print(SampleClass(0).a_classmethod(10)) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1832 | # Expected: |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 1833 | ## 12 |
Georg Brandl | ecf93c7 | 2005-06-26 23:09:51 +0000 | [diff] [blame] | 1834 | <BLANKLINE> |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1835 | """ |
| 1836 | |
| 1837 | def test_debug(): r""" |
| 1838 | |
| 1839 | Create a docstring that we want to debug: |
| 1840 | |
| 1841 | >>> s = ''' |
| 1842 | ... >>> x = 12 |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1843 | ... >>> print(x) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1844 | ... 12 |
| 1845 | ... ''' |
| 1846 | |
| 1847 | Create some fake stdin input, to feed to the debugger: |
| 1848 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1849 | >>> real_stdin = sys.stdin |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1850 | >>> sys.stdin = _FakeInput(['next', 'print(x)', 'continue']) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1851 | |
| 1852 | Run the debugger on the docstring, and then restore sys.stdin. |
| 1853 | |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1854 | >>> try: doctest.debug_src(s) |
| 1855 | ... finally: sys.stdin = real_stdin |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1856 | > <string>(1)<module>() |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1857 | (Pdb) next |
| 1858 | 12 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1859 | --Return-- |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1860 | > <string>(1)<module>()->None |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 1861 | (Pdb) print(x) |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1862 | 12 |
| 1863 | (Pdb) continue |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1864 | |
| 1865 | """ |
| 1866 | |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 1867 | if not hasattr(sys, 'gettrace') or not sys.gettrace(): |
| 1868 | def test_pdb_set_trace(): |
| 1869 | """Using pdb.set_trace from a doctest. |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1870 | |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 1871 | You can use pdb.set_trace from a doctest. To do so, you must |
| 1872 | retrieve the set_trace function from the pdb module at the time |
| 1873 | you use it. The doctest module changes sys.stdout so that it can |
| 1874 | capture program output. It also temporarily replaces pdb.set_trace |
| 1875 | with a version that restores stdout. This is necessary for you to |
| 1876 | see debugger output. |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1877 | |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 1878 | >>> doc = ''' |
| 1879 | ... >>> x = 42 |
| 1880 | ... >>> raise Exception('clé') |
| 1881 | ... Traceback (most recent call last): |
| 1882 | ... Exception: clé |
| 1883 | ... >>> import pdb; pdb.set_trace() |
| 1884 | ... ''' |
| 1885 | >>> parser = doctest.DocTestParser() |
| 1886 | >>> test = parser.get_doctest(doc, {}, "foo-bar@baz", "foo-bar@baz.py", 0) |
| 1887 | >>> runner = doctest.DocTestRunner(verbose=False) |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1888 | |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 1889 | To demonstrate this, we'll create a fake standard input that |
| 1890 | captures our debugger input: |
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 | >>> real_stdin = sys.stdin |
| 1893 | >>> sys.stdin = _FakeInput([ |
| 1894 | ... 'print(x)', # print data defined by the example |
| 1895 | ... 'continue', # stop debugging |
| 1896 | ... '']) |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1897 | |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 1898 | >>> try: runner.run(test) |
| 1899 | ... finally: sys.stdin = real_stdin |
| 1900 | --Return-- |
| 1901 | > <doctest foo-bar@baz[2]>(1)<module>()->None |
| 1902 | -> import pdb; pdb.set_trace() |
| 1903 | (Pdb) print(x) |
| 1904 | 42 |
| 1905 | (Pdb) continue |
| 1906 | TestResults(failed=0, attempted=3) |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1907 | |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 1908 | 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] | 1909 | |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 1910 | >>> def calls_set_trace(): |
| 1911 | ... y=2 |
| 1912 | ... import pdb; pdb.set_trace() |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1913 | |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 1914 | >>> doc = ''' |
| 1915 | ... >>> x=1 |
| 1916 | ... >>> calls_set_trace() |
| 1917 | ... ''' |
| 1918 | >>> test = parser.get_doctest(doc, globals(), "foo-bar@baz", "foo-bar@baz.py", 0) |
| 1919 | >>> real_stdin = sys.stdin |
| 1920 | >>> sys.stdin = _FakeInput([ |
| 1921 | ... 'print(y)', # print data defined in the function |
| 1922 | ... 'up', # out of function |
| 1923 | ... 'print(x)', # print data defined by the example |
| 1924 | ... 'continue', # stop debugging |
| 1925 | ... '']) |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1926 | |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 1927 | >>> try: |
| 1928 | ... runner.run(test) |
| 1929 | ... finally: |
| 1930 | ... sys.stdin = real_stdin |
| 1931 | --Return-- |
Serhiy Storchaka | e437a10 | 2016-04-24 21:41:02 +0300 | [diff] [blame] | 1932 | > <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] | 1933 | -> import pdb; pdb.set_trace() |
| 1934 | (Pdb) print(y) |
| 1935 | 2 |
| 1936 | (Pdb) up |
| 1937 | > <doctest foo-bar@baz[1]>(1)<module>() |
| 1938 | -> calls_set_trace() |
| 1939 | (Pdb) print(x) |
| 1940 | 1 |
| 1941 | (Pdb) continue |
| 1942 | TestResults(failed=0, attempted=2) |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1943 | |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 1944 | During interactive debugging, source code is shown, even for |
| 1945 | doctest examples: |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1946 | |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 1947 | >>> doc = ''' |
| 1948 | ... >>> def f(x): |
| 1949 | ... ... g(x*2) |
| 1950 | ... >>> def g(x): |
| 1951 | ... ... print(x+3) |
| 1952 | ... ... import pdb; pdb.set_trace() |
| 1953 | ... >>> f(3) |
| 1954 | ... ''' |
| 1955 | >>> test = parser.get_doctest(doc, globals(), "foo-bar@baz", "foo-bar@baz.py", 0) |
| 1956 | >>> real_stdin = sys.stdin |
| 1957 | >>> sys.stdin = _FakeInput([ |
| 1958 | ... 'list', # list source from example 2 |
| 1959 | ... 'next', # return from g() |
| 1960 | ... 'list', # list source from example 1 |
| 1961 | ... 'next', # return from f() |
| 1962 | ... 'list', # list source from example 3 |
| 1963 | ... 'continue', # stop debugging |
| 1964 | ... '']) |
| 1965 | >>> try: runner.run(test) |
| 1966 | ... finally: sys.stdin = real_stdin |
| 1967 | ... # doctest: +NORMALIZE_WHITESPACE |
| 1968 | --Return-- |
| 1969 | > <doctest foo-bar@baz[1]>(3)g()->None |
| 1970 | -> import pdb; pdb.set_trace() |
| 1971 | (Pdb) list |
| 1972 | 1 def g(x): |
| 1973 | 2 print(x+3) |
| 1974 | 3 -> import pdb; pdb.set_trace() |
| 1975 | [EOF] |
| 1976 | (Pdb) next |
| 1977 | --Return-- |
| 1978 | > <doctest foo-bar@baz[0]>(2)f()->None |
| 1979 | -> g(x*2) |
| 1980 | (Pdb) list |
| 1981 | 1 def f(x): |
| 1982 | 2 -> g(x*2) |
| 1983 | [EOF] |
| 1984 | (Pdb) next |
| 1985 | --Return-- |
| 1986 | > <doctest foo-bar@baz[2]>(1)<module>()->None |
| 1987 | -> f(3) |
| 1988 | (Pdb) list |
| 1989 | 1 -> f(3) |
| 1990 | [EOF] |
| 1991 | (Pdb) continue |
| 1992 | ********************************************************************** |
| 1993 | File "foo-bar@baz.py", line 7, in foo-bar@baz |
| 1994 | Failed example: |
| 1995 | f(3) |
| 1996 | Expected nothing |
| 1997 | Got: |
| 1998 | 9 |
| 1999 | TestResults(failed=1, attempted=3) |
| 2000 | """ |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 2001 | |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 2002 | def test_pdb_set_trace_nested(): |
| 2003 | """This illustrates more-demanding use of set_trace with nested functions. |
Tim Peters | 50c6bdb | 2004-11-08 22:07:37 +0000 | [diff] [blame] | 2004 | |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 2005 | >>> class C(object): |
| 2006 | ... def calls_set_trace(self): |
| 2007 | ... y = 1 |
| 2008 | ... import pdb; pdb.set_trace() |
| 2009 | ... self.f1() |
| 2010 | ... y = 2 |
| 2011 | ... def f1(self): |
| 2012 | ... x = 1 |
| 2013 | ... self.f2() |
| 2014 | ... x = 2 |
| 2015 | ... def f2(self): |
| 2016 | ... z = 1 |
| 2017 | ... z = 2 |
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 | >>> calls_set_trace = C().calls_set_trace |
Tim Peters | 50c6bdb | 2004-11-08 22:07:37 +0000 | [diff] [blame] | 2020 | |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 2021 | >>> doc = ''' |
| 2022 | ... >>> a = 1 |
| 2023 | ... >>> calls_set_trace() |
| 2024 | ... ''' |
| 2025 | >>> parser = doctest.DocTestParser() |
| 2026 | >>> runner = doctest.DocTestRunner(verbose=False) |
| 2027 | >>> test = parser.get_doctest(doc, globals(), "foo-bar@baz", "foo-bar@baz.py", 0) |
| 2028 | >>> real_stdin = sys.stdin |
| 2029 | >>> sys.stdin = _FakeInput([ |
| 2030 | ... 'print(y)', # print data defined in the function |
| 2031 | ... 'step', 'step', 'step', 'step', 'step', 'step', 'print(z)', |
| 2032 | ... 'up', 'print(x)', |
| 2033 | ... 'up', 'print(y)', |
| 2034 | ... 'up', 'print(foo)', |
| 2035 | ... 'continue', # stop debugging |
| 2036 | ... '']) |
Tim Peters | 50c6bdb | 2004-11-08 22:07:37 +0000 | [diff] [blame] | 2037 | |
Brett Cannon | 31f5929 | 2011-02-21 19:29:56 +0000 | [diff] [blame] | 2038 | >>> try: |
| 2039 | ... runner.run(test) |
| 2040 | ... finally: |
| 2041 | ... sys.stdin = real_stdin |
| 2042 | ... # doctest: +REPORT_NDIFF |
| 2043 | > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(5)calls_set_trace() |
| 2044 | -> self.f1() |
| 2045 | (Pdb) print(y) |
| 2046 | 1 |
| 2047 | (Pdb) step |
| 2048 | --Call-- |
| 2049 | > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(7)f1() |
| 2050 | -> def f1(self): |
| 2051 | (Pdb) step |
| 2052 | > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(8)f1() |
| 2053 | -> x = 1 |
| 2054 | (Pdb) step |
| 2055 | > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(9)f1() |
| 2056 | -> self.f2() |
| 2057 | (Pdb) step |
| 2058 | --Call-- |
| 2059 | > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(11)f2() |
| 2060 | -> def f2(self): |
| 2061 | (Pdb) step |
| 2062 | > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(12)f2() |
| 2063 | -> z = 1 |
| 2064 | (Pdb) step |
| 2065 | > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(13)f2() |
| 2066 | -> z = 2 |
| 2067 | (Pdb) print(z) |
| 2068 | 1 |
| 2069 | (Pdb) up |
| 2070 | > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(9)f1() |
| 2071 | -> self.f2() |
| 2072 | (Pdb) print(x) |
| 2073 | 1 |
| 2074 | (Pdb) up |
| 2075 | > <doctest test.test_doctest.test_pdb_set_trace_nested[0]>(5)calls_set_trace() |
| 2076 | -> self.f1() |
| 2077 | (Pdb) print(y) |
| 2078 | 1 |
| 2079 | (Pdb) up |
| 2080 | > <doctest foo-bar@baz[1]>(1)<module>() |
| 2081 | -> calls_set_trace() |
| 2082 | (Pdb) print(foo) |
| 2083 | *** NameError: name 'foo' is not defined |
| 2084 | (Pdb) continue |
| 2085 | TestResults(failed=0, attempted=2) |
| 2086 | """ |
Tim Peters | 50c6bdb | 2004-11-08 22:07:37 +0000 | [diff] [blame] | 2087 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2088 | def test_DocTestSuite(): |
Tim Peters | 1e277ee | 2004-08-07 05:37:52 +0000 | [diff] [blame] | 2089 | """DocTestSuite creates a unittest test suite from a doctest. |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2090 | |
| 2091 | We create a Suite by providing a module. A module can be provided |
| 2092 | by passing a module object: |
| 2093 | |
| 2094 | >>> import unittest |
| 2095 | >>> import test.sample_doctest |
| 2096 | >>> suite = doctest.DocTestSuite(test.sample_doctest) |
| 2097 | >>> suite.run(unittest.TestResult()) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 2098 | <unittest.result.TestResult run=9 errors=0 failures=4> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2099 | |
| 2100 | We can also supply the module by name: |
| 2101 | |
| 2102 | >>> suite = doctest.DocTestSuite('test.sample_doctest') |
| 2103 | >>> suite.run(unittest.TestResult()) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 2104 | <unittest.result.TestResult run=9 errors=0 failures=4> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2105 | |
R David Murray | 5abd76a | 2012-09-10 10:15:58 -0400 | [diff] [blame] | 2106 | The module need not contain any doctest examples: |
| 2107 | |
| 2108 | >>> suite = doctest.DocTestSuite('test.sample_doctest_no_doctests') |
| 2109 | >>> suite.run(unittest.TestResult()) |
| 2110 | <unittest.result.TestResult run=0 errors=0 failures=0> |
| 2111 | |
R David Murray | 1976d9b | 2014-04-14 20:28:36 -0400 | [diff] [blame] | 2112 | The module need not contain any docstrings either: |
R David Murray | 5abd76a | 2012-09-10 10:15:58 -0400 | [diff] [blame] | 2113 | |
R David Murray | 1976d9b | 2014-04-14 20:28:36 -0400 | [diff] [blame] | 2114 | >>> suite = doctest.DocTestSuite('test.sample_doctest_no_docstrings') |
R David Murray | 5abd76a | 2012-09-10 10:15:58 -0400 | [diff] [blame] | 2115 | >>> suite.run(unittest.TestResult()) |
| 2116 | <unittest.result.TestResult run=0 errors=0 failures=0> |
| 2117 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2118 | We can use the current module: |
| 2119 | |
| 2120 | >>> suite = test.sample_doctest.test_suite() |
| 2121 | >>> suite.run(unittest.TestResult()) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 2122 | <unittest.result.TestResult run=9 errors=0 failures=4> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2123 | |
R David Murray | 1976d9b | 2014-04-14 20:28:36 -0400 | [diff] [blame] | 2124 | We can also provide a DocTestFinder: |
| 2125 | |
| 2126 | >>> finder = doctest.DocTestFinder() |
| 2127 | >>> suite = doctest.DocTestSuite('test.sample_doctest', |
| 2128 | ... test_finder=finder) |
| 2129 | >>> suite.run(unittest.TestResult()) |
| 2130 | <unittest.result.TestResult run=9 errors=0 failures=4> |
| 2131 | |
| 2132 | The DocTestFinder need not return any tests: |
| 2133 | |
| 2134 | >>> finder = doctest.DocTestFinder() |
| 2135 | >>> suite = doctest.DocTestSuite('test.sample_doctest_no_docstrings', |
| 2136 | ... test_finder=finder) |
| 2137 | >>> suite.run(unittest.TestResult()) |
| 2138 | <unittest.result.TestResult run=0 errors=0 failures=0> |
| 2139 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2140 | We can supply global variables. If we pass globs, they will be |
| 2141 | used instead of the module globals. Here we'll pass an empty |
| 2142 | globals, triggering an extra error: |
| 2143 | |
| 2144 | >>> suite = doctest.DocTestSuite('test.sample_doctest', globs={}) |
| 2145 | >>> suite.run(unittest.TestResult()) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 2146 | <unittest.result.TestResult run=9 errors=0 failures=5> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2147 | |
| 2148 | Alternatively, we can provide extra globals. Here we'll make an |
| 2149 | error go away by providing an extra global variable: |
| 2150 | |
| 2151 | >>> suite = doctest.DocTestSuite('test.sample_doctest', |
| 2152 | ... extraglobs={'y': 1}) |
| 2153 | >>> suite.run(unittest.TestResult()) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 2154 | <unittest.result.TestResult run=9 errors=0 failures=3> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2155 | |
| 2156 | You can pass option flags. Here we'll cause an extra error |
| 2157 | by disabling the blank-line feature: |
| 2158 | |
| 2159 | >>> suite = doctest.DocTestSuite('test.sample_doctest', |
Tim Peters | 1e277ee | 2004-08-07 05:37:52 +0000 | [diff] [blame] | 2160 | ... optionflags=doctest.DONT_ACCEPT_BLANKLINE) |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2161 | >>> suite.run(unittest.TestResult()) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 2162 | <unittest.result.TestResult run=9 errors=0 failures=5> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2163 | |
Tim Peters | 1e277ee | 2004-08-07 05:37:52 +0000 | [diff] [blame] | 2164 | You can supply setUp and tearDown functions: |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2165 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2166 | >>> def setUp(t): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2167 | ... import test.test_doctest |
| 2168 | ... test.test_doctest.sillySetup = True |
| 2169 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2170 | >>> def tearDown(t): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2171 | ... import test.test_doctest |
| 2172 | ... del test.test_doctest.sillySetup |
| 2173 | |
| 2174 | Here, we installed a silly variable that the test expects: |
| 2175 | |
| 2176 | >>> suite = doctest.DocTestSuite('test.sample_doctest', |
| 2177 | ... setUp=setUp, tearDown=tearDown) |
| 2178 | >>> suite.run(unittest.TestResult()) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 2179 | <unittest.result.TestResult run=9 errors=0 failures=3> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2180 | |
| 2181 | But the tearDown restores sanity: |
| 2182 | |
| 2183 | >>> import test.test_doctest |
| 2184 | >>> test.test_doctest.sillySetup |
| 2185 | Traceback (most recent call last): |
| 2186 | ... |
Ethan Furman | 7b9ff0e | 2014-04-24 14:47:47 -0700 | [diff] [blame] | 2187 | AttributeError: module 'test.test_doctest' has no attribute 'sillySetup' |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2188 | |
Berker Peksag | 4882cac | 2015-04-14 09:30:01 +0300 | [diff] [blame] | 2189 | The setUp and tearDown functions are passed test objects. Here |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2190 | we'll use the setUp function to supply the missing variable y: |
| 2191 | |
| 2192 | >>> def setUp(test): |
| 2193 | ... test.globs['y'] = 1 |
| 2194 | |
| 2195 | >>> suite = doctest.DocTestSuite('test.sample_doctest', setUp=setUp) |
| 2196 | >>> suite.run(unittest.TestResult()) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 2197 | <unittest.result.TestResult run=9 errors=0 failures=3> |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2198 | |
| 2199 | Here, we didn't need to use a tearDown function because we |
| 2200 | modified the test globals, which are a copy of the |
| 2201 | sample_doctest module dictionary. The test globals are |
| 2202 | automatically cleared for us after a test. |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2203 | """ |
| 2204 | |
| 2205 | def test_DocFileSuite(): |
| 2206 | """We can test tests found in text files using a DocFileSuite. |
| 2207 | |
| 2208 | We create a suite by providing the names of one or more text |
| 2209 | files that include examples: |
| 2210 | |
| 2211 | >>> import unittest |
| 2212 | >>> suite = doctest.DocFileSuite('test_doctest.txt', |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2213 | ... 'test_doctest2.txt', |
| 2214 | ... 'test_doctest4.txt') |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2215 | >>> suite.run(unittest.TestResult()) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 2216 | <unittest.result.TestResult run=3 errors=0 failures=2> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2217 | |
| 2218 | The test files are looked for in the directory containing the |
| 2219 | calling module. A package keyword argument can be provided to |
| 2220 | specify a different relative location. |
| 2221 | |
| 2222 | >>> import unittest |
| 2223 | >>> suite = doctest.DocFileSuite('test_doctest.txt', |
| 2224 | ... 'test_doctest2.txt', |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2225 | ... 'test_doctest4.txt', |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2226 | ... package='test') |
| 2227 | >>> suite.run(unittest.TestResult()) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 2228 | <unittest.result.TestResult run=3 errors=0 failures=2> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2229 | |
Guido van Rossum | cd4d452 | 2007-11-22 00:30:02 +0000 | [diff] [blame] | 2230 | Support for using a package's __loader__.get_data() is also |
| 2231 | provided. |
| 2232 | |
| 2233 | >>> import unittest, pkgutil, test |
Guido van Rossum | 0d3fb8a | 2007-11-26 23:23:18 +0000 | [diff] [blame] | 2234 | >>> added_loader = False |
Guido van Rossum | cd4d452 | 2007-11-22 00:30:02 +0000 | [diff] [blame] | 2235 | >>> if not hasattr(test, '__loader__'): |
| 2236 | ... test.__loader__ = pkgutil.get_loader(test) |
| 2237 | ... added_loader = True |
| 2238 | >>> try: |
| 2239 | ... suite = doctest.DocFileSuite('test_doctest.txt', |
| 2240 | ... 'test_doctest2.txt', |
| 2241 | ... 'test_doctest4.txt', |
| 2242 | ... package='test') |
| 2243 | ... suite.run(unittest.TestResult()) |
| 2244 | ... finally: |
| 2245 | ... if added_loader: |
| 2246 | ... del test.__loader__ |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 2247 | <unittest.result.TestResult run=3 errors=0 failures=2> |
Guido van Rossum | cd4d452 | 2007-11-22 00:30:02 +0000 | [diff] [blame] | 2248 | |
Edward Loper | 0273f5b | 2004-09-18 20:27:04 +0000 | [diff] [blame] | 2249 | '/' should be used as a path separator. It will be converted |
| 2250 | to a native separator at run time: |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2251 | |
| 2252 | >>> suite = doctest.DocFileSuite('../test/test_doctest.txt') |
| 2253 | >>> suite.run(unittest.TestResult()) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 2254 | <unittest.result.TestResult run=1 errors=0 failures=1> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2255 | |
Edward Loper | 0273f5b | 2004-09-18 20:27:04 +0000 | [diff] [blame] | 2256 | If DocFileSuite is used from an interactive session, then files |
| 2257 | are resolved relative to the directory of sys.argv[0]: |
| 2258 | |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 2259 | >>> import types, os.path, test.test_doctest |
Edward Loper | 0273f5b | 2004-09-18 20:27:04 +0000 | [diff] [blame] | 2260 | >>> save_argv = sys.argv |
| 2261 | >>> sys.argv = [test.test_doctest.__file__] |
| 2262 | >>> suite = doctest.DocFileSuite('test_doctest.txt', |
Christian Heimes | 45f9af3 | 2007-11-27 21:50:00 +0000 | [diff] [blame] | 2263 | ... package=types.ModuleType('__main__')) |
Edward Loper | 0273f5b | 2004-09-18 20:27:04 +0000 | [diff] [blame] | 2264 | >>> sys.argv = save_argv |
| 2265 | |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2266 | By setting `module_relative=False`, os-specific paths may be |
| 2267 | used (including absolute paths and paths relative to the |
| 2268 | working directory): |
Edward Loper | 0273f5b | 2004-09-18 20:27:04 +0000 | [diff] [blame] | 2269 | |
| 2270 | >>> # Get the absolute path of the test package. |
| 2271 | >>> test_doctest_path = os.path.abspath(test.test_doctest.__file__) |
| 2272 | >>> test_pkg_path = os.path.split(test_doctest_path)[0] |
| 2273 | |
| 2274 | >>> # Use it to find the absolute path of test_doctest.txt. |
| 2275 | >>> test_file = os.path.join(test_pkg_path, 'test_doctest.txt') |
| 2276 | |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2277 | >>> suite = doctest.DocFileSuite(test_file, module_relative=False) |
Edward Loper | 0273f5b | 2004-09-18 20:27:04 +0000 | [diff] [blame] | 2278 | >>> suite.run(unittest.TestResult()) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 2279 | <unittest.result.TestResult run=1 errors=0 failures=1> |
Edward Loper | 0273f5b | 2004-09-18 20:27:04 +0000 | [diff] [blame] | 2280 | |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2281 | It is an error to specify `package` when `module_relative=False`: |
| 2282 | |
| 2283 | >>> suite = doctest.DocFileSuite(test_file, module_relative=False, |
| 2284 | ... package='test') |
| 2285 | Traceback (most recent call last): |
| 2286 | ValueError: Package may only be specified for module-relative paths. |
| 2287 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2288 | You can specify initial global variables: |
| 2289 | |
| 2290 | >>> suite = doctest.DocFileSuite('test_doctest.txt', |
| 2291 | ... 'test_doctest2.txt', |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2292 | ... 'test_doctest4.txt', |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2293 | ... globs={'favorite_color': 'blue'}) |
| 2294 | >>> suite.run(unittest.TestResult()) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 2295 | <unittest.result.TestResult run=3 errors=0 failures=1> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2296 | |
| 2297 | In this case, we supplied a missing favorite color. You can |
| 2298 | provide doctest options: |
| 2299 | |
| 2300 | >>> suite = doctest.DocFileSuite('test_doctest.txt', |
| 2301 | ... 'test_doctest2.txt', |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2302 | ... 'test_doctest4.txt', |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2303 | ... optionflags=doctest.DONT_ACCEPT_BLANKLINE, |
| 2304 | ... globs={'favorite_color': 'blue'}) |
| 2305 | >>> suite.run(unittest.TestResult()) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 2306 | <unittest.result.TestResult run=3 errors=0 failures=2> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2307 | |
| 2308 | And, you can provide setUp and tearDown functions: |
| 2309 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2310 | >>> def setUp(t): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2311 | ... import test.test_doctest |
| 2312 | ... test.test_doctest.sillySetup = True |
| 2313 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2314 | >>> def tearDown(t): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2315 | ... import test.test_doctest |
| 2316 | ... del test.test_doctest.sillySetup |
| 2317 | |
| 2318 | Here, we installed a silly variable that the test expects: |
| 2319 | |
| 2320 | >>> suite = doctest.DocFileSuite('test_doctest.txt', |
| 2321 | ... 'test_doctest2.txt', |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2322 | ... 'test_doctest4.txt', |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2323 | ... setUp=setUp, tearDown=tearDown) |
| 2324 | >>> suite.run(unittest.TestResult()) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 2325 | <unittest.result.TestResult run=3 errors=0 failures=1> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2326 | |
| 2327 | But the tearDown restores sanity: |
| 2328 | |
| 2329 | >>> import test.test_doctest |
| 2330 | >>> test.test_doctest.sillySetup |
| 2331 | Traceback (most recent call last): |
| 2332 | ... |
Ethan Furman | 7b9ff0e | 2014-04-24 14:47:47 -0700 | [diff] [blame] | 2333 | AttributeError: module 'test.test_doctest' has no attribute 'sillySetup' |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2334 | |
Berker Peksag | 4882cac | 2015-04-14 09:30:01 +0300 | [diff] [blame] | 2335 | The setUp and tearDown functions are passed test objects. |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2336 | Here, we'll use a setUp function to set the favorite color in |
| 2337 | test_doctest.txt: |
| 2338 | |
| 2339 | >>> def setUp(test): |
| 2340 | ... test.globs['favorite_color'] = 'blue' |
| 2341 | |
| 2342 | >>> suite = doctest.DocFileSuite('test_doctest.txt', setUp=setUp) |
| 2343 | >>> suite.run(unittest.TestResult()) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 2344 | <unittest.result.TestResult run=1 errors=0 failures=0> |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2345 | |
| 2346 | Here, we didn't need to use a tearDown function because we |
| 2347 | modified the test globals. The test globals are |
| 2348 | automatically cleared for us after a test. |
Tim Peters | df7a208 | 2004-08-29 00:38:17 +0000 | [diff] [blame] | 2349 | |
Fred Drake | 7c404a4 | 2004-12-21 23:46:34 +0000 | [diff] [blame] | 2350 | Tests in a file run using `DocFileSuite` can also access the |
| 2351 | `__file__` global, which is set to the name of the file |
| 2352 | containing the tests: |
| 2353 | |
Benjamin Peterson | ab078e9 | 2016-07-13 21:13:29 -0700 | [diff] [blame] | 2354 | >>> suite = doctest.DocFileSuite('test_doctest3.txt') |
Fred Drake | 7c404a4 | 2004-12-21 23:46:34 +0000 | [diff] [blame] | 2355 | >>> suite.run(unittest.TestResult()) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 2356 | <unittest.result.TestResult run=1 errors=0 failures=0> |
Fred Drake | 7c404a4 | 2004-12-21 23:46:34 +0000 | [diff] [blame] | 2357 | |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2358 | If the tests contain non-ASCII characters, we have to specify which |
| 2359 | encoding the file is encoded with. We do so by using the `encoding` |
| 2360 | parameter: |
| 2361 | |
| 2362 | >>> suite = doctest.DocFileSuite('test_doctest.txt', |
| 2363 | ... 'test_doctest2.txt', |
| 2364 | ... 'test_doctest4.txt', |
| 2365 | ... encoding='utf-8') |
| 2366 | >>> suite.run(unittest.TestResult()) |
Benjamin Peterson | bed7d04 | 2009-07-19 21:01:52 +0000 | [diff] [blame] | 2367 | <unittest.result.TestResult run=3 errors=0 failures=2> |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2368 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2369 | """ |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2370 | |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 2371 | def test_trailing_space_in_test(): |
| 2372 | """ |
Tim Peters | a7def72 | 2004-08-23 22:13:22 +0000 | [diff] [blame] | 2373 | Trailing spaces in expected output are significant: |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 2374 | |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 2375 | >>> x, y = 'foo', '' |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 2376 | >>> print(x, y) |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 2377 | foo \n |
| 2378 | """ |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2379 | |
Yury Selivanov | b532df6 | 2014-12-08 15:00:05 -0500 | [diff] [blame] | 2380 | class Wrapper: |
| 2381 | def __init__(self, func): |
| 2382 | self.func = func |
| 2383 | functools.update_wrapper(self, func) |
| 2384 | |
| 2385 | def __call__(self, *args, **kwargs): |
| 2386 | self.func(*args, **kwargs) |
| 2387 | |
| 2388 | @Wrapper |
| 2389 | def test_look_in_unwrapped(): |
| 2390 | """ |
| 2391 | Docstrings in wrapped functions must be detected as well. |
| 2392 | |
| 2393 | >>> 'one other test' |
| 2394 | 'one other test' |
| 2395 | """ |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2396 | |
| 2397 | def test_unittest_reportflags(): |
| 2398 | """Default unittest reporting flags can be set to control reporting |
| 2399 | |
| 2400 | Here, we'll set the REPORT_ONLY_FIRST_FAILURE option so we see |
| 2401 | only the first failure of each test. First, we'll look at the |
| 2402 | output without the flag. The file test_doctest.txt file has two |
| 2403 | tests. They both fail if blank lines are disabled: |
| 2404 | |
| 2405 | >>> suite = doctest.DocFileSuite('test_doctest.txt', |
| 2406 | ... optionflags=doctest.DONT_ACCEPT_BLANKLINE) |
| 2407 | >>> import unittest |
| 2408 | >>> result = suite.run(unittest.TestResult()) |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 2409 | >>> print(result.failures[0][1]) # doctest: +ELLIPSIS |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2410 | Traceback ... |
| 2411 | Failed example: |
| 2412 | favorite_color |
| 2413 | ... |
| 2414 | Failed example: |
| 2415 | if 1: |
| 2416 | ... |
| 2417 | |
| 2418 | Note that we see both failures displayed. |
| 2419 | |
| 2420 | >>> old = doctest.set_unittest_reportflags( |
| 2421 | ... doctest.REPORT_ONLY_FIRST_FAILURE) |
| 2422 | |
| 2423 | Now, when we run the test: |
| 2424 | |
| 2425 | >>> result = suite.run(unittest.TestResult()) |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 2426 | >>> print(result.failures[0][1]) # doctest: +ELLIPSIS |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2427 | Traceback ... |
| 2428 | Failed example: |
| 2429 | favorite_color |
| 2430 | Exception raised: |
| 2431 | ... |
| 2432 | NameError: name 'favorite_color' is not defined |
| 2433 | <BLANKLINE> |
| 2434 | <BLANKLINE> |
Tim Peters | df7a208 | 2004-08-29 00:38:17 +0000 | [diff] [blame] | 2435 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2436 | We get only the first failure. |
| 2437 | |
| 2438 | If we give any reporting options when we set up the tests, |
| 2439 | however: |
| 2440 | |
| 2441 | >>> suite = doctest.DocFileSuite('test_doctest.txt', |
| 2442 | ... optionflags=doctest.DONT_ACCEPT_BLANKLINE | doctest.REPORT_NDIFF) |
| 2443 | |
| 2444 | Then the default eporting options are ignored: |
| 2445 | |
| 2446 | >>> result = suite.run(unittest.TestResult()) |
Guido van Rossum | 7131f84 | 2007-02-09 20:13:25 +0000 | [diff] [blame] | 2447 | >>> print(result.failures[0][1]) # doctest: +ELLIPSIS |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2448 | Traceback ... |
| 2449 | Failed example: |
| 2450 | favorite_color |
| 2451 | ... |
| 2452 | Failed example: |
| 2453 | if 1: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 2454 | print('a') |
| 2455 | print() |
| 2456 | print('b') |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 2457 | Differences (ndiff with -expected +actual): |
| 2458 | a |
| 2459 | - <BLANKLINE> |
| 2460 | + |
| 2461 | b |
| 2462 | <BLANKLINE> |
| 2463 | <BLANKLINE> |
| 2464 | |
| 2465 | |
| 2466 | Test runners can restore the formatting flags after they run: |
| 2467 | |
| 2468 | >>> ignored = doctest.set_unittest_reportflags(old) |
| 2469 | |
| 2470 | """ |
| 2471 | |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2472 | def test_testfile(): r""" |
| 2473 | Tests for the `testfile()` function. This function runs all the |
| 2474 | doctest examples in a given file. In its simple invokation, it is |
| 2475 | called with the name of a file, which is taken to be relative to the |
| 2476 | calling module. The return value is (#failures, #tests). |
| 2477 | |
Florent Xicluna | 5925085 | 2010-02-27 14:21:57 +0000 | [diff] [blame] | 2478 | We don't want `-v` in sys.argv for these tests. |
| 2479 | |
| 2480 | >>> save_argv = sys.argv |
| 2481 | >>> if '-v' in sys.argv: |
| 2482 | ... sys.argv = [arg for arg in save_argv if arg != '-v'] |
| 2483 | |
| 2484 | |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2485 | >>> doctest.testfile('test_doctest.txt') # doctest: +ELLIPSIS |
| 2486 | ********************************************************************** |
| 2487 | File "...", line 6, in test_doctest.txt |
| 2488 | Failed example: |
| 2489 | favorite_color |
| 2490 | Exception raised: |
| 2491 | ... |
| 2492 | NameError: name 'favorite_color' is not defined |
| 2493 | ********************************************************************** |
| 2494 | 1 items had failures: |
| 2495 | 1 of 2 in test_doctest.txt |
| 2496 | ***Test Failed*** 1 failures. |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 2497 | TestResults(failed=1, attempted=2) |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2498 | >>> doctest.master = None # Reset master. |
| 2499 | |
| 2500 | (Note: we'll be clearing doctest.master after each call to |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 2501 | `doctest.testfile`, to suppress warnings about multiple tests with the |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2502 | same name.) |
| 2503 | |
| 2504 | Globals may be specified with the `globs` and `extraglobs` parameters: |
| 2505 | |
| 2506 | >>> globs = {'favorite_color': 'blue'} |
| 2507 | >>> doctest.testfile('test_doctest.txt', globs=globs) |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 2508 | TestResults(failed=0, attempted=2) |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2509 | >>> doctest.master = None # Reset master. |
| 2510 | |
| 2511 | >>> extraglobs = {'favorite_color': 'red'} |
| 2512 | >>> doctest.testfile('test_doctest.txt', globs=globs, |
| 2513 | ... extraglobs=extraglobs) # doctest: +ELLIPSIS |
| 2514 | ********************************************************************** |
| 2515 | File "...", line 6, in test_doctest.txt |
| 2516 | Failed example: |
| 2517 | favorite_color |
| 2518 | Expected: |
| 2519 | 'blue' |
| 2520 | Got: |
| 2521 | 'red' |
| 2522 | ********************************************************************** |
| 2523 | 1 items had failures: |
| 2524 | 1 of 2 in test_doctest.txt |
| 2525 | ***Test Failed*** 1 failures. |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 2526 | TestResults(failed=1, attempted=2) |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2527 | >>> doctest.master = None # Reset master. |
| 2528 | |
| 2529 | The file may be made relative to a given module or package, using the |
| 2530 | optional `module_relative` parameter: |
| 2531 | |
| 2532 | >>> doctest.testfile('test_doctest.txt', globs=globs, |
| 2533 | ... module_relative='test') |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 2534 | TestResults(failed=0, attempted=2) |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2535 | >>> doctest.master = None # Reset master. |
| 2536 | |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 2537 | Verbosity can be increased with the optional `verbose` parameter: |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2538 | |
| 2539 | >>> doctest.testfile('test_doctest.txt', globs=globs, verbose=True) |
| 2540 | Trying: |
| 2541 | favorite_color |
| 2542 | Expecting: |
| 2543 | 'blue' |
| 2544 | ok |
| 2545 | Trying: |
| 2546 | if 1: |
Guido van Rossum | bdc36e4 | 2007-02-09 22:43:47 +0000 | [diff] [blame] | 2547 | print('a') |
| 2548 | print() |
| 2549 | print('b') |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2550 | Expecting: |
| 2551 | a |
| 2552 | <BLANKLINE> |
| 2553 | b |
| 2554 | ok |
| 2555 | 1 items passed all tests: |
| 2556 | 2 tests in test_doctest.txt |
| 2557 | 2 tests in 1 items. |
| 2558 | 2 passed and 0 failed. |
| 2559 | Test passed. |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 2560 | TestResults(failed=0, attempted=2) |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2561 | >>> doctest.master = None # Reset master. |
| 2562 | |
| 2563 | The name of the test may be specified with the optional `name` |
| 2564 | parameter: |
| 2565 | |
| 2566 | >>> doctest.testfile('test_doctest.txt', name='newname') |
| 2567 | ... # doctest: +ELLIPSIS |
| 2568 | ********************************************************************** |
| 2569 | File "...", line 6, in newname |
| 2570 | ... |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 2571 | TestResults(failed=1, attempted=2) |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2572 | >>> doctest.master = None # Reset master. |
| 2573 | |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 2574 | The summary report may be suppressed with the optional `report` |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2575 | parameter: |
| 2576 | |
| 2577 | >>> doctest.testfile('test_doctest.txt', report=False) |
| 2578 | ... # doctest: +ELLIPSIS |
| 2579 | ********************************************************************** |
| 2580 | File "...", line 6, in test_doctest.txt |
| 2581 | Failed example: |
| 2582 | favorite_color |
| 2583 | Exception raised: |
| 2584 | ... |
| 2585 | NameError: name 'favorite_color' is not defined |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 2586 | TestResults(failed=1, attempted=2) |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2587 | >>> doctest.master = None # Reset master. |
| 2588 | |
| 2589 | The optional keyword argument `raise_on_error` can be used to raise an |
| 2590 | exception on the first error (which may be useful for postmortem |
| 2591 | debugging): |
| 2592 | |
| 2593 | >>> doctest.testfile('test_doctest.txt', raise_on_error=True) |
| 2594 | ... # doctest: +ELLIPSIS |
| 2595 | Traceback (most recent call last): |
Guido van Rossum | 6a2a2a0 | 2006-08-26 20:37:44 +0000 | [diff] [blame] | 2596 | doctest.UnexpectedException: ... |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2597 | >>> doctest.master = None # Reset master. |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2598 | |
| 2599 | If the tests contain non-ASCII characters, the tests might fail, since |
| 2600 | it's unknown which encoding is used. The encoding can be specified |
| 2601 | using the optional keyword argument `encoding`: |
| 2602 | |
Martin v. Löwis | b1a9f27 | 2007-07-20 07:13:39 +0000 | [diff] [blame] | 2603 | >>> doctest.testfile('test_doctest4.txt', encoding='latin-1') # doctest: +ELLIPSIS |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2604 | ********************************************************************** |
| 2605 | File "...", line 7, in test_doctest4.txt |
| 2606 | Failed example: |
Martin v. Löwis | b1a9f27 | 2007-07-20 07:13:39 +0000 | [diff] [blame] | 2607 | '...' |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2608 | Expected: |
Martin v. Löwis | b1a9f27 | 2007-07-20 07:13:39 +0000 | [diff] [blame] | 2609 | 'f\xf6\xf6' |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2610 | Got: |
Martin v. Löwis | b1a9f27 | 2007-07-20 07:13:39 +0000 | [diff] [blame] | 2611 | 'f\xc3\xb6\xc3\xb6' |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2612 | ********************************************************************** |
| 2613 | ... |
| 2614 | ********************************************************************** |
| 2615 | 1 items had failures: |
Martin v. Löwis | b1a9f27 | 2007-07-20 07:13:39 +0000 | [diff] [blame] | 2616 | 2 of 2 in test_doctest4.txt |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2617 | ***Test Failed*** 2 failures. |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 2618 | TestResults(failed=2, attempted=2) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2619 | >>> doctest.master = None # Reset master. |
| 2620 | |
| 2621 | >>> doctest.testfile('test_doctest4.txt', encoding='utf-8') |
Christian Heimes | 25bb783 | 2008-01-11 16:17:00 +0000 | [diff] [blame] | 2622 | TestResults(failed=0, attempted=2) |
Thomas Wouters | 4d70c3d | 2006-06-08 14:42:34 +0000 | [diff] [blame] | 2623 | >>> doctest.master = None # Reset master. |
Florent Xicluna | 5925085 | 2010-02-27 14:21:57 +0000 | [diff] [blame] | 2624 | |
| 2625 | Test the verbose output: |
| 2626 | |
| 2627 | >>> doctest.testfile('test_doctest4.txt', encoding='utf-8', verbose=True) |
| 2628 | Trying: |
| 2629 | 'föö' |
| 2630 | Expecting: |
| 2631 | 'f\xf6\xf6' |
| 2632 | ok |
| 2633 | Trying: |
| 2634 | 'bÄ…r' |
| 2635 | Expecting: |
| 2636 | 'b\u0105r' |
| 2637 | ok |
| 2638 | 1 items passed all tests: |
| 2639 | 2 tests in test_doctest4.txt |
| 2640 | 2 tests in 1 items. |
| 2641 | 2 passed and 0 failed. |
| 2642 | Test passed. |
| 2643 | TestResults(failed=0, attempted=2) |
| 2644 | >>> doctest.master = None # Reset master. |
| 2645 | >>> sys.argv = save_argv |
Edward Loper | 052d0cd | 2004-09-19 17:19:33 +0000 | [diff] [blame] | 2646 | """ |
| 2647 | |
R David Murray | b48cb29 | 2014-10-02 22:42:42 -0400 | [diff] [blame] | 2648 | def test_lineendings(): r""" |
| 2649 | *nix systems use \n line endings, while Windows systems use \r\n. Python |
| 2650 | handles this using universal newline mode for reading files. Let's make |
| 2651 | sure doctest does so (issue 8473) by creating temporary test files using each |
| 2652 | of the two line disciplines. One of the two will be the "wrong" one for the |
| 2653 | platform the test is run on. |
| 2654 | |
| 2655 | Windows line endings first: |
| 2656 | |
| 2657 | >>> import tempfile, os |
| 2658 | >>> fn = tempfile.mktemp() |
Serhiy Storchaka | c9ba38c | 2015-04-04 10:36:25 +0300 | [diff] [blame] | 2659 | >>> with open(fn, 'wb') as f: |
| 2660 | ... 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] | 2661 | 35 |
Victor Stinner | 09a08de | 2015-12-02 14:37:17 +0100 | [diff] [blame] | 2662 | >>> doctest.testfile(fn, module_relative=False, verbose=False) |
R David Murray | b48cb29 | 2014-10-02 22:42:42 -0400 | [diff] [blame] | 2663 | TestResults(failed=0, attempted=1) |
| 2664 | >>> os.remove(fn) |
| 2665 | |
| 2666 | And now *nix line endings: |
| 2667 | |
| 2668 | >>> fn = tempfile.mktemp() |
Serhiy Storchaka | c9ba38c | 2015-04-04 10:36:25 +0300 | [diff] [blame] | 2669 | >>> with open(fn, 'wb') as f: |
| 2670 | ... 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] | 2671 | 30 |
Victor Stinner | 09a08de | 2015-12-02 14:37:17 +0100 | [diff] [blame] | 2672 | >>> doctest.testfile(fn, module_relative=False, verbose=False) |
R David Murray | b48cb29 | 2014-10-02 22:42:42 -0400 | [diff] [blame] | 2673 | TestResults(failed=0, attempted=1) |
| 2674 | >>> os.remove(fn) |
| 2675 | |
| 2676 | """ |
| 2677 | |
R. David Murray | 58641de | 2009-06-12 15:33:19 +0000 | [diff] [blame] | 2678 | def test_testmod(): r""" |
| 2679 | Tests for the testmod function. More might be useful, but for now we're just |
| 2680 | testing the case raised by Issue 6195, where trying to doctest a C module would |
| 2681 | fail with a UnicodeDecodeError because doctest tried to read the "source" lines |
| 2682 | out of the binary module. |
| 2683 | |
| 2684 | >>> import unicodedata |
Florent Xicluna | 5925085 | 2010-02-27 14:21:57 +0000 | [diff] [blame] | 2685 | >>> doctest.testmod(unicodedata, verbose=False) |
R. David Murray | 58641de | 2009-06-12 15:33:19 +0000 | [diff] [blame] | 2686 | TestResults(failed=0, attempted=0) |
| 2687 | """ |
| 2688 | |
Victor Stinner | 9d39639 | 2010-10-16 21:54:59 +0000 | [diff] [blame] | 2689 | try: |
| 2690 | os.fsencode("foo-bär@baz.py") |
| 2691 | except UnicodeEncodeError: |
| 2692 | # Skip the test: the filesystem encoding is unable to encode the filename |
| 2693 | pass |
| 2694 | else: |
| 2695 | def test_unicode(): """ |
| 2696 | Check doctest with a non-ascii filename: |
| 2697 | |
| 2698 | >>> doc = ''' |
| 2699 | ... >>> raise Exception('clé') |
| 2700 | ... ''' |
| 2701 | ... |
| 2702 | >>> parser = doctest.DocTestParser() |
| 2703 | >>> test = parser.get_doctest(doc, {}, "foo-bär@baz", "foo-bär@baz.py", 0) |
| 2704 | >>> test |
| 2705 | <DocTest foo-bär@baz from foo-bär@baz.py:0 (1 example)> |
| 2706 | >>> runner = doctest.DocTestRunner(verbose=False) |
| 2707 | >>> runner.run(test) # doctest: +ELLIPSIS |
| 2708 | ********************************************************************** |
| 2709 | File "foo-bär@baz.py", line 2, in foo-bär@baz |
| 2710 | Failed example: |
| 2711 | raise Exception('clé') |
| 2712 | Exception raised: |
| 2713 | Traceback (most recent call last): |
| 2714 | File ... |
| 2715 | compileflags, 1), test.globs) |
| 2716 | File "<doctest foo-bär@baz[0]>", line 1, in <module> |
| 2717 | raise Exception('clé') |
| 2718 | Exception: clé |
| 2719 | TestResults(failed=1, attempted=1) |
| 2720 | """ |
| 2721 | |
R David Murray | 5707d50 | 2013-06-23 14:24:13 -0400 | [diff] [blame] | 2722 | def test_CLI(): r""" |
| 2723 | The doctest module can be used to run doctests against an arbitrary file. |
| 2724 | These tests test this CLI functionality. |
| 2725 | |
| 2726 | 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] | 2727 | to a temp dir to run the command against. Due to a current limitation in |
| 2728 | script_helpers, though, we need a little utility function to turn the returned |
| 2729 | output into something we can doctest against: |
R David Murray | 5707d50 | 2013-06-23 14:24:13 -0400 | [diff] [blame] | 2730 | |
R David Murray | 4af6898 | 2013-06-25 08:11:22 -0400 | [diff] [blame] | 2731 | >>> def normalize(s): |
| 2732 | ... return '\n'.join(s.decode().splitlines()) |
| 2733 | |
R David Murray | 4af6898 | 2013-06-25 08:11:22 -0400 | [diff] [blame] | 2734 | With those preliminaries out of the way, we'll start with a file with two |
| 2735 | simple tests and no errors. We'll run both the unadorned doctest command, and |
| 2736 | the verbose version, and then check the output: |
R David Murray | 5707d50 | 2013-06-23 14:24:13 -0400 | [diff] [blame] | 2737 | |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 2738 | >>> from test.support import script_helper, temp_dir |
| 2739 | >>> with temp_dir() as tmpdir: |
R David Murray | 5707d50 | 2013-06-23 14:24:13 -0400 | [diff] [blame] | 2740 | ... fn = os.path.join(tmpdir, 'myfile.doc') |
| 2741 | ... with open(fn, 'w') as f: |
| 2742 | ... _ = f.write('This is a very simple test file.\n') |
| 2743 | ... _ = f.write(' >>> 1 + 1\n') |
| 2744 | ... _ = f.write(' 2\n') |
| 2745 | ... _ = f.write(' >>> "a"\n') |
| 2746 | ... _ = f.write(" 'a'\n") |
| 2747 | ... _ = f.write('\n') |
| 2748 | ... _ = f.write('And that is it.\n') |
| 2749 | ... rc1, out1, err1 = script_helper.assert_python_ok( |
Berker Peksag | e495646 | 2016-06-24 09:28:50 +0300 | [diff] [blame] | 2750 | ... '-m', 'doctest', fn) |
R David Murray | 5707d50 | 2013-06-23 14:24:13 -0400 | [diff] [blame] | 2751 | ... rc2, out2, err2 = script_helper.assert_python_ok( |
Berker Peksag | e495646 | 2016-06-24 09:28:50 +0300 | [diff] [blame] | 2752 | ... '-m', 'doctest', '-v', fn) |
R David Murray | 5707d50 | 2013-06-23 14:24:13 -0400 | [diff] [blame] | 2753 | |
| 2754 | With no arguments and passing tests, we should get no output: |
| 2755 | |
| 2756 | >>> rc1, out1, err1 |
| 2757 | (0, b'', b'') |
| 2758 | |
| 2759 | With the verbose flag, we should see the test output, but no error output: |
| 2760 | |
| 2761 | >>> rc2, err2 |
| 2762 | (0, b'') |
R David Murray | 4af6898 | 2013-06-25 08:11:22 -0400 | [diff] [blame] | 2763 | >>> print(normalize(out2)) |
R David Murray | 5707d50 | 2013-06-23 14:24:13 -0400 | [diff] [blame] | 2764 | Trying: |
| 2765 | 1 + 1 |
| 2766 | Expecting: |
| 2767 | 2 |
| 2768 | ok |
| 2769 | Trying: |
| 2770 | "a" |
| 2771 | Expecting: |
| 2772 | 'a' |
| 2773 | ok |
| 2774 | 1 items passed all tests: |
| 2775 | 2 tests in myfile.doc |
| 2776 | 2 tests in 1 items. |
| 2777 | 2 passed and 0 failed. |
| 2778 | Test passed. |
R David Murray | 5707d50 | 2013-06-23 14:24:13 -0400 | [diff] [blame] | 2779 | |
| 2780 | Now we'll write a couple files, one with three tests, the other a python module |
| 2781 | with two tests, both of the files having "errors" in the tests that can be made |
| 2782 | non-errors by applying the appropriate doctest options to the run (ELLIPSIS in |
| 2783 | the first file, NORMALIZE_WHITESPACE in the second). This combination will |
Martin Panter | c04fb56 | 2016-02-10 05:44:01 +0000 | [diff] [blame] | 2784 | 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] | 2785 | ability to process more than one file on the command line and, since the second |
| 2786 | file ends in '.py', its handling of python module files (as opposed to straight |
| 2787 | text files). |
| 2788 | |
Berker Peksag | ce64391 | 2015-05-06 06:33:17 +0300 | [diff] [blame] | 2789 | >>> from test.support import script_helper, temp_dir |
| 2790 | >>> with temp_dir() as tmpdir: |
R David Murray | 5707d50 | 2013-06-23 14:24:13 -0400 | [diff] [blame] | 2791 | ... fn = os.path.join(tmpdir, 'myfile.doc') |
| 2792 | ... with open(fn, 'w') as f: |
| 2793 | ... _ = f.write('This is another simple test file.\n') |
| 2794 | ... _ = f.write(' >>> 1 + 1\n') |
| 2795 | ... _ = f.write(' 2\n') |
| 2796 | ... _ = f.write(' >>> "abcdef"\n') |
| 2797 | ... _ = f.write(" 'a...f'\n") |
| 2798 | ... _ = f.write(' >>> "ajkml"\n') |
| 2799 | ... _ = f.write(" 'a...l'\n") |
| 2800 | ... _ = f.write('\n') |
| 2801 | ... _ = f.write('And that is it.\n') |
| 2802 | ... fn2 = os.path.join(tmpdir, 'myfile2.py') |
| 2803 | ... with open(fn2, 'w') as f: |
| 2804 | ... _ = f.write('def test_func():\n') |
| 2805 | ... _ = f.write(' \"\"\"\n') |
| 2806 | ... _ = f.write(' This is simple python test function.\n') |
| 2807 | ... _ = f.write(' >>> 1 + 1\n') |
| 2808 | ... _ = f.write(' 2\n') |
| 2809 | ... _ = f.write(' >>> "abc def"\n') |
| 2810 | ... _ = f.write(" 'abc def'\n") |
| 2811 | ... _ = f.write("\n") |
| 2812 | ... _ = f.write(' \"\"\"\n') |
R David Murray | 5707d50 | 2013-06-23 14:24:13 -0400 | [diff] [blame] | 2813 | ... rc1, out1, err1 = script_helper.assert_python_failure( |
Berker Peksag | e495646 | 2016-06-24 09:28:50 +0300 | [diff] [blame] | 2814 | ... '-m', 'doctest', fn, fn2) |
R David Murray | 5707d50 | 2013-06-23 14:24:13 -0400 | [diff] [blame] | 2815 | ... rc2, out2, err2 = script_helper.assert_python_ok( |
Berker Peksag | e495646 | 2016-06-24 09:28:50 +0300 | [diff] [blame] | 2816 | ... '-m', 'doctest', '-o', 'ELLIPSIS', fn) |
R David Murray | 5707d50 | 2013-06-23 14:24:13 -0400 | [diff] [blame] | 2817 | ... rc3, out3, err3 = script_helper.assert_python_ok( |
| 2818 | ... '-m', 'doctest', '-o', 'ELLIPSIS', |
Berker Peksag | e495646 | 2016-06-24 09:28:50 +0300 | [diff] [blame] | 2819 | ... '-o', 'NORMALIZE_WHITESPACE', fn, fn2) |
R David Murray | 5707d50 | 2013-06-23 14:24:13 -0400 | [diff] [blame] | 2820 | ... rc4, out4, err4 = script_helper.assert_python_failure( |
Berker Peksag | e495646 | 2016-06-24 09:28:50 +0300 | [diff] [blame] | 2821 | ... '-m', 'doctest', '-f', fn, fn2) |
R David Murray | 5707d50 | 2013-06-23 14:24:13 -0400 | [diff] [blame] | 2822 | ... rc5, out5, err5 = script_helper.assert_python_ok( |
| 2823 | ... '-m', 'doctest', '-v', '-o', 'ELLIPSIS', |
Berker Peksag | e495646 | 2016-06-24 09:28:50 +0300 | [diff] [blame] | 2824 | ... '-o', 'NORMALIZE_WHITESPACE', fn, fn2) |
R David Murray | 5707d50 | 2013-06-23 14:24:13 -0400 | [diff] [blame] | 2825 | |
| 2826 | Our first test run will show the errors from the first file (doctest stops if a |
| 2827 | file has errors). Note that doctest test-run error output appears on stdout, |
| 2828 | not stderr: |
| 2829 | |
| 2830 | >>> rc1, err1 |
| 2831 | (1, b'') |
R David Murray | 4af6898 | 2013-06-25 08:11:22 -0400 | [diff] [blame] | 2832 | >>> print(normalize(out1)) # doctest: +ELLIPSIS |
R David Murray | 5707d50 | 2013-06-23 14:24:13 -0400 | [diff] [blame] | 2833 | ********************************************************************** |
| 2834 | File "...myfile.doc", line 4, in myfile.doc |
| 2835 | Failed example: |
| 2836 | "abcdef" |
| 2837 | Expected: |
| 2838 | 'a...f' |
| 2839 | Got: |
| 2840 | 'abcdef' |
| 2841 | ********************************************************************** |
| 2842 | File "...myfile.doc", line 6, in myfile.doc |
| 2843 | Failed example: |
| 2844 | "ajkml" |
| 2845 | Expected: |
| 2846 | 'a...l' |
| 2847 | Got: |
| 2848 | 'ajkml' |
| 2849 | ********************************************************************** |
| 2850 | 1 items had failures: |
| 2851 | 2 of 3 in myfile.doc |
| 2852 | ***Test Failed*** 2 failures. |
R David Murray | 5707d50 | 2013-06-23 14:24:13 -0400 | [diff] [blame] | 2853 | |
| 2854 | With -o ELLIPSIS specified, the second run, against just the first file, should |
| 2855 | produce no errors, and with -o NORMALIZE_WHITESPACE also specified, neither |
| 2856 | should the third, which ran against both files: |
| 2857 | |
| 2858 | >>> rc2, out2, err2 |
| 2859 | (0, b'', b'') |
| 2860 | >>> rc3, out3, err3 |
| 2861 | (0, b'', b'') |
| 2862 | |
| 2863 | The fourth run uses FAIL_FAST, so we should see only one error: |
| 2864 | |
| 2865 | >>> rc4, err4 |
| 2866 | (1, b'') |
R David Murray | 4af6898 | 2013-06-25 08:11:22 -0400 | [diff] [blame] | 2867 | >>> print(normalize(out4)) # doctest: +ELLIPSIS |
R David Murray | 5707d50 | 2013-06-23 14:24:13 -0400 | [diff] [blame] | 2868 | ********************************************************************** |
| 2869 | File "...myfile.doc", line 4, in myfile.doc |
| 2870 | Failed example: |
| 2871 | "abcdef" |
| 2872 | Expected: |
| 2873 | 'a...f' |
| 2874 | Got: |
| 2875 | 'abcdef' |
| 2876 | ********************************************************************** |
| 2877 | 1 items had failures: |
| 2878 | 1 of 2 in myfile.doc |
| 2879 | ***Test Failed*** 1 failures. |
R David Murray | 5707d50 | 2013-06-23 14:24:13 -0400 | [diff] [blame] | 2880 | |
| 2881 | The fifth test uses verbose with the two options, so we should get verbose |
| 2882 | success output for the tests in both files: |
| 2883 | |
| 2884 | >>> rc5, err5 |
| 2885 | (0, b'') |
R David Murray | 4af6898 | 2013-06-25 08:11:22 -0400 | [diff] [blame] | 2886 | >>> print(normalize(out5)) |
R David Murray | 5707d50 | 2013-06-23 14:24:13 -0400 | [diff] [blame] | 2887 | Trying: |
| 2888 | 1 + 1 |
| 2889 | Expecting: |
| 2890 | 2 |
| 2891 | ok |
| 2892 | Trying: |
| 2893 | "abcdef" |
| 2894 | Expecting: |
| 2895 | 'a...f' |
| 2896 | ok |
| 2897 | Trying: |
| 2898 | "ajkml" |
| 2899 | Expecting: |
| 2900 | 'a...l' |
| 2901 | ok |
| 2902 | 1 items passed all tests: |
| 2903 | 3 tests in myfile.doc |
| 2904 | 3 tests in 1 items. |
| 2905 | 3 passed and 0 failed. |
| 2906 | Test passed. |
| 2907 | Trying: |
| 2908 | 1 + 1 |
| 2909 | Expecting: |
| 2910 | 2 |
| 2911 | ok |
| 2912 | Trying: |
| 2913 | "abc def" |
| 2914 | Expecting: |
| 2915 | 'abc def' |
| 2916 | ok |
| 2917 | 1 items had no tests: |
| 2918 | myfile2 |
| 2919 | 1 items passed all tests: |
| 2920 | 2 tests in myfile2.test_func |
| 2921 | 2 tests in 2 items. |
| 2922 | 2 passed and 0 failed. |
| 2923 | Test passed. |
R David Murray | 5707d50 | 2013-06-23 14:24:13 -0400 | [diff] [blame] | 2924 | |
| 2925 | We should also check some typical error cases. |
| 2926 | |
| 2927 | Invalid file name: |
| 2928 | |
| 2929 | >>> rc, out, err = script_helper.assert_python_failure( |
Berker Peksag | e495646 | 2016-06-24 09:28:50 +0300 | [diff] [blame] | 2930 | ... '-m', 'doctest', 'nosuchfile') |
R David Murray | 5707d50 | 2013-06-23 14:24:13 -0400 | [diff] [blame] | 2931 | >>> rc, out |
| 2932 | (1, b'') |
R David Murray | 4af6898 | 2013-06-25 08:11:22 -0400 | [diff] [blame] | 2933 | >>> print(normalize(err)) # doctest: +ELLIPSIS |
R David Murray | 5707d50 | 2013-06-23 14:24:13 -0400 | [diff] [blame] | 2934 | Traceback (most recent call last): |
| 2935 | ... |
| 2936 | FileNotFoundError: [Errno ...] No such file or directory: 'nosuchfile' |
| 2937 | |
| 2938 | Invalid doctest option: |
| 2939 | |
| 2940 | >>> rc, out, err = script_helper.assert_python_failure( |
Berker Peksag | e495646 | 2016-06-24 09:28:50 +0300 | [diff] [blame] | 2941 | ... '-m', 'doctest', '-o', 'nosuchoption') |
R David Murray | 5707d50 | 2013-06-23 14:24:13 -0400 | [diff] [blame] | 2942 | >>> rc, out |
| 2943 | (2, b'') |
R David Murray | 4af6898 | 2013-06-25 08:11:22 -0400 | [diff] [blame] | 2944 | >>> print(normalize(err)) # doctest: +ELLIPSIS |
R David Murray | 5707d50 | 2013-06-23 14:24:13 -0400 | [diff] [blame] | 2945 | usage...invalid...nosuchoption... |
| 2946 | |
| 2947 | """ |
| 2948 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2949 | ###################################################################### |
| 2950 | ## Main |
| 2951 | ###################################################################### |
| 2952 | |
| 2953 | def test_main(): |
| 2954 | # Check the doctest cases in doctest itself: |
Giampaolo Rodola' | e09fb71 | 2014-04-04 15:34:17 +0200 | [diff] [blame] | 2955 | ret = support.run_doctest(doctest, verbosity=True) |
Victor Stinner | 931602a | 2016-03-25 12:48:17 +0100 | [diff] [blame] | 2956 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2957 | # Check the doctest cases defined here: |
| 2958 | from test import test_doctest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 2959 | support.run_doctest(test_doctest, verbosity=True) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2960 | |
Miss Islington (bot) | 5a0c398 | 2018-03-06 07:16:11 -0800 | [diff] [blame^] | 2961 | # Run unittests |
| 2962 | support.run_unittest(__name__) |
| 2963 | |
| 2964 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2965 | def test_coverage(coverdir): |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 2966 | trace = support.import_module('trace') |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 2967 | tracer = trace.Trace(ignoredirs=[sys.base_prefix, sys.base_exec_prefix,], |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2968 | trace=0, count=1) |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 2969 | tracer.run('test_main()') |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2970 | r = tracer.results() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 2971 | print('Writing coverage results...') |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2972 | r.write_results(show_missing=True, summary=True, |
| 2973 | coverdir=coverdir) |
| 2974 | |
| 2975 | if __name__ == '__main__': |
| 2976 | if '-c' in sys.argv: |
| 2977 | test_coverage('/tmp/doctest.cover') |
| 2978 | else: |
| 2979 | test_main() |