请先阅读问题,然后再将其标记为副本!
这个问题是由许多人提出的,但几乎所有人都给出了相同的解决办法,我已经申请。
所以我有类TestCase
,B
,C
,D
&;E
。
类C
&;D
继承类B
&;类E
继承二者C
&;D
。类B
继承TestCase
。
当我运行代码时,类E
只与类C
的方法一起运行,并且一直忽略D
。
现在我的课是这样的
class GenericAuthClass(TestCase):
def setUp(self):
"""Create a dummy user for the purpose of testing."""
# set some objects and variabels
def _get_authenticated_api_client(self):
pass
class TokenAuthTests(GenericAuthClass):
def _get_authenticated_api_client(self):
"""Get token recieved on login."""
super()._get_authenticated_api_client()
# make object
return object
class BasicAuthTests(GenericAuthClass):
def _get_authenticated_api_client(self):
"""Get token recieved on login."""
super()._get_authenticated_api_client()
# make object
return object
class ClientTestCase(BasicAuthTests, TokenAuthTests):
def dothis(self):
return self._get_authenticated_api_client()
C
中的方法(使用相同的名称)和E
中的D
,就像C++
中的菱形问题一样?到目前为止,当我从E
使用self.method()
调用某个方法时,它只从C
调用该方法,而忽略了从D
调用的同一个方法,而我认为它应该同时调用这两个方法。请注意,该方法在类E
中不存在,我的代码现在运行正常,没有错误,只是从C
调用了该方法。你知道吗This seems like a
Python
question mainly but taggingdjango
too asTestCase
class might have something to do with it.
This answer是正确的
这意味着您需要编写自己的基类,该基类位于
B
和C
之间,并且TestCase
通过实现附加方法结束super
链,然后不将任何调用委派给其父类:相关问题 更多 >
编程相关推荐