r"""Run doc tests but raise an exception as soon as there is a failure. If an unexpected exception occurs, an UnexpectedException is raised. It contains the test, the example, and the original exception: >>> runner = DebugRunner(verbose=False) >>> test = DocTe
| 1786 | return str(self.test) |
| 1787 | |
| 1788 | class DebugRunner(DocTestRunner): |
| 1789 | r"""Run doc tests but raise an exception as soon as there is a failure. |
| 1790 | |
| 1791 | If an unexpected exception occurs, an UnexpectedException is raised. |
| 1792 | It contains the test, the example, and the original exception: |
| 1793 | |
| 1794 | >>> runner = DebugRunner(verbose=False) |
| 1795 | >>> test = DocTestParser().get_doctest('>>> raise KeyError\n42', |
| 1796 | ... {}, 'foo', 'foo.py', 0) |
| 1797 | >>> try: |
| 1798 | ... runner.run(test) |
| 1799 | ... except UnexpectedException as f: |
| 1800 | ... failure = f |
| 1801 | |
| 1802 | >>> failure.test is test |
| 1803 | True |
| 1804 | |
| 1805 | >>> failure.example.want |
| 1806 | '42\n' |
| 1807 | |
| 1808 | >>> exc_info = failure.exc_info |
| 1809 | >>> raise exc_info[1] # Already has the traceback |
| 1810 | Traceback (most recent call last): |
| 1811 | ... |
| 1812 | KeyError |
| 1813 | |
| 1814 | We wrap the original exception to give the calling application |
| 1815 | access to the test and example information. |
| 1816 | |
| 1817 | If the output doesn't match, then a DocTestFailure is raised: |
| 1818 | |
| 1819 | >>> test = DocTestParser().get_doctest(''' |
| 1820 | ... >>> x = 1 |
| 1821 | ... >>> x |
| 1822 | ... 2 |
| 1823 | ... ''', {}, 'foo', 'foo.py', 0) |
| 1824 | |
| 1825 | >>> try: |
| 1826 | ... runner.run(test) |
| 1827 | ... except DocTestFailure as f: |
| 1828 | ... failure = f |
| 1829 | |
| 1830 | DocTestFailure objects provide access to the test: |
| 1831 | |
| 1832 | >>> failure.test is test |
| 1833 | True |
| 1834 | |
| 1835 | As well as to the example: |
| 1836 | |
| 1837 | >>> failure.example.want |
| 1838 | '2\n' |
| 1839 | |
| 1840 | and the actual output: |
| 1841 | |
| 1842 | >>> failure.got |
| 1843 | '1\n' |
| 1844 | |
| 1845 | If a failure or error occurs, the globals are left intact: |