简体   繁体   中英

Not able call a local method from setUpClass

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

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