""" Test Selection --------------
Test selection is handled by a Selector. The test loader calls the appropriate selector method for each object it encounters that it thinks may be a test. """
# for efficiency and easier mocking
"""Core test selector. Examines test candidates and determines whether, given the specified configuration, the test candidate should be selected as a test. """ config = Config()
"""Does the name match my requirements?
To match, a name must match config.testMatch OR config.include and it must not match config.exclude """ or (self.include and [_f for _f in [inc.search(name) for inc in self.include] if _f])) and ((not self.exclude) or not [_f for _f in [exc.search(name) for exc in self.exclude] if _f] ))
"""Is the class a wanted test class?
A class must be a unittest.TestCase subclass, or match test name requirements. Classes that start with _ are always excluded. """ wanted = declared else: and (issubclass(cls, unittest.TestCase) or self.matches(cls.__name__)))
log.debug("Plugin setting selection of %s to %s", cls, plug_wants) wanted = plug_wants
"""Is the directory a wanted test directory?
All package directories match, so long as they do not match exclude. All other directories must match test requirements. """ tail = op_basename(dirname) if ispackage(dirname): wanted = (not self.exclude or not [_f for _f in [exc.search(tail) for exc in self.exclude] if _f]) else: wanted = (self.matches(tail) or (self.config.srcDirs and tail in self.config.srcDirs)) plug_wants = self.plugins.wantDirectory(dirname) if plug_wants is not None: log.debug("Plugin setting selection of %s to %s", dirname, plug_wants) wanted = plug_wants log.debug("wantDirectory %s? %s", dirname, wanted) return wanted
"""Is the file a wanted test file?
The file must be a python source file and match testMatch or include, and not match exclude. Files that match ignore are *never* wanted, regardless of plugin, testMatch, include or exclude settings. """ # never, ever load files that match anything in ignore # (.* _* and *setup*.py by default) if ignore_this.search(base) ] base) log.info('%s is executable; skipped', file) return False
log.debug("plugin setting want %s to %s", file, plug_wants) wanted = plug_wants
"""Is the function a test function? """ funcname = function.compat_func_name else: except AttributeError: # not a function return False wanted = declared else: wanted = plug_wants
"""Is the method a test method? """ try: method_name = method.__name__ except AttributeError: # not a method return False if method_name.startswith('_'): # never collect 'private' methods return False declared = getattr(method, '__test__', None) if declared is not None: wanted = declared else: wanted = self.matches(method_name) plug_wants = self.plugins.wantMethod(method) if plug_wants is not None: wanted = plug_wants log.debug("wantMethod %s? %s", method, wanted) return wanted
"""Is the module a test module?
The tail of the module name must match test requirements. One exception: we always want __main__. """ wanted = declared else: or module.__name__ == '__main__' wanted = plug_wants
"""A test address represents a user's request to run a particular test. The user may specify a filename or module (or neither), and/or a callable (a class, function, or method). The naming format for test addresses is:
filename_or_module:callable
Filenames that are not absolute will be made absolute relative to the working dir.
The filename or module part will be considered a module name if it doesn't look like a file, that is, if it doesn't exist on the file system and it doesn't contain any directory separators and it doesn't end in .py.
Callables may be a class name, function name, method name, or class.method specification. """ workingDir = os.getcwd() name, self.filename, self.module, self.call) if self.module is not None: self.filename = getfilename(self.module, self.workingDir) self.filename)) 'Final resolution of test name %s: file %s module %s call %s', name, self.filename, self.module, self.call)
return (self.filename, self.module, self.call)
def __str__(self): return self.name
def __repr__(self): return "%s: (%s, %s, %s)" % (self.name, self.filename, self.module, self.call) |