Ale*_*lex 14 python import inheritance
我在基类中有以下装饰器:
class BaseTests(TestCase):
@staticmethod
def check_time(self, fn):
@wraps(fn)
def test_wrapper(*args,**kwargs):
# do checks ...
return test_wrapper
Run Code Online (Sandbox Code Playgroud)
以下类继承自BaseTests:
from path.base_posting import BaseTests
from path.base_posting.BaseTests import check_time # THIS LINE DOES NOT WORK!
class SpecificTest(BaseTests):
@check_time # use the decorator
def test_post(self):
# do testing ...
Run Code Online (Sandbox Code Playgroud)
我想在上面的SpecificTest中使用装饰器,而不必使用BaseTests.check_time,因为在原始代码中他们有很长的名字,我必须在很多地方使用它.有任何想法吗?
编辑:我决定让check_time成为BaseTests文件中的一个独立函数,并简单地导入
from path.base_posting import BaseTests, check_time
Run Code Online (Sandbox Code Playgroud)
unu*_*tbu 16
简单的说
check_time = BaseTests.check_time
Run Code Online (Sandbox Code Playgroud)
在你的第二个模块中:
from module_paths.base_posting import BaseTests
check_time = BaseTests.check_time
class SpecificTest(BaseTests):
@check_time # use the decorator
def test_post(self):
# do testing ...
Run Code Online (Sandbox Code Playgroud)
您可能还想重新考虑制作check_time静态方法,因为看起来您的用例更多地将其用作独立函数而不是静态方法.