A class used to run DocTest test cases, and accumulate statistics. The `run` method is used to process a single DocTest case. It returns a tuple `(f, t)`, where `t` is the number of test cases tried, and `f` is the number of test cases that failed. >>> tests = DocTes
| 1145 | ###################################################################### |
| 1146 | |
| 1147 | class DocTestRunner: |
| 1148 | """ |
| 1149 | A class used to run DocTest test cases, and accumulate statistics. |
| 1150 | The `run` method is used to process a single DocTest case. It |
| 1151 | returns a tuple `(f, t)`, where `t` is the number of test cases |
| 1152 | tried, and `f` is the number of test cases that failed. |
| 1153 | |
| 1154 | >>> tests = DocTestFinder().find(_TestClass) |
| 1155 | >>> runner = DocTestRunner(verbose=False) |
| 1156 | >>> tests.sort(key = lambda test: test.name) |
| 1157 | >>> for test in tests: |
| 1158 | ... print(test.name, '->', runner.run(test)) |
| 1159 | _TestClass -> TestResults(failed=0, attempted=2) |
| 1160 | _TestClass.__init__ -> TestResults(failed=0, attempted=2) |
| 1161 | _TestClass.get -> TestResults(failed=0, attempted=2) |
| 1162 | _TestClass.square -> TestResults(failed=0, attempted=1) |
| 1163 | |
| 1164 | The `summarize` method prints a summary of all the test cases that |
| 1165 | have been run by the runner, and returns an aggregated `(f, t)` |
| 1166 | tuple: |
| 1167 | |
| 1168 | >>> runner.summarize(verbose=1) |
| 1169 | 4 items passed all tests: |
| 1170 | 2 tests in _TestClass |
| 1171 | 2 tests in _TestClass.__init__ |
| 1172 | 2 tests in _TestClass.get |
| 1173 | 1 tests in _TestClass.square |
| 1174 | 7 tests in 4 items. |
| 1175 | 7 passed and 0 failed. |
| 1176 | Test passed. |
| 1177 | TestResults(failed=0, attempted=7) |
| 1178 | |
| 1179 | The aggregated number of tried examples and failed examples is |
| 1180 | also available via the `tries` and `failures` attributes: |
| 1181 | |
| 1182 | >>> runner.tries |
| 1183 | 7 |
| 1184 | >>> runner.failures |
| 1185 | 0 |
| 1186 | |
| 1187 | The comparison between expected outputs and actual outputs is done |
| 1188 | by an `OutputChecker`. This comparison may be customized with a |
| 1189 | number of option flags; see the documentation for `testmod` for |
| 1190 | more information. If the option flags are insufficient, then the |
| 1191 | comparison may also be customized by passing a subclass of |
| 1192 | `OutputChecker` to the constructor. |
| 1193 | |
| 1194 | The test runner's display output can be controlled in two ways. |
| 1195 | First, an output function (`out) can be passed to |
| 1196 | `TestRunner.run`; this function will be called with strings that |
| 1197 | should be displayed. It defaults to `sys.stdout.write`. If |
| 1198 | capturing the output is not sufficient, then the display output |
| 1199 | can be also customized by subclassing DocTestRunner, and |
| 1200 | overriding the methods `report_start`, `report_success`, |
| 1201 | `report_unexpected_exception`, and `report_failure`. |
| 1202 | """ |
| 1203 | # This divider string is used to separate failure messages, and to |
| 1204 | # separate sections of the summary. |