0

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

chfw
  • 4,502
  • 2
  • 29
  • 32

2 Answers2

1

Have you tried using ELLIPSIS like this:

>>> a = ['a', 'b']
>>> a.index('i') # doctest:+ELLIPSIS
Traceback (most recent call last):
...
ValueError: ...
zero323
  • 322,348
  • 103
  • 959
  • 935
  • I forgot to add "# doctest:+ELLIPSIS" initially. After adding it after my code line, both python v2.6 and pypy passed my tests in the second attempt. thanks @zero323 – chfw Dec 18 '14 at 00:37
0

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.

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
  • I agree with you that tests had better be written using those test frameworks. I just wanted to add that doctest test cases would validate the orphan example codes in docstring and rst files after the code has been changed. So those test cases would remind me of updating those invalidated example code. – chfw Dec 18 '14 at 00:42