Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)
like image 669
dmp Avatar asked May 09 '11 14:05

dmp


1 Answers

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)

like image 61
Rosh Oxymoron Avatar answered Oct 23 '22 00:10

Rosh Oxymoron