我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用unittest2.skip()。
def test_skipping(self): class Foo(unittest2.TestCase): def test_skip_me(self): self.skipTest("skip") events = [] result = LoggingResult(events) test = Foo("test_skip_me") test.run(result) self.assertEqual(events, ['startTest', 'addSkip', 'stopTest']) self.assertEqual(result.skipped, [(test, "skip")]) # Try letting setUp skip the test now. class Foo(unittest2.TestCase): def setUp(self): self.skipTest("testing") def test_nothing(self): pass events = [] result = LoggingResult(events) test = Foo("test_nothing") test.run(result) self.assertEqual(events, ['startTest', 'addSkip', 'stopTest']) self.assertEqual(result.skipped, [(test, "testing")]) self.assertEqual(result.testsRun, 1)
def test_skipping_subtests(self): class Foo(unittest.TestCase): def test_skip_me(self): with self.subTest(a=1): with self.subTest(b=2): self.skipTest("skip 1") self.skipTest("skip 2") self.skipTest("skip 3") events = [] result = LoggingResult(events) test = Foo("test_skip_me") test.run(result) self.assertEqual(events, ['startTest', 'addSkip', 'addSkip', 'addSkip', 'stopTest']) self.assertEqual(len(result.skipped), 3) subtest, msg = result.skipped[0] self.assertEqual(msg, "skip 1") self.assertIsInstance(subtest, unittest.TestCase) self.assertIsNot(subtest, test) subtest, msg = result.skipped[1] self.assertEqual(msg, "skip 2") self.assertIsInstance(subtest, unittest.TestCase) self.assertIsNot(subtest, test) self.assertEqual(result.skipped[2], (test, "skip 3"))
def test_skip_doesnt_run_setup(self): class Foo(unittest2.TestCase): wasSetUp = False wasTornDown = False def setUp(self): Foo.wasSetUp = True def tornDown(self): Foo.wasTornDown = True @unittest2.skip('testing') def test_1(self): pass result = unittest2.TestResult() test = Foo("test_1") suite = unittest2.TestSuite([test]) suite.run(result) self.assertEqual(result.skipped, [(test, "testing")]) self.assertFalse(Foo.wasSetUp) self.assertFalse(Foo.wasTornDown)
def test_decorated_skip(self): def decorator(func): def inner(*a): return func(*a) return inner class Foo(unittest2.TestCase): @decorator @unittest2.skip('testing') def test_1(self): pass result = unittest2.TestResult() test = Foo("test_1") suite = unittest2.TestSuite([test]) suite.run(result) self.assertEqual(result.skipped, [(test, "testing")])
def test_class_not_setup_or_torndown_when_skipped(self): class Test(unittest2.TestCase): classSetUp = False tornDown = False @classmethod def setUpClass(cls): Test.classSetUp = True @classmethod def tearDownClass(cls): Test.tornDown = True def test_one(self): pass Test = unittest2.skip("hop")(Test) self.runTests(Test) self.assertFalse(Test.classSetUp) self.assertFalse(Test.tornDown)
def skip(reason, condition=None): """ Mark a test case or method for skipping. @param reason: message @type reason: str @param condition: skip only if the specified condition is True @type condition: bool/expression """ if isinstance(reason, types.FunctionType): raise TypeError('skip: no reason specified') if condition is None: return unittest.skip(reason) else: return unittest.skipIf(condition, reason)
def init_plugin(self, config_content=None): conf = None if config_content: conf = XmlConfigParser() conf.setXml(config_content) elif ADV_CONFIG_CONTENT: conf = XmlConfigParser() conf.setXml(ADV_CONFIG_CONTENT) else: unittest.skip("cannot get default plugin config file at %s" % ADV_CONFIG_FILE) self.p = AdvPlugin(self.console, conf) self.p.save = Mock() self.conf = self.p.config self.log.setLevel(logging.DEBUG) self.log.info("============================= Adv plugin: loading config ============================") self.p.onLoadConfig() self.log.info("============================= Adv plugin: starting =================================") self.p.onStartup()
def test_old_testresult_class(self): class Test(unittest2.TestCase): def testFoo(self): pass Test = unittest2.skip('no reason')(Test) self.assertOldResultWarning(Test('testFoo'), 0)
def test_skip_class(self): class Foo(unittest2.TestCase): def test_1(self): record.append(1) # was originally a class decorator... Foo = unittest2.skip("testing")(Foo) record = [] result = unittest2.TestResult() test = Foo("test_1") suite = unittest2.TestSuite([test]) suite.run(result) self.assertEqual(result.skipped, [(test, "testing")]) self.assertEqual(record, [])
def test_skip_non_unittest_class_old_style(self): @unittest.skip("testing") class Mixin: def test_1(self): record.append(1) class Foo(Mixin, unittest.TestCase): pass record = [] result = unittest.TestResult() test = Foo("test_1") suite = unittest.TestSuite([test]) suite.run(result) self.assertEqual(result.skipped, [(test, "testing")]) self.assertEqual(record, [])
def requires_resource(resource): if resource == 'gui' and not _is_gui_available(): return unittest.skip("resource 'gui' is not available") if is_resource_enabled(resource): return _id else: return unittest.skip("resource {0!r} is not enabled".format(resource))
def impl_detail(msg=None, **guards): if check_impl_detail(**guards): return _id if msg is None: guardnames, default = _parse_guards(guards) if default: msg = "implementation detail not available on {0}" else: msg = "implementation detail specific to {0}" guardnames = sorted(guardnames.keys()) msg = msg.format(' or '.join(guardnames)) return unittest.skip(msg)
def skip_unless_symlink(test): """Skip decorator for tests that require functional symlink""" ok = can_symlink() msg = "Requires functional symlink implementation" return test if ok else unittest.skip(msg)(test)
def skip_unless_xattr(test): """Skip decorator for tests that require functional extended attributes""" ok = can_xattr() msg = "no non-broken extended attribute support" return test if ok else unittest.skip(msg)(test)
def skip(reason): def decorator(test_item): if isinstance(test_item, type) and issubclass(test_item, unittest.TestCase): class skip_wrapper(test_item): def setUp(self): raise SkipTest(reason) else: @functools.wraps(test_item) def skip_wrapper(*args, **kwargs): raise SkipTest(reason) return skip_wrapper return decorator
def skipIf(condition, reason): if condition: return skip(reason) else: return lambda item: item
def skipUnless(condition, reason): if condition: return lambda item: item else: return skip(reason)
def skip_unless_has_memory_collection(cls): """Class decorator to skip tests that require memory collection. Any test that uses memory collection (such as the resource leak tests) can decorate their class with skip_unless_has_memory_collection to indicate that if the platform does not support memory collection the tests should be skipped. """ if platform.system() not in ['Darwin', 'Linux']: return unittest.skip('Memory tests only supported on mac/linux.')(cls) return cls
def requires_xarray(test): return test if has_xarray else unittest.skip('requires xarray')(test)
def requires_dask(test): return test if has_dask else unittest.skip('requires dask')(test)
def requires_numba(test): return test if has_numba else unittest.skip('requires numba')(test)
def requires_matplotlib(test): return test if has_matplotlib else unittest.skip('requires matplotlib')(test)
def requires_bottleneck(test): return test if has_bottleneck else unittest.skip('requires bottleneck')(test)