我正在对数据库运行一些集成测试,我希望有一个看起来像这样的结构:
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 Server测试和Oracle测试:
nosetests -a oracle
nosetests -a sql_server
或者像这样的所有集成测试:
nosetests -a integration
但是,看起来nose只会查找子类的属性,而不是基类.因此,我必须像这样定义测试类,否则测试将不会运行:
class test_OracleSomeTests(SomeTests, OracleMixin):
oracle = True
integration = True
class test_SqlServerSomeTests(SomeTests, SqlServerMixin):
sql_server = True
integration = True
维护起来有点乏味.任何想法如何解决这个问题?如果我只是处理一个基类,我只使用元类并定义每个类的属性.但是我对于测试类的元类,Oracle的元类和SQL Server的元类感到不安.
解决方法:
我不认为你可以不制作自己的插件. 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__
...
但是,我不确定这是元类选项的好坏.