r"""Run the test case without results and without catching exceptions The unit test framework includes a debug method on test cases and test suites to support post-mortem debugging. The test code is run in such a way that errors are not caught. This way a
(self)
| 2420 | ) |
| 2421 | |
| 2422 | def debug(self): |
| 2423 | r"""Run the test case without results and without catching exceptions |
| 2424 | |
| 2425 | The unit test framework includes a debug method on test cases |
| 2426 | and test suites to support post-mortem debugging. The test code |
| 2427 | is run in such a way that errors are not caught. This way a |
| 2428 | caller can catch the errors and initiate post-mortem debugging. |
| 2429 | |
| 2430 | The DocTestCase provides a debug method that raises |
| 2431 | UnexpectedException errors if there is an unexpected |
| 2432 | exception: |
| 2433 | |
| 2434 | >>> test = DocTestParser().get_doctest('>>> raise KeyError\n42', |
| 2435 | ... {}, 'foo', 'foo.py', 0) |
| 2436 | >>> case = DocTestCase(test) |
| 2437 | >>> try: |
| 2438 | ... case.debug() |
| 2439 | ... except UnexpectedException as f: |
| 2440 | ... failure = f |
| 2441 | |
| 2442 | The UnexpectedException contains the test, the example, and |
| 2443 | the original exception: |
| 2444 | |
| 2445 | >>> failure.test is test |
| 2446 | True |
| 2447 | |
| 2448 | >>> failure.example.want |
| 2449 | '42\n' |
| 2450 | |
| 2451 | >>> exc_info = failure.exc_info |
| 2452 | >>> raise exc_info[1] # Already has the traceback |
| 2453 | Traceback (most recent call last): |
| 2454 | ... |
| 2455 | KeyError |
| 2456 | |
| 2457 | If the output doesn't match, then a DocTestFailure is raised: |
| 2458 | |
| 2459 | >>> test = DocTestParser().get_doctest(''' |
| 2460 | ... >>> x = 1 |
| 2461 | ... >>> x |
| 2462 | ... 2 |
| 2463 | ... ''', {}, 'foo', 'foo.py', 0) |
| 2464 | >>> case = DocTestCase(test) |
| 2465 | |
| 2466 | >>> try: |
| 2467 | ... case.debug() |
| 2468 | ... except DocTestFailure as f: |
| 2469 | ... failure = f |
| 2470 | |
| 2471 | DocTestFailure objects provide access to the test: |
| 2472 | |
| 2473 | >>> failure.test is test |
| 2474 | True |
| 2475 | |
| 2476 | As well as to the example: |
| 2477 | |
| 2478 | >>> failure.example.want |
| 2479 | '2\n' |
no test coverage detected