diff options
author | Nick Coghlan <ncoghlan@gmail.com> | 2010-04-28 14:29:06 +0000 |
---|---|---|
committer | Nick Coghlan <ncoghlan@gmail.com> | 2010-04-28 14:29:06 +0000 |
commit | dfb45dfd048160a60b2165f25a2b1fb99ca0495d (patch) | |
tree | fabfed4a36184962d489ce58ed6a76ae7e2d7e0e /Lib | |
parent | 616de77779fe9732b54d2c7cddfea62afff28a88 (diff) | |
download | cpython-git-dfb45dfd048160a60b2165f25a2b1fb99ca0495d.tar.gz |
Issue 7490: make IGNORE_EXCEPTION_DETAIL also ignore details of the module containing the exception under test (original patch by Lennart Regebro)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/doctest.py | 6 | ||||
-rw-r--r-- | Lib/test/test_doctest.py | 71 |
2 files changed, 74 insertions, 3 deletions
diff --git a/Lib/doctest.py b/Lib/doctest.py index a9357b599d..70d3359545 100644 --- a/Lib/doctest.py +++ b/Lib/doctest.py @@ -1282,9 +1282,9 @@ class DocTestRunner: # Another chance if they didn't care about the detail. elif self.optionflags & IGNORE_EXCEPTION_DETAIL: - m1 = re.match(r'[^:]*:', example.exc_msg) - m2 = re.match(r'[^:]*:', exc_msg) - if m1 and m2 and check(m1.group(0), m2.group(0), + m1 = re.match(r'(?:[^:]*\.)?([^:]*:)', example.exc_msg) + m2 = re.match(r'(?:[^:]*\.)?([^:]*:)', exc_msg) + if m1 and m2 and check(m1.group(1), m2.group(1), self.optionflags): outcome = SUCCESS diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py index df65d7e067..105106809c 100644 --- a/Lib/test/test_doctest.py +++ b/Lib/test/test_doctest.py @@ -865,6 +865,77 @@ detail: >>> doctest.DocTestRunner(verbose=False).run(test) TestResults(failed=0, attempted=1) +IGNORE_EXCEPTION_DETAIL also ignores difference in exception formatting +between Python versions. For example, in Python 3.x, the module path of +the exception is in the output, but this will fail under Python 2: + + >>> def f(x): + ... r''' + ... >>> from httplib import HTTPException + ... >>> raise HTTPException('message') + ... Traceback (most recent call last): + ... httplib.HTTPException: message + ... ''' + >>> test = doctest.DocTestFinder().find(f)[0] + >>> doctest.DocTestRunner(verbose=False).run(test) + ... # doctest: +ELLIPSIS + ********************************************************************** + File ..., line 4, in f + Failed example: + raise HTTPException('message') + Expected: + Traceback (most recent call last): + httplib.HTTPException: message + Got: + Traceback (most recent call last): + ... + HTTPException: message + TestResults(failed=1, attempted=2) + +But in Python 2 the module path is not included, an therefore a test must look +like the following test to succeed in Python 2. But that test will fail under +Python 3. + + >>> def f(x): + ... r''' + ... >>> from httplib import HTTPException + ... >>> raise HTTPException('message') + ... Traceback (most recent call last): + ... HTTPException: message + ... ''' + >>> test = doctest.DocTestFinder().find(f)[0] + >>> doctest.DocTestRunner(verbose=False).run(test) + TestResults(failed=0, attempted=2) + +However, with IGNORE_EXCEPTION_DETAIL, the module name of the exception +(if any) will be ignored: + + >>> def f(x): + ... r''' + ... >>> from httplib import HTTPException + ... >>> raise HTTPException('message') #doctest: +IGNORE_EXCEPTION_DETAIL + ... Traceback (most recent call last): + ... HTTPException: message + ... ''' + >>> test = doctest.DocTestFinder().find(f)[0] + >>> doctest.DocTestRunner(verbose=False).run(test) + TestResults(failed=0, attempted=2) + +The module path will be completely ignored, so two different module paths will +still pass if IGNORE_EXCEPTION_DETAIL is given. This is intentional, so it can +be used when exceptions have changed module. + + >>> def f(x): + ... r''' + ... >>> from httplib import HTTPException + ... >>> raise HTTPException('message') #doctest: +IGNORE_EXCEPTION_DETAIL + ... Traceback (most recent call last): + ... foo.bar.HTTPException: message + ... ''' + >>> test = doctest.DocTestFinder().find(f)[0] + >>> doctest.DocTestRunner(verbose=False).run(test) + TestResults(failed=0, attempted=2) + But IGNORE_EXCEPTION_DETAIL does not allow a mismatch in the exception type: >>> def f(x): |