How can I get a nose to find class attributes defined in a base test class?

I get integration tests with a database, and I would like to have a structure that looks something like this:

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 

Thus, I can run SQL Server tests and Oracle tests separately as follows:

 nosetests -a oracle nosetests -a sql_server 

Or all integration tests:

 nosetests -a integration 

However, it looks like the nose will only look for attributes in a subclass, not a base class. Thus, I have to define test classes like this, or the tests will not run:

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

It is a little tiring to maintain. Any ideas how to get around this? If I were just dealing with one base class, I would just use a metaclass and define attributes for each class. But I feel awkward about having a metaclass for a test class, a metaclass for Oracle, and a metaclass for SQL Server.

+4
source share
2 answers

I do not think you can make your own plugin. The code in the attribute plugin only looks at __dict__ classes. Here is the 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 ... 

You can hack the plugin to do something like (not verified).

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

However, I'm not sure if this is better or worse than the metaclass variant.

+4
source

If you want to find the attribute defined in the parent class, and you have an attribute with the same name in the subclass, you will need to add the name of the parent class to access the scope you want

I believe this is what you want:

 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" 

and in your case it looks like you have two different classes that define different variables, so you can just try: catch: at the top of the test functions, or perhaps in the initializer

and say

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

(although in fact they must define the same variables so that the test class does not know anything about subclasses)

0
source

All Articles