5

I have a doctest where I test a float conversion:

>>> float('fish')

In Python < 2.7 this results in:

ValueError: invalid literal for float(): fish

In Python 2.7 the result is

ValueError: could not convert string to float: fish

Can I make both these results acceptable in my doctest?

pafcu
  • 7,808
  • 12
  • 42
  • 55

2 Answers2

4

You are looking for the doctest.IGNORE_EXCEPTION_DETAIL option. The documentation has a good example of how to use it. You can also use the ellipsis constant in the doctest like a wildcard.

Something like this as the doctest:

>>> float('fish')
ValueError:...

You can see Alex Martellis post about this same thing here.

Community
  • 1
  • 1
Jason Webb
  • 7,938
  • 9
  • 40
  • 49
  • 1
    Now that's a great use for ellipsis (which is a constant singleton, however!) –  Jan 05 '11 at 17:08
3

Yes with something like this :

>>> float('fish') #doctest: +IGNORE_EXCEPTION_DETAIL
Traceback (most recent call last):
ValueError:

look here for why.

mouad
  • 67,571
  • 18
  • 114
  • 106