def get_module(fname): """Given a filename or module path name, return a tuple of the form (filename, module). """ if fname.endswith('.py'): modpath, inpkg = fpath2modpath(fname) if not modpath: raise RuntimeError("can't find module %s" % fname) if not inpkg: if modpath in _non_pkg_files: old = _non_pkg_files[modpath] if old != fname: raise RuntimeError( "module '%s' was already imported earlier from file '%s' so " "it can't be imported from file '%s'. To fix this problem, " "either rename the file or add the file to a python package " "so the resulting module path will be unique." % (modpath, old, fname)) else: _non_pkg_files[modpath] = fname else: modpath = fname fname = find_module(modpath) if not fname: raise ImportError("can't import %s" % modpath) start_coverage() try: mod = import_module(modpath) except ImportError: # this might be a module that's not in the same # environment as testflo, so try temporarily prepending # its parent dirs to sys.path so it'll (hopefully) be # importable oldpath = sys.path[:] sys.path.extend(parent_dirs(fname)) sys.path.append(os.getcwd()) try: mod = import_module(modpath) # don't keep this module around in sys.modules, but # keep a reference to it, else multiprocessing on Windows # will have problems _store[modpath] = mod del sys.modules[modpath] finally: sys.path = oldpath finally: stop_coverage() return fname, mod
def run_test(self, testspec, parent, method): start_time = time.time() if self.nocap_stdout: outstream = sys.stdout else: outstream = DevNull() errstream = cStringIO() setup = getattr(parent, "setUp", None) teardown = getattr(parent, "tearDown", None) run_method = True run_td = True try: old_err = sys.stderr old_out = sys.stdout sys.stdout = outstream sys.stderr = errstream start_coverage() # if there's a setUp method, run it if setup: status = try_call(setup) if status != "OK": run_method = False run_td = False if run_method: status = try_call(getattr(parent, method)) if teardown and run_td: tdstatus = try_call(teardown) if status == "OK": status = tdstatus result = TestResult(testspec, start_time, time.time(), status, errstream.getvalue()) finally: stop_coverage() sys.stderr = old_err sys.stdout = old_out return result
def get_module(fname): """Given a filename or module path name, return a tuple of the form (filename, module). """ if fname.endswith('.py'): modpath = get_module_path(fname) if not modpath: raise RuntimeError("can't find module %s" % fname) else: modpath = fname fname = find_module(modpath) if not fname: raise ImportError("can't import %s" % modpath) start_coverage() try: __import__(modpath) mod = sys.modules[modpath] except ImportError: # this might be a module that's not in the same # environment as testflo, so try temporarily prepending # its parent dirs to sys.path so it'll (hopefully) be # importable pdirs = parent_dirs(fname) oldpath = sys.path[:] sys.path.extend(pdirs) try: __import__(modpath) mod = sys.modules[modpath] # don't keep this module around in sys.modules, but # keep a reference to it, else multiprocessing on Windows # will have problems _store[modpath] = sys.modules[modpath] del sys.modules[modpath] finally: sys.path = oldpath finally: stop_coverage() return fname, mod
def get_module(fname): """Given a filename or module path name, return a tuple of the form (filename, module). """ if fname.endswith('.py'): modpath = get_module_path(fname) if not modpath: raise RuntimeError("can't find module %s" % fname) else: modpath = fname fname = find_module(modpath) if not fname: raise ImportError("can't import %s" % modpath) start_coverage() try: __import__(modpath) mod = sys.modules[modpath] except ImportError: # this might be a module that's not in the same # environment as testflo, so try temporarily prepending # its parent dirs to sys.path so it'll (hopefully) be # importable oldpath = sys.path[:] sys.path.extend(parent_dirs(fname)) sys.path.append(os.getcwd()) try: __import__(modpath) mod = sys.modules[modpath] # don't keep this module around in sys.modules, but # keep a reference to it, else multiprocessing on Windows # will have problems _store[modpath] = sys.modules[modpath] del sys.modules[modpath] finally: sys.path = oldpath finally: stop_coverage() return fname, mod
def get_module(fname): """Given a filename or module path name, return a tuple of the form (filename, module). """ if fname.endswith('.py'): modpath = get_module_path(fname) if not modpath: raise RuntimeError("can't find module %s" % fname) else: modpath = fname fname = find_module(modpath) if not fname: raise ImportError("can't import %s" % modpath) start_coverage() try: __import__(modpath) except ImportError: # this might be a module that's not in the same # environment as testflo, so try temporarily prepending # its parent dirs to sys.path so it'll (hopefully) be # importable pdirs = parent_dirs(fname) oldpath = sys.path[:] sys.path.extend(pdirs) try: __import__(modpath) finally: sys.path = oldpath finally: stop_coverage() return fname, sys.modules[modpath]
def run(self, server=None, addr=None, authkey=None): """Runs the test, assuming status is not already known.""" if self.status is not None: # premature failure occurred , just return return self if server is not None: if MPI is not None and self.mpi and self.nprocs > 0: return self._run_mpi(server, addr, authkey) elif self.isolated: return self._run_isolated(server, addr, authkey) # this is for test files without an __init__ file. This MUST # be done before the call to _get_test_parent. sys.path.insert(0, os.path.dirname(self.spec.split(':',1)[0])) parent, method, _ = self._get_test_parent() if not isinstance(parent, ModuleType) and issubclass(parent, unittest.TestCase): parent = parent(methodName=method) if self.nocapture: outstream = sys.stdout else: outstream = DevNull() errstream = cStringIO() setup = getattr(parent, 'setUp', None) teardown = getattr(parent, 'tearDown', None) run_method = True run_td = True try: old_err = sys.stderr old_out = sys.stdout sys.stdout = outstream sys.stderr = errstream start_coverage() self.start_time = time.time() # if there's a setUp method, run it if setup: status = _try_call(setup) if status != 'OK': run_method = False run_td = False if run_method: status = _try_call(getattr(parent, method)) if teardown and run_td: tdstatus = _try_call(teardown) if status == 'OK': status = tdstatus self.end_time = time.time() self.status = status self.err_msg = errstream.getvalue() self.memory_usage = get_memory_usage() finally: sys.path = sys.path[1:] stop_coverage() sys.stderr = old_err sys.stdout = old_out return self
def run(self, queue=None): """Runs the test, assuming status is not already known.""" if self.status is not None: # premature failure occurred (or dry run), just return return self if queue is not None: if MPI is not None and self.mpi and self.nprocs > 0: return self._run_mpi(queue) elif self.isolated: return self._run_isolated(queue) with TestContext(self): if self.tcase is None: mod, testcase, funcname, nprocs = self._get_test_info() else: mod, testcase, funcname, nprocs = (self.mod, self.tcase, self.funcname, self.nprocs) mod_setup = mod_teardown = tcase_setup = tcase_teardown = None if self._mod_fixture_first: mod_setup = getattr(mod, 'setUpModule', None) if self._mod_fixture_last: mod_teardown = getattr(mod, 'tearDownModule', None) if testcase is not None: if self._tcase_fixture_first: tcase_setup = getattr(testcase, 'setUpClass', None) if self._tcase_fixture_last: tcase_teardown = getattr(testcase, 'tearDownClass', None) parent = testcase(methodName=funcname) # if we get here an nprocs > 0, we need # to set .comm in our TestCase instance. if nprocs > 0: if MPI is not None and self.mpi: parent.comm = MPI.COMM_WORLD else: parent.comm = FakeComm() setup = getattr(parent, 'setUp', None) teardown = getattr(parent, 'tearDown', None) else: parent = mod setup = teardown = None if self.nocapture: outstream = sys.stdout else: outstream = DevNull() errstream = cStringIO() done = False expected = expected2 = expected3 = False try: old_err = sys.stderr old_out = sys.stdout sys.stdout = outstream sys.stderr = errstream start_coverage() self.start_time = time.time() # if there's a module setup, run it if mod_setup: status, expected = _try_call(mod_setup) if status != 'OK': done = True mod_teardown = None # don't do teardown if setup failed # handle @unittest.skip class decorator if not done and hasattr( parent, '__unittest_skip__') and parent.__unittest_skip__: sys.stderr.write("%s\n" % parent.__unittest_skip_why__) status = 'SKIP' done = True tcase_setup = None tcase_teardown = None if tcase_setup: status, expected = _try_call(tcase_setup) if status != 'OK': done = True tcase_teardown = None # if there's a setUp method, run it if not done and setup: status, expected = _try_call(setup) if status != 'OK': done = True if not done: status, expected2 = _try_call(getattr(parent, funcname)) if not done and teardown: tdstatus, expected3 = _try_call(teardown) if status == 'OK': status = tdstatus if tcase_teardown: _try_call(tcase_teardown) if mod_teardown: _try_call(mod_teardown) self.end_time = time.time() self.status = status self.err_msg = errstream.getvalue() self.memory_usage = get_memory_usage() self.expected_fail = expected or expected2 or expected3 if sys.platform == 'win32': self.load1m, self.load5m, self.load15m = (0.0, 0.0, 0.0) else: self.load1m, self.load5m, self.load15m = os.getloadavg() finally: stop_coverage() sys.stderr = old_err sys.stdout = old_out return self
def run(self, queue=None): """Runs the test, assuming status is not already known.""" if self.status is not None: # premature failure occurred (or dry run), just return return self if queue is not None: if MPI is not None and self.mpi and self.nprocs > 0: return self._run_mpi(queue) elif self.isolated: return self._run_isolated(queue) with TestContext(self): if self.tcase is None: mod, testcase, funcname, nprocs = self._get_test_info() else: mod, testcase, funcname, nprocs = (self.mod, self.tcase, self.funcname, self.nprocs) mod_setup = mod_teardown = tcase_setup = tcase_teardown = None if self._mod_fixture_first: mod_setup = getattr(mod, 'setUpModule', None) if self._mod_fixture_last: mod_teardown = getattr(mod, 'tearDownModule', None) if testcase is not None: if self._tcase_fixture_first: tcase_setup = getattr(testcase, 'setUpClass', None) if self._tcase_fixture_last: tcase_teardown = getattr(testcase, 'tearDownClass', None) parent = testcase(methodName=funcname) # if we get here an nprocs > 0, we need # to set .comm in our TestCase instance. if nprocs > 0: if MPI is not None and self.mpi: parent.comm = MPI.COMM_WORLD else: parent.comm = FakeComm() setup = getattr(parent, 'setUp', None) teardown = getattr(parent, 'tearDown', None) else: parent = mod setup = teardown = None if self.nocapture: outstream = sys.stdout else: outstream = DevNull() errstream = cStringIO() done = False expected = expected2 = expected3 = False try: old_err = sys.stderr old_out = sys.stdout sys.stdout = outstream sys.stderr = errstream start_coverage() self.start_time = time.time() # if there's a module setup, run it if mod_setup: status, expected = _try_call(mod_setup) if status != 'OK': done = True mod_teardown = None # don't do teardown if setup failed # handle @unittest.skip class decorator if not done and hasattr(parent, '__unittest_skip__') and parent.__unittest_skip__: sys.stderr.write("%s\n" % parent.__unittest_skip_why__) status = 'SKIP' done = True tcase_setup = None tcase_teardown = None if tcase_setup: status, expected = _try_call(tcase_setup) if status != 'OK': done = True tcase_teardown = None # if there's a setUp method, run it if not done and setup: status, expected = _try_call(setup) if status != 'OK': done = True if not done: status, expected2 = _try_call(getattr(parent, funcname)) if not done and teardown: tdstatus, expected3 = _try_call(teardown) if status == 'OK': status = tdstatus if tcase_teardown: _try_call(tcase_teardown) if mod_teardown: _try_call(mod_teardown) self.end_time = time.time() self.status = status self.err_msg = errstream.getvalue() self.memory_usage = get_memory_usage() self.expected_fail = expected or expected2 or expected3 if sys.platform == 'win32': self.load1m, self.load5m, self.load15m = (0.0, 0.0, 0.0) else: self.load1m, self.load5m, self.load15m = os.getloadavg() finally: stop_coverage() sys.stderr = old_err sys.stdout = old_out return self