Convert doctest tests for a module to a unittest test suite. This converts each documentation string in a module that contains doctest tests to a unittest test case. If any of the tests in a doc string fail, then the test case fails. An exception is raised showing the name of
(module=None, globs=None, extraglobs=None, test_finder=None,
**options)
| 2465 | |
| 2466 | |
| 2467 | def DocTestSuite(module=None, globs=None, extraglobs=None, test_finder=None, |
| 2468 | **options): |
| 2469 | """ |
| 2470 | Convert doctest tests for a module to a unittest test suite. |
| 2471 | |
| 2472 | This converts each documentation string in a module that |
| 2473 | contains doctest tests to a unittest test case. If any of the |
| 2474 | tests in a doc string fail, then the test case fails. An exception |
| 2475 | is raised showing the name of the file containing the test and a |
| 2476 | (sometimes approximate) line number. |
| 2477 | |
| 2478 | The `module` argument provides the module to be tested. The argument |
| 2479 | can be either a module or a module name. |
| 2480 | |
| 2481 | If no argument is given, the calling module is used. |
| 2482 | |
| 2483 | A number of options may be provided as keyword arguments: |
| 2484 | |
| 2485 | setUp |
| 2486 | A set-up function. This is called before running the |
| 2487 | tests in each file. The setUp function will be passed a DocTest |
| 2488 | object. The setUp function can access the test globals as the |
| 2489 | globs attribute of the test passed. |
| 2490 | |
| 2491 | tearDown |
| 2492 | A tear-down function. This is called after running the |
| 2493 | tests in each file. The tearDown function will be passed a DocTest |
| 2494 | object. The tearDown function can access the test globals as the |
| 2495 | globs attribute of the test passed. |
| 2496 | |
| 2497 | globs |
| 2498 | A dictionary containing initial global variables for the tests. |
| 2499 | |
| 2500 | optionflags |
| 2501 | A set of doctest option flags expressed as an integer. |
| 2502 | """ |
| 2503 | |
| 2504 | if test_finder is None: |
| 2505 | test_finder = DocTestFinder() |
| 2506 | |
| 2507 | module = _normalize_module(module) |
| 2508 | tests = test_finder.find(module, globs=globs, extraglobs=extraglobs) |
| 2509 | |
| 2510 | if not tests and sys.flags.optimize >=2: |
| 2511 | # Skip doctests when running with -O2 |
| 2512 | suite = _DocTestSuite() |
| 2513 | suite.addTest(SkipDocTestCase(module)) |
| 2514 | return suite |
| 2515 | |
| 2516 | tests.sort() |
| 2517 | suite = _DocTestSuite() |
| 2518 | |
| 2519 | for test in tests: |
| 2520 | if len(test.examples) == 0: |
| 2521 | continue |
| 2522 | if not test.filename: |
| 2523 | filename = module.__file__ |
| 2524 | if filename[-4:] == ".pyc": |
no test coverage detected