简体   繁体   中英

unittest setUpClass alternative for python < 2.7

In Python 2.7, it is possible to run class level setup in unittest.Testcase as follows:

class ClassName(unittest.TestCase):
    @classmethod
    def setUpClass(self):
        print 'Some class level setup'

Unfortunately, I need to run some tests in Python 2.6 environment. What is the alternative for setUpClass in that version?

The Python 2.7 version of unittest is available for Python 2.6 (actually, all the way back to Python 2.3!) as the unittest2 module on PyPI: https://pypi.python.org/pypi/unittest2

Once that's installed, setupClass methods will work on your test classes:

import unittest2
class ClassName(unittest2.TestCase):
    @classmethod
    def setUpClass(cls):
        print 'Some class level setup'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM