4

나는 데이터베이스에 대해 실행 일부 통합 테스트를 받고 있어요, 나는 이런 식으로 뭔가 보이는 구조를 가지고 싶습니다기본 테스트 클래스에 정의 된 클래스 속성을 찾으려면 어떻게해야합니까?

class OracleMixin(object): 
    oracle = True 
    # ... set up the oracle connection 

class SqlServerMixin(object): 
    sql_server = True 
    # ... set up the sql server connection 

class SomeTests(object): 
    integration = True 
    # ... define test methods here 

class test_OracleSomeTests(SomeTests, OracleMixin): 
    pass 

class test_SqlServerSomeTests(SomeTests, SqlServerMixin): 
    pass 

이 방법은, 내가 SQL 서버 테스트를 실행할 수 있습니다 및 오라클과 같은 별도 테스트를 이 :이 같은

nosetests -a oracle 
nosetests -a sql_server 

또는 모든 통합 테스트 : 그러나

nosetests -a integration 

, 코는 ATTRIB를 찾을 것으로 보인다 서브 클래스의 클래스는 아니고, 기본 클래스의 서브 클래스는 아니다. 따라서 다음과 같이 테스트 클래스를 정의하거나 테스트를 실행하지 않아도됩니다.

class test_OracleSomeTests(SomeTests, OracleMixin): 
    oracle = True 
    integration = True 

class test_SqlServerSomeTests(SomeTests, SqlServerMixin): 
    sql_server = True 
    integration = True 

유지 관리가 약간 지루합니다. 어떤 아이디어가이 문제를 어떻게 해결할 수 있습니까? 하나의 기본 클래스 만 처리한다면 메타 클래스를 사용하고 각 클래스의 특성을 정의하면됩니다. 하지만 테스트 클래스에 대한 메타 클래스, 오라클에 대한 메타 클래스 및 SQL Server에 대한 메타 클래스에 대한 불안감이 생깁니다.

답변

4

나는 당신 자신의 플러그인을 만들지 않고는 그렇게 생각하지 않는다. attrib 플러그인의 코드는 __dict__ 클래스 만 찾습니다. 여기에 code

def wantClass(self, cls): 
    """Accept the class if the class or any method is wanted. 
    """ 
    cls_attr = cls.__dict__ 
    if self.validateAttrib(cls_attr) is not False: 
     return None 
    ... 

당신은 같은 (테스트하지)를 할 수있는 플러그인을 해킹 할 수있다.

def wantClass(self, cls): 
    """Accept the class if the class or any method is wanted. 
    """ 
    for class_ in cls.__mro__: 
     cls_attr = class_.__dict__ 
     if self.validateAttrib(cls_attr) is not False: 
      return None 
    cls_attr = cls.__dict__ 
    ... 

그러나 메타 클래스 옵션이 더 좋거나 나쁘다는 것은 확실하지 않습니다.

+0

(테스트 클래스는 서브 클래스에 대해 알 필요가 없도록 실제로는 같은 변수를 정의해야하지만) "새로운 스타일"클래스가 아니라 (즉, "객체"를 확장하지 않는다), 나는'__mro__' 속성이있을 것이라고 생각하지 않습니다. getattr (cls, '__mro__', [])의 class_에 대해 'class_ ...에 대해 에서 '까지 줄을 변경하십시오. –

0

당신이 부모 클래스에 정의 된 속성을 찾으려면, 당신은 당신이

원하는 범위를 액세스하기 위해 부모 클래스의 이름을 추가해야합니다 서브 클래스에서 같은 이름의 속성이있는 경우

class Parent: 
    prop = 'a property' 

    def self_prop(self): 
     print self.prop 

    # will always print 'a property' 
    def parent_prop(self): 
     print Parent.prop 

class Child(Parent): 
    prop = 'child property' 

    def access_eclipsed(self): 
     print Parent.prop 

class Other(Child): 
    pass 

>>> Parent().self_prop() 
"a property" 
>>> Parent().parent_prop() 
"a property" 
>>> Child().self_prop() 
"child property" 
>>> Child().parent_prop() 
"a property" 
>>> Child().access_eclipsed() 
"a property" 
>>> Other().self_prop() 
"child property" 
>>> Other().parent_prop() 
"a property" 
>>> Other().access_eclipsed() 
"a property" 

을하고 방금 시도 할 수 있도록 다른 변수를 정의하는 두 개의 다른 클래스를 가지고있는 것처럼 귀하의 경우에 그것은 본다 : 나는이 당신이 원하는 것을 믿습니다 캐치 : 테스트 기능의 상단에 또는 이니셜 라이저에

말할

try: 
    isSQLServer = self.sql_server 
except AttributeError: 
    isSQLServer = False 

부모 클래스 중 하나가있는 경우

관련 문제