dmp*_*dmp 7 python unit-testing
我的代码:
class TestSystemPromotion(unittest2.TestCase):
@classmethod
def setUpClass(self):
...
self.setup_test_data()
..
def test_something(self):
...
def setup_test_data(self):
...
if __name__ == '__main__':
unittest2.main()
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
TypeError: unbound method setup_test_data() must be called with TestSystemPromotion
instance as first argument (got nothing instead)
Run Code Online (Sandbox Code Playgroud)
Ros*_*ron 15
您无法从类方法中调用实例方法.或者考虑使用setUp,或者也可以setup_test_data使用类方法.此外,最好是调用参数cls而不是self避免混淆 - 类方法的第一个参数是类,而不是实例.调用self时,instance()根本不存在setUpClass.
class TestSystemPromotion(unittest2.TestCase):
@classmethod
def setUpClass(cls):
cls.setup_test_data()
@classmethod
def setup_test_data(cls):
...
def test_something(self):
...
Run Code Online (Sandbox Code Playgroud)
要么:
class TestSystemPromotion(unittest2.TestCase):
def setUp(self):
self.setup_test_data()
def setup_test_data(self):
...
def test_something(self):
...
Run Code Online (Sandbox Code Playgroud)
为了更好地理解,你可以这样想: cls == type(self)