1""" 2Test Selection 3""" 4import unittest 5from nose_helper.config import Config 6 7class Selector(object): 8 """Examines test candidates and determines whether, 9 given the specified configuration, the test candidate should be selected 10 as a test. 11 """ 12 def __init__(self, config): 13 if config is None: 14 config = Config() 15 self.configure(config) 16 17 def configure(self, config): 18 self.config = config 19 self.match = config.testMatch 20 21 def matches(self, name): 22 return self.match.search(name) 23 24 def wantClass(self, cls): 25 """Is the class a wanted test class 26 """ 27 declared = getattr(cls, '__test__', None) 28 if declared is not None: 29 wanted = declared 30 else: 31 wanted = (not cls.__name__.startswith('_') 32 and (issubclass(cls, unittest.TestCase) 33 or self.matches(cls.__name__))) 34 35 return wanted 36 37 def wantFunction(self, function): 38 """Is the function a test function 39 """ 40 try: 41 if hasattr(function, 'compat_func_name'): 42 funcname = function.compat_func_name 43 else: 44 funcname = function.__name__ 45 except AttributeError: 46 # not a function 47 return False 48 import inspect 49 arguments = inspect.getargspec(function) 50 if len(arguments[0]) or arguments[1] or arguments[2]: 51 return False 52 declared = getattr(function, '__test__', None) 53 if declared is not None: 54 wanted = declared 55 else: 56 wanted = not funcname.startswith('_') and self.matches(funcname) 57 58 return wanted 59 60 def wantMethod(self, method): 61 """Is the method a test method 62 """ 63 try: 64 method_name = method.__name__ 65 except AttributeError: 66 # not a method 67 return False 68 if method_name.startswith('_'): 69 # never collect 'private' methods 70 return False 71 declared = getattr(method, '__test__', None) 72 if declared is not None: 73 wanted = declared 74 else: 75 wanted = self.matches(method_name) 76 return wanted 77 78 def wantModule(self, module): 79 """Is the module a test module 80 we always want __main__. 81 """ 82 declared = getattr(module, '__test__', None) 83 if declared is not None: 84 wanted = declared 85 else: 86 wanted = self.matches(module.__name__.split('.')[-1]) \ 87 or module.__name__ == '__main__' 88 return wanted 89 90defaultSelector = Selector 91 92