def _startIOCapture(self): """ Start capturing stdout and stderr to StringIOs If options['verbose'] has been set, the data will be output to the stdout/stderr as well """ if self.options.valueOf('silent'): # just capture it self.err = StringIO.StringIO() self.out = self.err else: # capture I/O but display it as well self.out = TeeStringIO(sys.stdout) self.err = TeeStringIO(sys.stderr, targetStream = self.out) sys.stderr = self.err sys.stdout = self.out
class BaseTestRunner(IOMixin): """ Base class for all other TestRunners. A TestRunner runs a specific kind of test (i.e. UnitTestRunner) """ # overloaded for each runner, contains allowed options for each runner # for this case: # * silent - True if the output shouldn't be redirected to the console _runnerOptions = {'silent': Option} # path to this current file setupDir = os.path.dirname(__file__) def __init__(self, **kwargs): """ Options can be passed as kwargs, currently the following is supported: """ self.err = None self.out = None # make a TestConfig instance available everywhere self.config = TestConfig.getInstance() # initialize allowed options self.options = OptionProxy(self._runnerOptions) self.options.configure(**kwargs) self._logger = logging.getLogger('test') def _run(self): """ This method should be overloaded by inheriting classes. It should provide the code that executes the actual tests, returning output information. """ pass def run(self): """ Executes the actual test code """ # get the description from the first lines # of the docstring description = self.__doc__.strip().split('\n')[0] self._startIOCapture() self._info("Running %s" % description) self._callOptions('pre_run') result = self._run() self._callOptions('post_run') if result: self._success("%s successful!" % description) else: self._error("%s failed!" % description) # ask the option handlers to compute a final message self._callOptions('final_message') self._writeReport(self.__class__.__name__, self._finishIOCapture()) return result def _startIOCapture(self): """ Start capturing stdout and stderr to StringIOs If options['verbose'] has been set, the data will be output to the stdout/stderr as well """ if self.options.valueOf('silent'): # just capture it self.err = StringIO.StringIO() self.out = self.err else: # capture I/O but display it as well self.out = TeeStringIO(sys.stdout) self.err = TeeStringIO(sys.stderr, targetStream = self.out) sys.stderr = self.err sys.stdout = self.out def _finishIOCapture(self): """ Restore stdout/stderr and return the captured data """ sys.stderr = sys.__stderr__ sys.stdout = sys.__stdout__ return self.out.getvalue() @staticmethod def findPlugins(): """ Goes throught the plugin directories, and adds existing unit test dirs """ dirs = [] for epoint in pkg_resources.iter_entry_points('indico.ext_types'): dirs.append(os.path.dirname(epoint.load().__file__)) for epoint in pkg_resources.iter_entry_points('indico.ext'): dirs.append(os.path.dirname(epoint.load().__file__)) return dirs @staticmethod def _redirectPipeToStdout(pipe): """ Redirect a given pipe to stdout """ while True: data = pipe.readline() if not data: break print data, def _writeReport(self, filename, content): """ Write the test report, using the filename and content that are passed """ filePath = os.path.join(self.setupDir, 'report', filename + ".txt") try: f = open(filePath, 'w') f.write(content) f.close() except IOError: return "Unable to write in %s, check your file permissions." % \ os.path.join(self.setupDir, 'report', filename + ".txt") self._info("report in %s" % filePath) @staticmethod def walkThroughFolders(rootPath, foldersPattern): """ Scan a directory and return folders which match the pattern """ rootPluginsPath = os.path.join(rootPath) foldersArray = [] for root, __, __ in os.walk(rootPluginsPath): if root.endswith(foldersPattern) > 0: foldersArray.append(root) return foldersArray def _callOptions(self, method, *args): """ Invokes the option proxy, providing the hot spot with name 'method', that options should have extended """ # invoke the option proxy self.options.call(self, method, *args)