def testSecondInterrupt(self):
# Can't use skipIf decorator because the signal handler may have
# been changed after defining this method.
if signal.getsignal(signal.SIGINT) == signal.SIG_IGN:
self.skipTest("test requires SIGINT to not be ignored")
result = unittest2.TestResult()
unittest2.installHandler()
unittest2.registerResult(result)
def test(result):
pid = os.getpid()
os.kill(pid, signal.SIGINT)
result.breakCaught = True
self.assertTrue(result.shouldStop)
os.kill(pid, signal.SIGINT)
self.fail("Second KeyboardInterrupt not raised")
try:
test(result)
except KeyboardInterrupt:
pass
else:
self.fail("Second KeyboardInterrupt not raised")
self.assertTrue(result.breakCaught)
python类skipIf()的实例源码
def testHandlerReplacedButCalled(self):
# Can't use skipIf decorator because the signal handler may have
# been changed after defining this method.
if signal.getsignal(signal.SIGINT) == signal.SIG_IGN:
self.skipTest("test requires SIGINT to not be ignored")
# If our handler has been replaced (is no longer installed) but is
# called by the *new* handler, then it isn't safe to delay the
# SIGINT and we should immediately delegate to the default handler
unittest2.installHandler()
handler = signal.getsignal(signal.SIGINT)
def new_handler(frame, signum):
handler(frame, signum)
signal.signal(signal.SIGINT, new_handler)
try:
pid = os.getpid()
os.kill(pid, signal.SIGINT)
except KeyboardInterrupt:
pass
else:
self.fail("replaced but delegated handler doesn't raise interrupt")
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 __getattr__(self, name):
"""Proxy all unknown attributes to the original method.
This is important for some of the decorators in the `unittest`
module, such as `unittest.skipIf`.
"""
return getattr(self.orig_method, name)
def __getattr__(self, name):
"""Proxy all unknown attributes to the original method.
This is important for some of the decorators in the `unittest`
module, such as `unittest.skipIf`.
"""
return getattr(self.orig_method, name)
def __getattr__(self, name):
"""Proxy all unknown attributes to the original method.
This is important for some of the decorators in the `unittest`
module, such as `unittest.skipIf`.
"""
return getattr(self.orig_method, name)
def test_skipping_decorators(self):
op_table = ((unittest2.skipUnless, False, True),
(unittest2.skipIf, True, False))
for deco, do_skip, dont_skip in op_table:
class Foo(unittest2.TestCase):
@deco(do_skip, "testing")
def test_skip(self):
pass
@deco(dont_skip, "testing")
def test_dont_skip(self):
pass
test_do_skip = Foo("test_skip")
test_dont_skip = Foo("test_dont_skip")
suite = unittest2.TestSuite([test_do_skip, test_dont_skip])
events = []
result = LoggingResult(events)
suite.run(result)
self.assertEqual(len(result.skipped), 1)
expected = ['startTest', 'addSkip', 'stopTest',
'startTest', 'addSuccess', 'stopTest']
self.assertEqual(events, expected)
self.assertEqual(result.testsRun, 2)
self.assertEqual(result.skipped, [(test_do_skip, "testing")])
self.assertTrue(result.wasSuccessful())
def skip_if_windows(reason):
"""Decorator to skip tests that should not be run on windows.
Example usage:
@skip_if_windows("Not valid")
def test_some_non_windows_stuff(self):
self.assertEqual(...)
"""
def decorator(func):
return unittest.skipIf(
platform.system() not in ['Darwin', 'Linux'], reason)(func)
return decorator
def skipIf(condition, reason):
if condition:
return skip(reason)
else:
return lambda item: item
def __getattr__(self, name):
"""Proxy all unknown attributes to the original method.
This is important for some of the decorators in the `unittest`
module, such as `unittest.skipIf`.
"""
return getattr(self.orig_method, name)
def __getattr__(self, name):
"""Proxy all unknown attributes to the original method.
This is important for some of the decorators in the `unittest`
module, such as `unittest.skipIf`.
"""
return getattr(self.orig_method, name)
def __getattr__(self, name):
"""Proxy all unknown attributes to the original method.
This is important for some of the decorators in the `unittest`
module, such as `unittest.skipIf`.
"""
return getattr(self.orig_method, name)
def skipIf(condition, reason):
if condition:
return skip(reason)
else:
return lambda item: item
def __getattr__(self, name):
"""Proxy all unknown attributes to the original method.
This is important for some of the decorators in the `unittest`
module, such as `unittest.skipIf`.
"""
return getattr(self.orig_method, name)
def __getattr__(self, name):
"""Proxy all unknown attributes to the original method.
This is important for some of the decorators in the `unittest`
module, such as `unittest.skipIf`.
"""
return getattr(self.orig_method, name)
def skipIf(condition, reason):
if condition:
return skip(reason)
else:
return lambda item: item
def skip_if_windows(reason):
"""Decorator to skip tests that should not be run on windows.
Example usage:
@skip_if_windows("Not valid")
def test_some_non_windows_stuff(self):
self.assertEqual(...)
"""
def decorator(func):
return unittest.skipIf(
platform.system() not in ['Darwin', 'Linux'], reason)(func)
return decorator
def skip_if_using_serial_implementation(reason):
"""Decorator to skip tests when running as the serial implementation"""
def decorator(func):
return unittest.skipIf(
is_serial_implementation(), reason)(func)
return decorator
def skipIf(condition, reason):
if condition:
return skip(reason)
else:
return lambda item: item
def test_saybig_with_color_codes(self):
self.parser.msgPrefix = "[Pre]"
with patch.object(self.parser.output, 'write') as write_mock:
self.parser.saybig("^7message ^1with ^2color ^8codes")
write_mock.assert_has_calls([call('sm_hsay [Pre] message with color codes')])
# @unittest.skipIf(WAS_FROSTBITE_LOADED, "Frostbite(1|2) parsers monkey patch the Client class and make this test fail")
def __getattr__(self, name):
"""Proxy all unknown attributes to the original method.
This is important for some of the decorators in the `unittest`
module, such as `unittest.skipIf`.
"""
return getattr(self.orig_method, name)
def SkipIfCppImplementation(func):
return unittest.skipIf(
api_implementation.Type() == 'cpp' and api_implementation.Version() == 2,
'C++ implementation does not expose unknown fields to Python')(func)
def test_total_lines(self):
""" Reader.total_lines test """
empty_reader = Reader(browser_config={})
self.assertIs(type(empty_reader.total_lines), int)
# def test_randomize_list_exception(self):
# """ Reader.randomize_list exception test """
#
# reader = Reader(browser_config={})
# setattr(reader, '_Reader__config', self.__configuration_for_exception)
# setattr(sys, '_Output__is_windows', False)
# with self.assertRaises(ReaderError) as context:
# reader.randomize_list('directories', 'tmplist')
# self.assertTrue(ReaderError == context.expected)
#
# @unittest.skipIf(True is sys().is_windows, "Skip test for windows")
# def test_randomize_list_unix(self):
# """ Reader.randomize_list unix test """
#
# reader = Reader(browser_config={
# 'is_external_wordlist': True,
# 'list': 'tests/data/directories.dat',
# })
# setattr(reader, '_Reader__config', self.__configuration)
# reader.count_total_lines()
# self.assertIsNone(reader.randomize_list('directories', 'tmplist'))
# fe = open('tests/data/directories.dat', 'r')
# fa = open('tests/tmp/list.tmp', 'r')
# expected = sum(1 for l in fe)
# actual = sum(1 for l in fa)
# self.assertIs(expected, actual)
#
# def test_randomize_list_windows(self):
# """ Reader.randomize_list windows test """
#
# reader = Reader(browser_config={
# 'is_external_wordlist': True,
# 'list': 'tests/data/directories.dat',
# })
# setattr(reader, '_Reader__config', self.__configuration)
# setattr(sys, 'is_windows', True)
# reader.count_total_lines()
# self.assertIsNone(reader.randomize_list('directories', 'tmplist'))
# fe = open('tests/data/directories.dat', 'r')
# fa = open('tests/tmp/list.tmp', 'r')
# expected = sum(1 for l in fe)
# actual = sum(1 for l in fa)
# self.assertIs(expected, actual)