Failed to call local method from setUpClass

My code is:

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

The error I am getting is:

 TypeError: unbound method setup_test_data() must be called with TestSystemPromotion instance as first argument (got nothing instead) 
+6
source share
1 answer

You cannot call instance methods from class methods. Either consider using setUp , or make the setup_test_data class method. It’s also better if you called cls instead of self to avoid confusion - the first argument to the class method is the class, not the instance. An instance ( self ) does not exist at all when calling setUpClass .

 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 a better understanding, you can think of it this way: cls == type(self)

+14
source

Source: https://habr.com/ru/post/887626/


All Articles