python - Python 类方法钩子和派生
问题描述
我有以下代码:
import unittest
class MyBaseThingTestClass(unittest.TestCase):
"""
Abstract
"""
def setUp(self):
# Is a hook in unittest.TestCase
self.x = 1
def tearDown(self):
# Is a hook in unittest.TestCase
self.x = 0
class MyThingTestClass(MyBaseThingTestClass):
"""
Real Test
"""
def setUp(self):
print('I do what I want')
super(MyThingTestClass, self).setUp()
def tearDown(self):
print('Im done')
super(MyThingTestClass, self).tearDown()
def test_x(self):
assert self.x == 1
if __name__ == '__main__':
unittest.main()
unittest.TestCase 的 setUp 和 tearDown 方法是 Hooks,所以没有实现,我们不需要在 MyBaseThingTestClass 中调用 super:
def setUp(self):
"Hook method for setting up the test fixture before exercising it."
pass
def tearDown(self):
"Hook method for deconstructing the test fixture after testing it."
pass
我的问题是:是否可以像我一样开发两级派生,而无需在 MyThingTestClass 中为 setUp 和 tearDown 方法调用 super ?我看到的唯一方法是创建两个具有其他名称的方法,因此 MyThingTestClass 应该覆盖它们而不是默认方法。但我不希望这样,因为每个人都知道这些名称并想使用它们。
谢谢
解决方案
推荐阅读
- java - 如何使用 Mockito 将 void 方法测试到服务类中?
- sql - 使临时表永久化
- c++ - 从内存导入密钥时出现 Crypto++ BERDecode 异常?
- python - DSaaS 使用 Python 添加全局规则
- python - 在小提琴图中可视化线图
- java - 如何将 MockWebServer 中的响应分配给预期的请求?
- python - 如何监督变分自动编码器 (VAE) 中的潜在变量
- rxjs - 使用 rxjs mergeMap() 发出并发请求是顺序的
- python - 从 python 和 android 评估 tflite 模型时的不同结果
- amazon-web-services - 连接到 aws redis elasticache 的生菜客户端配置