pythonunittest2

What is the most pythonic way to support unittest2 features across a range of Python versions?


I can think of two ways to ensure that I can use modern features from the unittest library across a wide range of Python versions:

try:
    from unittest2 import TestCase
except ImportError:
    from unittest import TestCase

or

import sys
if sys.verson_info.major>=2 and sys.version_info.minor>=7:
    from unittest import TestCase
else:
    from unittest2 import TestCase

Which one of these is more Pythonic?


Solution

  • i'd use the try statement. It's an often used idiom. Also your sys version is wrong for python3.3:

    >>> if sys.version_info.major>=2 and sys.version_info.minor>=7:
    ...     from unittest import TestCase
    ... else:
    ...     from unittest2 import TestCase
    ... 
    Traceback (most recent call last):
      File "<stdin>", line 4, in <module>
    ImportError: No module named 'unittest2'
    

    While it should be:

    >>> import sys
    >>> major, minor = sys.version_info.major, sys.version_info.minor
    >>> if (major >= 2 and minor >= 7) or (major >= 3 and minor >= 2):
    ...     from unittest import TestCase
    ... else:
    ...     from unittest2 import TestCase
    ... 
    >>> 
    

    This shows also that the try version is more robust across python's versions.

    I often use the try variant when I have an "accelerated" version of the module written in C, at the end of the file I put a:

    try:
        from _accelerated import *
    except ImportError:
        pass
    

    to overwrite the python implementation with the accellerated one.