我们从Python开源项目中,提取了以下42个代码示例,用于说明如何使用unittest.__file__()。
def run_tests(self): import unittest # Purge modules under test from sys.modules. The test loader will # re-import them from the build location. Required when 2to3 is used # with namespace packages. if sys.version_info >= (3,) and getattr(self.distribution, 'use_2to3', False): module = self.test_args[-1].split('.')[0] if module in _namespace_packages: del_modules = [] if module in sys.modules: del_modules.append(module) module += '.' for name in sys.modules: if name.startswith(module): del_modules.append(name) list(map(sys.modules.__delitem__, del_modules)) loader_ep = EntryPoint.parse("x="+self.test_loader) loader_class = loader_ep.load(require=False) cks = loader_class() unittest.main( None, None, [unittest.__file__]+self.test_args, testLoader = cks )
def run_tests(self): # Purge modules under test from sys.modules. The test loader will # re-import them from the build location. Required when 2to3 is used # with namespace packages. if PY3 and getattr(self.distribution, 'use_2to3', False): module = self.test_args[-1].split('.')[0] if module in _namespace_packages: del_modules = [] if module in sys.modules: del_modules.append(module) module += '.' for name in sys.modules: if name.startswith(module): del_modules.append(name) list(map(sys.modules.__delitem__, del_modules)) unittest_main( None, None, [unittest.__file__] + self.test_args, testLoader=self._resolve_as_ep(self.test_loader), testRunner=self._resolve_as_ep(self.test_runner), )
def test_file(self): fp = open(unittest.__file__) self.assertTrue(repr(fp).startswith( "<open file %r, mode 'r' at 0x" % unittest.__file__)) fp.close() self.assertTrue(repr(fp).startswith( "<closed file %r, mode 'r' at 0x" % unittest.__file__))
def test_module(self): eq = self.assertEqual touch(os.path.join(self.subpkgname, self.pkgname + os.extsep + 'py')) from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import areallylongpackageandmodulenametotestreprtruncation eq(repr(areallylongpackageandmodulenametotestreprtruncation), "<module '%s' from '%s'>" % (areallylongpackageandmodulenametotestreprtruncation.__name__, areallylongpackageandmodulenametotestreprtruncation.__file__)) eq(repr(sys), "<module 'sys' (built-in)>")
def prepare_test_env(): test_subdir = os.path.split(os.path.split(os.path.abspath(__file__))[0])[0] main_dir = os.path.split(test_subdir)[0] sys.path.insert(0, test_subdir) fake_test_subdir = os.path.join(test_subdir, 'run_tests__tests') return main_dir, test_subdir, fake_test_subdir
def test_module(self): eq = self.assertEqual touch(os.path.join(self.subpkgname, self.pkgname + os.extsep + 'py')) from areallylongpackageandmodulenametotestreprtruncation.areallylongpackageandmodulenametotestreprtruncation import areallylongpackageandmodulenametotestreprtruncation # On PyPy, we use %r to format the file name; on CPython it is done # with '%s'. It seems to me that %r is safer <arigo>. if '__pypy__' in sys.builtin_module_names: eq(repr(areallylongpackageandmodulenametotestreprtruncation), "<module %r from %r>" % (areallylongpackageandmodulenametotestreprtruncation.__name__, areallylongpackageandmodulenametotestreprtruncation.__file__)) else: eq(repr(areallylongpackageandmodulenametotestreprtruncation), "<module '%s' from '%s'>" % (areallylongpackageandmodulenametotestreprtruncation.__name__, areallylongpackageandmodulenametotestreprtruncation.__file__)) eq(repr(sys), "<module 'sys' (built-in)>")
def _iter_valid_frames(self, frames): """only consider non-testlib frames when formatting traceback""" lgc_testlib = osp.abspath(__file__) std_testlib = osp.abspath(unittest.__file__) invalid = lambda fi: osp.abspath(fi[1]) in (lgc_testlib, std_testlib) for frameinfo in dropwhile(invalid, frames): yield frameinfo
def datadir(cls): # pylint: disable=E0213 """helper attribute holding the standard test's data directory NOTE: this is a logilab's standard """ mod = __import__(cls.__module__) return osp.join(osp.dirname(osp.abspath(mod.__file__)), 'data') # cache it (use a class method to cache on class since TestCase is # instantiated for each test run)
def datadir(cls): # pylint: disable=E0213 """helper attribute holding the standard test's data directory NOTE: this is a logilab's standard """ mod = sys.modules[cls.__module__] return osp.join(osp.dirname(osp.abspath(mod.__file__)), 'data') # cache it (use a class method to cache on class since TestCase is # instantiated for each test run)
def run_test(module, **kwds): """Run a unit test module Recognized keyword arguments: incomplete, nosubprocess """ option_incomplete = kwds.get('incomplete', False) option_nosubprocess = kwds.get('nosubprocess', False) suite = unittest.TestSuite() test_utils.fail_incomplete_tests = option_incomplete m = import_submodule(module) if m.unittest is not unittest: raise ImportError( "%s is not using correct unittest\n\n" % module + "should be: %s\n is using: %s" % (unittest.__file__, m.unittest.__file__) ) print ('loading %s' % module) test = unittest.defaultTestLoader.loadTestsFromName(module) suite.addTest(test) output = StringIO.StringIO() runner = unittest.TextTestRunner(stream=output) results = runner.run(suite) output = StringIOContents(output) num_tests = results.testsRun failures = results.failures errors = results.errors tests = results.tests results = {module:from_namespace(locals(), RESULTS_TEMPLATE)} if not option_nosubprocess: print (TEST_RESULTS_START) print (pformat(results)) else: return results ################################################################################