简体   繁体   中英

Using variable inside setUpClass (class method) - unittest python

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:

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) 

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

调用BaseTest.__init__(cls, arg) 之前,需要设置cls.param

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