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 = DocTestPars
| 1872 | return str(self.test) |
| 1873 | |
| 1874 | class DebugRunner(DocTestRunner): |
| 1875 | r"""Run doc tests but raise an exception as soon as there is a failure. |
| 1876 | |
| 1877 | If an unexpected exception occurs, an UnexpectedException is raised. |
| 1878 | It contains the test, the example, and the original exception: |
| 1879 | |
| 1880 | >>> runner = DebugRunner(verbose=False) |
| 1881 | >>> test = DocTestParser().get_doctest('>>> raise KeyError\n42', |
| 1882 | ... {}, 'foo', 'foo.py', 0) |
| 1883 | >>> try: |
| 1884 | ... runner.run(test) |
| 1885 | ... except UnexpectedException as f: |
| 1886 | ... failure = f |
| 1887 | |
| 1888 | >>> failure.test is test |
| 1889 | True |
| 1890 | |
| 1891 | >>> failure.example.want |
| 1892 | '42\n' |
| 1893 | |
| 1894 | >>> exc_info = failure.exc_info |
| 1895 | >>> raise exc_info[1] # Already has the traceback |
| 1896 | Traceback (most recent call last): |
| 1897 | ... |
| 1898 | KeyError |
| 1899 | |
| 1900 | We wrap the original exception to give the calling application |
| 1901 | access to the test and example information. |
| 1902 | |
| 1903 | If the output doesn't match, then a DocTestFailure is raised: |
| 1904 | |
| 1905 | >>> test = DocTestParser().get_doctest(''' |
| 1906 | ... >>> x = 1 |
| 1907 | ... >>> x |
| 1908 | ... 2 |
| 1909 | ... ''', {}, 'foo', 'foo.py', 0) |
| 1910 | |
| 1911 | >>> try: |
| 1912 | ... runner.run(test) |
| 1913 | ... except DocTestFailure as f: |
| 1914 | ... failure = f |
| 1915 | |
| 1916 | DocTestFailure objects provide access to the test: |
| 1917 | |
| 1918 | >>> failure.test is test |
| 1919 | True |
| 1920 | |
| 1921 | As well as to the example: |
| 1922 | |
| 1923 | >>> failure.example.want |
| 1924 | '2\n' |
| 1925 | |
| 1926 | and the actual output: |
| 1927 | |
| 1928 | >>> failure.got |
| 1929 | '1\n' |
| 1930 | |
| 1931 | If a failure or error occurs, the globals are left intact: |