Nose/Unittest different tests dependent on Python version
Question: I am writing [a nose test](https://travis-ci.org/pydata/pandas/jobs/75838430)
for an API addition to pandas I'm writing. They use Travis CI to run their
test suites.

In the version of Python I'm using (Python 2.7.6, numpy 1.9.2) the following
raises a `TypeError`:

    
    
    >>> numpy.round([1.23, 1.132], 2.5)
    TypeError: integer argument expected, got float
    

But in a version being tested against by Travis CI (Python 2.6.9, numpy 1.9.2)
the same command raises a warning:

    
    
    >>> numpy.round([1.23, 1.132], 2.5)
    /home/some_user/anaconda/envs/py26/lib/python2.6/site-packages/numpy/core/fromnumeric.py:45: DeprecationWarning: integer argument expected, got float
      result = getattr(asarray(obj), method)(*args, **kwds)
    array([ 1.23,  1.13])
    

This means that my `assertRaises(TypeError)` test fails.

How can I write a test which will either check for `TypeError` or do
`assert_produces_warning(DeprecationWarning)` depending on what Python version
is being tested?


Answer: There seem to be two ways to do this, used in the pandas testing source.

There are several (very similar) variants on this approach:

    
    
    if sys.version_info < (2, 7):
    

And the rather more delightful:

    
    
    from distutils.version import LooseVersion
    if sys.version < LooseVersion('2.7'):
    

which has the entertaining comment in its docstring:

> Version numbering for anarchists and software realists.

I don't know of either way being preferred over the other.

