提交 64767826 编写于 作者: A Amador Pahim

Don't execute setUp()/tearDown() on SKIP

Skip decorators are intended to skip the test. Skipping the test means
also no setUp() and no tearDown(). This patch makes the setUp() and
tearDown() to be also skipped when using the skip decorators.
Signed-off-by: NAmador Pahim <apahim@redhat.com>
上级 863789af
......@@ -65,6 +65,7 @@ def skip(message=None):
def wrapper(*args, **kwargs):
raise core_exceptions.TestDecoratorSkip(message)
function = wrapper
function.__skip_test_decorator__ = True
return function
return decorator
......
......@@ -559,14 +559,18 @@ class Test(unittest.TestCase):
cleanup_exception = None
stdout_check_exception = None
stderr_check_exception = None
skip_test = getattr(testMethod, '__skip_test_decorator__', False)
try:
self.setUp()
if skip_test is False:
self.setUp()
except (exceptions.TestSetupSkip,
exceptions.TestDecoratorSkip,
exceptions.TestTimeoutSkip,
exceptions.TestSkipError) as details:
stacktrace.log_exc_info(sys.exc_info(), logger='avocado.test')
raise exceptions.TestSkipError(details)
except exceptions.TestDecoratorSkip as details:
stacktrace.log_exc_info(sys.exc_info(), logger='avocado.test')
raise exceptions.TestSkipError(details)
except exceptions.TestCancel as details:
stacktrace.log_exc_info(sys.exc_info(), logger='avocado.test')
skip_illegal_msg = ('Calling cancel() in setUp() '
......@@ -605,7 +609,8 @@ class Test(unittest.TestCase):
self.log.debug(' -> %s %s: %s', key, type(value), value)
finally:
try:
self.tearDown()
if skip_test is False:
self.tearDown()
except exceptions.TestSetupSkip as details:
stacktrace.log_exc_info(sys.exc_info(), logger='avocado.test')
skip_illegal_msg = ('Calling skip() in places other than '
......@@ -615,8 +620,9 @@ class Test(unittest.TestCase):
raise exceptions.TestError(skip_illegal_msg)
except exceptions.TestDecoratorSkip as details:
stacktrace.log_exc_info(sys.exc_info(), logger='avocado.test')
skip_illegal_msg = ('Using skip decorators after the test '
'will have no effect, you must fix your '
skip_illegal_msg = ('Using skip decorators in tearDown() '
'is not allowed in '
'avocado, you must fix your '
'test. Original skip exception: %s' %
details)
raise exceptions.TestError(skip_illegal_msg)
......
......@@ -76,7 +76,7 @@ Avocado supports the most common exit statuses:
be nice to review. (some result plugins does not support this and report
``PASS`` instead)
* ``SKIP`` - the test's pre-requisites were not satisfied and the test's
body was not executed (nor its ``tearDown``)
body was not executed (nor its ``setUp()`` and ``tearDown``).
* ``FAIL`` - test did not result in the expected outcome. A failure points
at a (possible) bug in the tested subject, and not in the test itself.
When the test (and its) execution breaks, an ``ERROR`` and not a ``FAIL``
......@@ -1036,9 +1036,8 @@ Will produce the following result::
Notice the ``test3`` was not skipped because the provided condition was
not ``False``.
Using the skip decorators, since the `setUp()` was already executed, the
`tearDown()` will be also executed.
Using the skip decorators, nothing is actually executed. We will skip
the `setUp()` method, the test method and the `tearDown()` method.
Cancelling Tests
================
......
......@@ -18,31 +18,25 @@ from lib_skip_decorators import check_condition
class AvocadoSkipTests(avocado.Test):
def setUp(self):
self.log.info('setup executed')
@avocado.skip('Test skipped')
def test1(self):
pass
self.log.info('test executed')
@avocado.skipIf(check_condition(True),
'Skipped due to the True condition')
def test2(self):
pass
self.log.info('test executed')
@avocado.skipUnless(check_condition(False),
'Skipped due to the False condition')
def test3(self):
pass
@avocado.skipIf(False)
def test4(self):
pass
@avocado.skipUnless(True)
def test5(self):
pass
self.log.info('test executed')
@avocado.skip()
def test6(self):
pass
def tearDown(self):
self.log.info('teardown executed')
"""
......@@ -54,6 +48,34 @@ def check_condition(condition):
"""
AVOCADO_SKIP_DECORATOR_SETUP = """
import avocado
class AvocadoSkipTests(avocado.Test):
@avocado.skip('Test skipped')
def setUp(self):
pass
def test1(self):
pass
"""
AVOCADO_SKIP_DECORATOR_TEARDOWN = """
import avocado
class AvocadoSkipTests(avocado.Test):
def test1(self):
pass
@avocado.skip('Test skipped')
def tearDown(self):
pass
"""
class TestSkipDecorators(unittest.TestCase):
def setUp(self):
......@@ -69,6 +91,18 @@ class TestSkipDecorators(unittest.TestCase):
self.test_lib = script.Script(lib_path, AVOCADO_TEST_SKIP_LIB)
self.test_lib.save()
skip_setup_path = os.path.join(self.tmpdir,
'test_skip_decorator_setup.py')
self.skip_setup = script.Script(skip_setup_path,
AVOCADO_SKIP_DECORATOR_SETUP)
self.skip_setup.save()
bad_teardown_path = os.path.join(self.tmpdir,
'test_skip_decorator_teardown.py')
self.bad_teardown = script.Script(bad_teardown_path,
AVOCADO_SKIP_DECORATOR_TEARDOWN)
self.bad_teardown.save()
def test_skip_decorators(self):
os.chdir(basedir)
cmd_line = ['./scripts/avocado',
......@@ -80,9 +114,41 @@ class TestSkipDecorators(unittest.TestCase):
'--json -']
result = process.run(' '.join(cmd_line), ignore_status=True)
json_results = json.loads(result.stdout)
debuglog = json_results['debuglog']
self.assertEqual(result.exit_status, exit_codes.AVOCADO_ALL_OK)
self.assertEquals(json_results['skip'], 3)
self.assertFalse('setup executed' in open(debuglog, 'r').read())
self.assertFalse('test executed' in open(debuglog, 'r').read())
self.assertFalse('teardown executed' in open(debuglog, 'r').read())
def test_skip_setup(self):
os.chdir(basedir)
cmd_line = ['./scripts/avocado',
'run',
'--sysinfo=off',
'--job-results-dir',
'%s' % self.tmpdir,
'%s' % self.skip_setup,
'--json -']
result = process.run(' '.join(cmd_line), ignore_status=True)
json_results = json.loads(result.stdout)
self.assertEqual(result.exit_status, exit_codes.AVOCADO_ALL_OK)
self.assertEquals(json_results['pass'], 2)
self.assertEquals(json_results['skip'], 4)
self.assertEquals(json_results['skip'], 1)
def test_skip_teardown(self):
os.chdir(basedir)
cmd_line = ['./scripts/avocado',
'run',
'--sysinfo=off',
'--job-results-dir',
'%s' % self.tmpdir,
'%s' % self.bad_teardown,
'--json -']
result = process.run(' '.join(cmd_line), ignore_status=True)
json_results = json.loads(result.stdout)
self.assertEqual(result.exit_status, exit_codes.AVOCADO_TESTS_FAIL)
self.assertEquals(json_results['errors'], 1)
def tearDown(self):
shutil.rmtree(self.tmpdir)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册