doctests/unittest problem with exception

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Jan 26 22:49:32 EST 2013


Paul wrote:

> Hello. I converted doctests into DocTestSuite() to use with unittest. And
> try it under Python 3.
> 
> And, sure, I get errors with unmatched exceptions details (mismatched name
> of exception class: a.b.c.MyError instead of MyError). So, I have 2
> questions:
> 
> 1) how to turn on option IGNORE_EXCEPTION_DETAIL for all doctests in
> DocStestSuite (like 'optionflags' argument in doctest.testmod())

Have you tried reading the Fine Manual? If you don't have access to the
Python documentation 

http://docs.python.org/3/library/doctest.html

you can get interactive help at the interpreter. Launch the Python
interactive interpreter, and then give these two commands:

import doctest
help(doctest.DocTestSuite)


In particular, note that DocTestSuite takes a keyword argument:

    optionflags
       A set of doctest option flags expressed as an integer.


So try passing optionFlags=doctest.IGNORE_EXCEPTION_DETAIL to the
DocTestSuite.



> 2) Is a way to ignore all 'package path' of exception but not message?
> Something like:
> ---cut--- 
>         Traceback (most recent call last):
>             ...
>         ...MyError: 'details are not ignored!'
> ---cut---
> see, ellipsis-prefix in MyError


Have you tried it to see? Add this comment to your docstring, following the
line which causes an exception:

>>> example()  #doctest: +ELLIPSIS
Traceback (most recent call last):
  ...
...MyError: 'details are not ignored!'


Does that do what you expect?


-- 
Steven




More information about the Python-list mailing list