简体   繁体   English

在setUpClass内部使用变量(类方法)-unittest Python

[英]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: 我想使用一个在子类中初始化的变量,该变量将在父类的setUpClass中使用,这是下面的代码片段:

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 ? 它给了我这个: AttributeError: type object 'TestSomething' has no attribute 'param' ,我理解这是正确的,因为setUpClass and tearDownClass是类,而不是实例方法,任何使用在setUpClass of parent class使用的基类中实例化的变量的方法setUpClass of parent class

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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