简体   繁体   中英

How to write a unittest with setUpClass & tearDownClass in Python 2.3

I am trying to write unittests in python 2.3. I've got everything working accept the class level setUp and tearDown function. I've tried to look online but am not able to find a way to define these in a python 2.3 unittest case.

Hence, is it possible to have setUpClass() & tearDownClass() in a python 2.3 testcase? If yes, how do I go about doing that?

According to the Python 2.3 standard library documentation , unittest.TestCase subclassing should work in the same way it works in more recent versions of the language.

import unittest

class MyTest(unittest.TestCase):

    def setUp(self):
        'Do stuff to set up before each test'

    def tearDown(self):
        'Do stuff to tear down after each test'

If there was something more specific that you're trying to accomplish, could you describe it?

Try to use nose or other runner that is compatible with python 2.3; The python 2.3 runner didn't know about this methods (setUpClass/tearDownClass).

In python 2.3 to decorate a function you should do manually (no syntactic sugar) like:

class MyTest(unittest.TestCase):
    def setUpClass(cls): ...
    setUpClass = classmethod(setUpClass)

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