开发者

Not able call a local method from setUpClass

开发者 https://www.devze.com 2023-03-05 02:06 出处:网络
My code: class TestSystemPromotion(unittest2.TestCase): @classmethod def setUpClass(self): ... self.setup_test_data()

My code:

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()

Error which I'm getting is:

TypeError: unbound method setup_test_data() must be called with TestSystemPromotion
instance a开发者_C百科s first argument (got nothing instead)


You can't call instance methods from class methods. Either consider using setUp instead, or make setup_test_data a class method too. Also, it's better if you called the argument cls instead of self to avoid the confusion - the first argument to the class method is the class, not the instance. The instance (self) doesn't exist at all when setUpClass is called.

class TestSystemPromotion(unittest2.TestCase):

  @classmethod
  def setUpClass(cls):
    cls.setup_test_data()


  @classmethod
  def setup_test_data(cls):
    ...

  def test_something(self):
    ...

Or:

class TestSystemPromotion(unittest2.TestCase):

  def setUp(self):
    self.setup_test_data()

  def setup_test_data(self):
    ...

  def test_something(self):
    ...

For better comprehension, you can think of it this way: cls == type(self)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号