2012-09-26 9 views
5

私は、基本クラスで、次のデコレータていますインポートのみクラスの静的メソッド

class BaseTests(TestCase): 
    @staticmethod 
    def check_time(self, fn): 
     @wraps(fn) 
     def test_wrapper(*args,**kwargs): 
      # do checks ... 
     return test_wrapper 

、次のクラスが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 ... 

私はSpecificTestでデコレータを使用したいですBaseTests.check_timeを使わなくても元のコードに長い名前が付いているので、これを多くの場所で使う必要があります。何か案は?

EDIT: 私はCHECK_TIME BaseTestsファイル内の独立した機能作ることを決めた、と単純に

from path.base_posting import BaseTests, check_time 

答えて

9

は、単にあなたの第二のモジュールで

check_time = BaseTests.check_time 

を入れてインポ​​ートします。


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 ... 

check_timeをstaticmethodにすることを再考することもできます。これは、ユースケースでスタティックメソッドとしてではなくスタンドアロンの機能として使用されているように見えるからです。

+1

よろしくお願いいたします。メソッドを直接インポートするソリューションを探していましたが、あなたの提案も同様に機能します。 – Alex

関連する問題