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
| 1899 | return str(self.test) |
| 1900 | |
| 1901 | class DebugRunner(DocTestRunner): |
| 1902 | r"""Run doc tests but raise an exception as soon as there is a failure. |
| 1903 | |
| 1904 | If an unexpected exception occurs, an UnexpectedException is raised. |
| 1905 | It contains the test, the example, and the original exception: |
| 1906 | |
| 1907 | >>> runner = DebugRunner(verbose=False) |
| 1908 | >>> test = DocTestParser().get_doctest('>>> raise KeyError\n42', |
| 1909 | ... {}, 'foo', 'foo.py', 0) |
| 1910 | >>> try: |
| 1911 | ... runner.run(test) |
| 1912 | ... except UnexpectedException as f: |
| 1913 | ... failure = f |
| 1914 | |
| 1915 | >>> failure.test is test |
| 1916 | True |
| 1917 | |
| 1918 | >>> failure.example.want |
| 1919 | '42\n' |
| 1920 | |
| 1921 | >>> exc_info = failure.exc_info |
| 1922 | >>> raise exc_info[1] # Already has the traceback |
| 1923 | Traceback (most recent call last): |
| 1924 | ... |
| 1925 | KeyError |
| 1926 | |
| 1927 | We wrap the original exception to give the calling application |
| 1928 | access to the test and example information. |
| 1929 | |
| 1930 | If the output doesn't match, then a DocTestFailure is raised: |
| 1931 | |
| 1932 | >>> test = DocTestParser().get_doctest(''' |
| 1933 | ... >>> x = 1 |
| 1934 | ... >>> x |
| 1935 | ... 2 |
| 1936 | ... ''', {}, 'foo', 'foo.py', 0) |
| 1937 | |
| 1938 | >>> try: |
| 1939 | ... runner.run(test) |
| 1940 | ... except DocTestFailure as f: |
| 1941 | ... failure = f |
| 1942 | |
| 1943 | DocTestFailure objects provide access to the test: |
| 1944 | |
| 1945 | >>> failure.test is test |
| 1946 | True |
| 1947 | |
| 1948 | As well as to the example: |
| 1949 | |
| 1950 | >>> failure.example.want |
| 1951 | '2\n' |
| 1952 | |
| 1953 | and the actual output: |
| 1954 | |
| 1955 | >>> failure.got |
| 1956 | '1\n' |
| 1957 | |
| 1958 | If a failure or error occurs, the globals are left intact: |