zhutoulala
zhutoulala

Reputation: 4832

how to skip a unittest case in python 2.6

unittest.skip* decorators and methods as below (see here for more details) were added since python2.7 and i found they are quite useful.

unittest.skip(reason)
unittest.skipIf(condition, reason)
unittest.skipUnless(condition, reason)

However, my question is how we should do the similar if working with python2.6?

Upvotes: 11

Views: 2523

Answers (3)

John McGehee
John McGehee

Reputation: 10299

Use unittest2.

The following code imports the right unittest in a manner transparent to the rest of your code:

import sys
if sys.version_info < (2, 7):
    import unittest2 as unittest
else:
    import unittest

Upvotes: 5

Thomas
Thomas

Reputation: 6752

If you can't use unittest2 and don't mind having a different number of tests in Python 2.6, you can write simple decorators that make the tests disappear:

try:
    from unittest import skip, skipUnless
except ImportError:
    def skip(f):
        return lambda self: None

    def skipUnless(condition, reason):
        if condition:
            return lambda x: x
        else:
            return lambda x: None

Upvotes: 5

jagttt
jagttt

Reputation: 1060

If you have the liberty of installing additional packages, you can use unittest2 which is Python 2.7 unittest backported to Python 2.3+. It does contain the skip decorators.

Upvotes: 2

Related Questions