chfw
chfw

Reputation: 4592

Doctest succeeds in Python v2.7 but not with Python 2.6 due to inconsistent error message

I have coded up some doc test cases in my code comments and put them under travis-ci to run against Python v2.6, v2.7 and Pypy. Only v2.7 succeeded.

My doctest looks like the following::

>>> a = ['a', 'b']
>>> a.index('i')
Traceback (most recent call last):
...
ValueError: 'i' is not in list

Python v2.6 and Pypy both complained that the error return were:

ValueError: list.index(x): x not in list

Is there a better way of testing it than simply deleting these test cases?

Thanks

chfw

Upvotes: 0

Views: 70

Answers (2)

Simeon Visser
Simeon Visser

Reputation: 122376

Avoid the use of doctests as they're indeed not portable. Consider using a testing framework such as unittest (built-in), nosetests or py.test. Assertions in your tests can be made portable because they can be written in a general way.

You can make certain tests specific to certain versions of Python so that you'll only test the content of error messages (if desired) in those Python versions. Or, to put differently, you can allow tests to be skipped for most Python versions except a particular one.

Upvotes: 0

zero323
zero323

Reputation: 330183

Have you tried using ELLIPSIS like this:

>>> a = ['a', 'b']
>>> a.index('i') # doctest:+ELLIPSIS
Traceback (most recent call last):
...
ValueError: ...

Upvotes: 1

Related Questions