I am using the unittest framework from Python, and run into issues when setting up a class for a test case. I would like to call a method from the base class in the setUpClass() method as follows:
class TestA(unittest.TestCase):
def __init__(self, *args, **kwargs):
super(TestA, self).__init__(*args, **kwargs)
self.variable = "hello"
def do_something(self):
print(self.variable)
return self.variable
class TestB(TestA.TestA):
@classmethod
def setUpClass(cls):
var = cls.do_something()
But I get an error saying that I cannot call do_something().
TypeError: unbound method do_something() must be called with TestB instance as first argument (got nothing instead)
I have no idea how (and if) I can call a method from the base class as part of the setUpClass method.
What am I missing?
Use super()
.
>>> class Base:
... def do(self):
... print('base is doing something')
...
>>> class Child(Base):
... def better_do(self):
... super().do()
... print('much better')
...
>>>
>>> c = Child()
>>> c.do()
base is doing something
>>> c.better_do()
base is doing something
much better
>>>
A class method, however, is bit different. You must call the class itself.
>>> class Base:
... @classmethod
... def cls_do(cls):
... print('Base class is doing something')
...
>>> Base.cls_do()
Base class is doing something
>>>
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.