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 and 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:
import six
import unittest
class MyTestCase(unittest.TestCase):
@unittest.skipIf(six.PY3, "not compatible with Python 3")
def test_example(self):
# This test won't run under Python 3
pass
Credits
Thanks to my colleague Nicola for giving me the inspiration to write this post.
from Planet Python
via read more
No comments:
Post a Comment