5 Test selection is handled by a Selector. The test loader calls the
6 appropriate selector method for each object it encounters that it
12 from nose.config import Config
13 from nose.util import split_test_name, src, getfilename, getpackage, ispackage
15 log = logging.getLogger(__name__)
17 __all__ = ['Selector', 'defaultSelector', 'TestAddress']
20 # for efficiency and easier mocking
21 op_join = os.path.join
22 op_basename = os.path.basename
23 op_exists = os.path.exists
24 op_splitext = os.path.splitext
25 op_isabs = os.path.isabs
26 op_abspath = os.path.abspath
29 class Selector(object):
30 """Core test selector. Examines test candidates and determines whether,
31 given the specified configuration, the test candidate should be selected
34 def __init__(self, config):
37 self.configure(config)
39 def configure(self, config):
41 self.exclude = config.exclude
42 self.ignoreFiles = config.ignoreFiles
43 self.include = config.include
44 self.plugins = config.plugins
45 self.match = config.testMatch
47 def matches(self, name):
48 """Does the name match my requirements?
50 To match, a name must match config.testMatch OR config.include
51 and it must not match config.exclude
53 return ((self.match.search(name)
56 [inc.search(name) for inc in self.include])))
57 and ((not self.exclude)
59 [exc.search(name) for exc in self.exclude])
62 def wantClass(self, cls):
63 """Is the class a wanted test class?
65 A class must be a unittest.TestCase subclass, or match test name
66 requirements. Classes that start with _ are always excluded.
68 declared = getattr(cls, '__test__', None)
69 if declared is not None:
72 wanted = (not cls.__name__.startswith('_')
73 and (issubclass(cls, unittest.TestCase)
74 or self.matches(cls.__name__)))
76 plug_wants = self.plugins.wantClass(cls)
77 if plug_wants is not None:
78 log.debug("Plugin setting selection of %s to %s", cls, plug_wants)
80 log.debug("wantClass %s? %s", cls, wanted)
83 def wantDirectory(self, dirname):
84 """Is the directory a wanted test directory?
86 All package directories match, so long as they do not match exclude.
87 All other directories must match test requirements.
89 tail = op_basename(dirname)
90 if ispackage(dirname):
91 wanted = (not self.exclude
93 [exc.search(tail) for exc in self.exclude]
96 wanted = (self.matches(tail)
97 or (self.config.srcDirs
98 and tail in self.config.srcDirs))
99 plug_wants = self.plugins.wantDirectory(dirname)
100 if plug_wants is not None:
101 log.debug("Plugin setting selection of %s to %s",
104 log.debug("wantDirectory %s? %s", dirname, wanted)
107 def wantFile(self, file):
108 """Is the file a wanted test file?
110 The file must be a python source file and match testMatch or
111 include, and not match exclude. Files that match ignore are *never*
112 wanted, regardless of plugin, testMatch, include or exclude settings.
114 # never, ever load files that match anything in ignore
115 # (.* _* and *setup*.py by default)
116 base = op_basename(file)
117 ignore_matches = [ ignore_this for ignore_this in self.ignoreFiles
118 if ignore_this.search(base) ]
120 log.debug('%s matches ignoreFiles pattern; skipped',
123 if not self.config.includeExe and os.access(file, os.X_OK):
124 log.info('%s is executable; skipped', file)
126 dummy, ext = op_splitext(base)
129 wanted = pysrc and self.matches(base)
130 plug_wants = self.plugins.wantFile(file)
131 if plug_wants is not None:
132 log.debug("plugin setting want %s to %s", file, plug_wants)
134 log.debug("wantFile %s? %s", file, wanted)
137 def wantFunction(self, function):
138 """Is the function a test function?
141 if hasattr(function, 'compat_func_name'):
142 funcname = function.compat_func_name
144 funcname = function.__name__
145 except AttributeError:
148 declared = getattr(function, '__test__', None)
149 if declared is not None:
152 wanted = not funcname.startswith('_') and self.matches(funcname)
153 plug_wants = self.plugins.wantFunction(function)
154 if plug_wants is not None:
156 log.debug("wantFunction %s? %s", function, wanted)
159 def wantMethod(self, method):
160 """Is the method a test method?
163 method_name = method.__name__
164 except AttributeError:
167 if method_name.startswith('_'):
168 # never collect 'private' methods
170 declared = getattr(method, '__test__', None)
171 if declared is not None:
174 wanted = self.matches(method_name)
175 plug_wants = self.plugins.wantMethod(method)
176 if plug_wants is not None:
178 log.debug("wantMethod %s? %s", method, wanted)
181 def wantModule(self, module):
182 """Is the module a test module?
184 The tail of the module name must match test requirements. One exception:
185 we always want __main__.
187 declared = getattr(module, '__test__', None)
188 if declared is not None:
191 wanted = self.matches(module.__name__.split('.')[-1]) \
192 or module.__name__ == '__main__'
193 plug_wants = self.plugins.wantModule(module)
194 if plug_wants is not None:
196 log.debug("wantModule %s? %s", module, wanted)
199 defaultSelector = Selector
202 class TestAddress(object):
203 """A test address represents a user's request to run a particular
204 test. The user may specify a filename or module (or neither),
205 and/or a callable (a class, function, or method). The naming
206 format for test addresses is:
208 filename_or_module:callable
210 Filenames that are not absolute will be made absolute relative to
213 The filename or module part will be considered a module name if it
214 doesn't look like a file, that is, if it doesn't exist on the file
215 system and it doesn't contain any directory separators and it
218 Callables may be a class name, function name, method name, or
219 class.method specification.
221 def __init__(self, name, workingDir=None):
222 if workingDir is None:
223 workingDir = os.getcwd()
225 self.workingDir = workingDir
226 self.filename, self.module, self.call = split_test_name(name)
227 log.debug('Test name %s resolved to file %s, module %s, call %s',
228 name, self.filename, self.module, self.call)
229 if self.filename is None:
230 if self.module is not None:
231 self.filename = getfilename(self.module, self.workingDir)
233 self.filename = src(self.filename)
234 if not op_isabs(self.filename):
235 self.filename = op_abspath(op_join(workingDir,
237 if self.module is None:
238 self.module = getpackage(self.filename)
240 'Final resolution of test name %s: file %s module %s call %s',
241 name, self.filename, self.module, self.call)
244 return (self.filename, self.module, self.call)
250 return "%s: (%s, %s, %s)" % (self.name, self.filename,
251 self.module, self.call)