本文整理匯總了Python中unittest.py方法的典型用法代碼示例。如果您正苦於以下問題:Python unittest.py方法的具體用法?Python unittest.py怎麽用?Python unittest.py使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類unittest
的用法示例。
在下文中一共展示了unittest.py方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: find_tests
# 需要導入模塊: import unittest [as 別名]
# 或者: from unittest import py [as 別名]
def find_tests(testdir,
prefixes=DEFAULT_PREFIXES, suffix=".py",
excludes=(),
remove_suffix=True):
"""
Return a list of all applicable test modules.
"""
tests = []
for name in os.listdir(testdir):
if not suffix or name.endswith(suffix):
for prefix in prefixes:
if name.startswith(prefix):
if remove_suffix and name.endswith(suffix):
name = name[:-len(suffix)]
if name not in excludes:
tests.append(name)
tests.sort()
return tests
示例2: test___is_valid_py_file
# 需要導入模塊: import unittest [as 別名]
# 或者: from unittest import py [as 別名]
def test___is_valid_py_file(self):
isvalid = self.MyTestRunner._PydevTestRunner__is_valid_py_file
self.assertEqual(1, isvalid("test.py"))
self.assertEqual(0, isvalid("asdf.pyc"))
self.assertEqual(0, isvalid("__init__.py"))
self.assertEqual(0, isvalid("__init__.pyc"))
self.assertEqual(1, isvalid("asdf asdf.pyw"))
示例3: test___unixify
# 需要導入模塊: import unittest [as 別名]
# 或者: from unittest import py [as 別名]
def test___unixify(self):
unixify = self.MyTestRunner._PydevTestRunner__unixify
self.assertEqual("c:/temp/junk/asdf.py", unixify("c:SEPtempSEPjunkSEPasdf.py".replace('SEP', os.sep)))
示例4: test___importify
# 需要導入模塊: import unittest [as 別名]
# 或者: from unittest import py [as 別名]
def test___importify(self):
importify = self.MyTestRunner._PydevTestRunner__importify
self.assertEqual("temp.junk.asdf", importify("temp/junk/asdf.py"))
self.assertEqual("asdf", importify("asdf.py"))
self.assertEqual("abc.def.hgi", importify("abc/def/hgi"))
示例5: test_finding_a_file_from_file_system
# 需要導入模塊: import unittest [as 別名]
# 或者: from unittest import py [as 別名]
def test_finding_a_file_from_file_system(self):
test_file = "simple_test.py"
self.MyTestRunner.files_or_dirs = [self.file_dir[0] + test_file]
files = self.MyTestRunner.find_import_files()
self.assertEqual(1, len(files))
self.assertEqual(files[0], self.file_dir[0] + test_file)
示例6: test_finding_tests_when_no_filter
# 需要導入模塊: import unittest [as 別名]
# 或者: from unittest import py [as 別名]
def test_finding_tests_when_no_filter(self):
# unittest.py will create a TestCase with 0 tests in it
# since it just imports what is given
self.assertEqual(1, len(self.all_tests) > 0)
files_with_tests = [1 for t in self.all_tests if len(t._tests) > 0]
self.assertNotEqual(len(self.files), len(files_with_tests))
示例7: loadTestsFromName
# 需要導入模塊: import unittest [as 別名]
# 或者: from unittest import py [as 別名]
def loadTestsFromName(self, name, module=None):
parts = name.split('.')
if module is None or len(parts) > 2:
# let the base class do its job here
return [super(NonStrictTestLoader, self).loadTestsFromName(name)]
tests = self._collect_tests(module)
# import pprint
# pprint.pprint(tests)
collected = []
if len(parts) == 1:
pattern = parts[0]
if callable(getattr(module, pattern, None)) and pattern not in tests:
# consider it as a suite
return self.loadTestsFromSuite(module, pattern)
if pattern in tests:
# case python unittest_foo.py MyTestTC
klass, methodnames = tests[pattern]
for methodname in methodnames:
collected = [klass(methodname) for methodname in methodnames]
else:
# case python unittest_foo.py something
for klass, methodnames in list(tests.values()):
collected += [klass(methodname) for methodname in methodnames]
elif len(parts) == 2:
# case "MyClass.test_1"
classname, pattern = parts
klass, methodnames = tests.get(classname, (None, []))
for methodname in methodnames:
collected = [klass(methodname) for methodname in methodnames]
return collected
示例8: set_description
# 需要導入模塊: import unittest [as 別名]
# 或者: from unittest import py [as 別名]
def set_description(self, descr):
"""sets the current test's description.
This can be useful for generative tests because it allows to specify
a description per yield
"""
self._current_test_descr = descr
# override default's unittest.py feature
示例9: __call__
# 需要導入模塊: import unittest [as 別名]
# 或者: from unittest import py [as 別名]
def __call__(self, result=None, runcondition=None, options=None):
"""rewrite TestCase.__call__ to support generative tests
This is mostly a copy/paste from unittest.py (i.e same
variable names, same logic, except for the generative tests part)
"""
if result is None:
result = self.defaultTestResult()
result.pdbclass = self.pdbclass
# if self.capture is True here, it means it was explicitly specified
# in the user's TestCase class. If not, do what was asked on cmd line
self.capture = self.capture or getattr(result, 'capture', False)
self._options_ = options
self._printonly = getattr(result, 'printonly', None)
# if result.cvg:
# result.cvg.start()
testMethod = self._get_test_method()
if runcondition and not runcondition(testMethod):
return # test is skipped
result.startTest(self)
try:
if not self.quiet_run(result, self.setUp):
return
# generative tests
if is_generator(testMethod.__func__):
success = self._proceed_generative(result, testMethod, runcondition)
else:
status = self._proceed(result, testMethod)
success = (status == 0)
if not self.quiet_run(result, self.tearDown):
return
if success:
result.addSuccess(self)
finally:
# if result.cvg:
# result.cvg.stop()
result.stopTest(self)
示例10: create_files
# 需要導入模塊: import unittest [as 別名]
# 或者: from unittest import py [as 別名]
def create_files(paths, chroot):
"""creates directories and files found in <path>
:param path: list of relative paths to files or directories
:param chroot: the root directory in which paths will be created
>>> from os.path import isdir, isfile
>>> isdir('/tmp/a')
False
>>> create_files(['a/b/foo.py', 'a/b/c/', 'a/b/c/d/e.py'], '/tmp')
>>> isdir('/tmp/a')
True
>>> isdir('/tmp/a/b/c')
True
>>> isfile('/tmp/a/b/c/d/e.py')
True
>>> isfile('/tmp/a/b/foo.py')
True
"""
dirs, files = set(), set()
for path in paths:
path = osp.join(chroot, path)
filename = osp.basename(path)
# path is a directory path
if filename == '':
dirs.add(path)
# path is a filename path
else:
dirs.add(osp.dirname(path))
files.add(path)
for dirpath in dirs:
if not osp.isdir(dirpath):
os.makedirs(dirpath)
for filepath in files:
file(filepath, 'w').close()
示例11: test___is_valid_py_file
# 需要導入模塊: import unittest [as 別名]
# 或者: from unittest import py [as 別名]
def test___is_valid_py_file(self):
isvalid = self.MyTestRunner._PydevTestRunner__is_valid_py_file
self.assertEquals(1, isvalid("test.py"))
self.assertEquals(0, isvalid("asdf.pyc"))
self.assertEquals(0, isvalid("__init__.py"))
self.assertEquals(0, isvalid("__init__.pyc"))
self.assertEquals(1, isvalid("asdf asdf.pyw"))
示例12: test___unixify
# 需要導入模塊: import unittest [as 別名]
# 或者: from unittest import py [as 別名]
def test___unixify(self):
unixify = self.MyTestRunner._PydevTestRunner__unixify
self.assertEquals("c:/temp/junk/asdf.py", unixify("c:SEPtempSEPjunkSEPasdf.py".replace('SEP', os.sep)))
示例13: test___importify
# 需要導入模塊: import unittest [as 別名]
# 或者: from unittest import py [as 別名]
def test___importify(self):
importify = self.MyTestRunner._PydevTestRunner__importify
self.assertEquals("temp.junk.asdf", importify("temp/junk/asdf.py"))
self.assertEquals("asdf", importify("asdf.py"))
self.assertEquals("abc.def.hgi", importify("abc/def/hgi"))
示例14: test_finding_a_file_from_file_system
# 需要導入模塊: import unittest [as 別名]
# 或者: from unittest import py [as 別名]
def test_finding_a_file_from_file_system(self):
test_file = "simple_test.py"
self.MyTestRunner.files_or_dirs = [self.file_dir[0] + test_file]
files = self.MyTestRunner.find_import_files()
self.assertEquals(1, len(files))
self.assertEquals(files[0], self.file_dir[0] + test_file)
示例15: test_finding_tests_when_no_filter
# 需要導入模塊: import unittest [as 別名]
# 或者: from unittest import py [as 別名]
def test_finding_tests_when_no_filter(self):
# unittest.py will create a TestCase with 0 tests in it
# since it just imports what is given
self.assertEquals(1, len(self.all_tests) > 0)
files_with_tests = [1 for t in self.all_tests if len(t._tests) > 0]
self.assertNotEquals(len(self.files), len(files_with_tests))