Sometimes we want to run certain tests only on a specific version of Python.
Suppose you are migrating a large project from Python 2 to Python 3 and you know in advance that certain tests won't run
under Python 3.
Chances are that during the migration you are already using the six library. The six libraries have
two boolean properties which are initialised to
True depending on the Python version which is being used:
PY2 when running under Python 2
PY3 when running under Python 3.
This library, combined with the skipIf method of unittest library
can be used to easily skip tests when using Python 3:
@unittest.skipIf(six.PY3, "not compatible with Python 3")
# This test won't run under Python 3
Thanks to my colleague Nicola for giving me the inspiration to write this post.