Using variable inside setUpClass (class method) - unittest python
<p>I would like to use a variable which I initialize in the subclass to be used in the setUpClass of parent class , here is the code snippet below:</p>

<pre><code>class BaseTest(unittest.TestCase):

    def __init__(cls, arg):
        unittest.TestCase.__init__(cls, arg)

    @classmethod
    def setUpClass(cls):
        print "Base.setUp()" ,cls.param

    @classmethod
    def tearDownClass(cls):
        print "Base.tearDown()"


class TestSomething(BaseTest):
    def __init__(cls, arg):
        BaseTest.__init__(cls, arg)
        cls.param = 'hello'

    def test_silly(self):
        print "Test method"
        self.assertTrue(1+1 == 2)

    def test_silly1(self):
        print "Test method"
        self.assertTrue(1+1 == 2) 
</code></pre>

<p>It gives me this : <code>AttributeError: type object 'TestSomething' has no attribute 'param'</code> , which I understand is true as <code>setUpClass and tearDownClass</code> are class and not instance method, any way to use variable instantiated in the base class to be used in <code>setUpClass of parent class</code>?</p>

<p>You need to set <code>cls.param</code> <em>before</em> you call <code>BaseTest.__init__(cls, arg)</code>.</p>

python python-2.7 unit-testing testing python-unittest