Python unittest: how to use setUpClass() and tearDownClass() with arguments

2.8k Views Asked by At

I would like to use python unittest setUpClass and tearDownClass methods with arguments. More specifically, here is what I am doing now:

import unittest2 as unittest
cache = VCache(arg1, arg2, arg3)

class Validation(unittest.TestCase):
    ''' Unit test class with a local cache to avoid intensive network traffic. '''

    @classmethod
    def setUpClass(cls):
        ''' Copy all required data locally. '''
        super(Validation, cls).setUpClass()
        cache.setUp()

    @classmethod
    def tearDownClass(cls):
        ''' Remove the cache. '''
        super(Validation, cls).tearDownClass()
        cache.tearDown()

It works but now I would like to wrap the cache management in a subclass of Validation, to avoid using a global variable and writing the setUpClass and tearDownClass everytime.

This does not work of course because setUpClass() and tearDownClass() do not accept arguments. Any solution?

0

There are 0 best solutions below