avocado.test: Complement compatibility with unittest.TestCase()

Make avocado.test.Test() to be compatible with unittest.TestCase(),
by making it take the methodName param, that will be largely for
the sake of compatibility. With this, we can run avocado tests
with unittest.main() and nosetests just fine:

$ nosetests tests/sleeptest/sleeptest.py
.
----------------------------------------------------------------------
Ran 1 test in 1.096s

OK

$ nosetests tests/failtest/failtest.py
E
======================================================================
ERROR: Run test method, for compatibility with unittest.TestCase.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/lmr/Code/avocado/avocado/test.py", line 180, in runTest
    self.action()
  File "/home/lmr/Code/avocado/tests/failtest/failtest.py", line 33, in
action
    raise exceptions.TestFail('This test is supposed to fail')
TestFail: This test is supposed to fail

----------------------------------------------------------------------
Ran 1 test in 0.088s

FAILED (errors=1)

In order to do this, the code in runTest was split to
an unhandled method, to be used by nosetests and the
unittest runner, and another one, to be used inside
avocado (run_avocado).
Signed-off-by: NLucas Meneghel Rodrigues <lmr@redhat.com>
上级 06378746
......@@ -77,7 +77,7 @@ class Job(object):
test_module = imp.load_module(url, f, p, d)
f.close()
test_class = getattr(test_module, url)
test_instance = test_class(name=url, base_logdir=self.debugdir)
test_instance = test_class(base_logdir=self.debugdir)
return test_instance
def run_test(self, url):
......@@ -85,7 +85,7 @@ class Job(object):
Run a single test URL.
"""
test_instance = self._load_test_instance(url)
test_instance.runTest()
test_instance.run_avocado()
return test_instance
def _make_test_result(self, urls):
......
......@@ -37,29 +37,39 @@ class Test(unittest.TestCase):
You'll inherit from this to write your own tests. Tipically you'll want
to implement setup(), action() and cleanup() methods on your own tests.
Test Attributes:
basedir:
Where the test .py file is located (root dir).
depsdir:
If this is an existing test suite wrapper, it'll contain the
test suite sources and other auxiliary files. Usually inside
basedir, 'deps' subdirectory.
workdir:
Place where temporary copies of the source code, binaries,
image files will be created and modified.
base_logdir:
Base log directory, where logs from all tests go to.
"""
def __init__(self, name, base_logdir=None, tag=None):
def __init__(self, methodName='runTest', base_logdir=None, tag=None):
"""
Initializes the test.
:param name: Test Name. Example: 'sleeptest'.
:param methodName: Name of the main method to run. For the sake of
compatibility with the original unittest class,
you should not set this.
:param base_logdir: Directory where test logs should go. If None
provided, it'll use ~/avocado.
:param tag: Tag that differentiates 2 executions of the same test name.
Example: 'long', 'short', so we can differentiate
'sleeptest.long' and 'sleeptest.short'.
Test Attributes:
basedir: Where the test .py file is located (root dir).
depsdir: If this is an existing test suite wrapper, it'll contain the
test suite sources and other auxiliary files. Usually inside
basedir, 'deps' subdirectory.
workdir: Place where temporary copies of the source code, binaries,
image files will be created and modified.
base_logdir: Base log directory, where logs from all tests go to.
Example: 'long', 'short', so we can differentiate
'sleeptest.long' and 'sleeptest.short'.
"""
self.name = name
self.name = self.__class__.__name__
self.tag = tag
self.basedir = os.path.join(data_dir.get_test_dir(), name)
self.basedir = os.path.join(data_dir.get_test_dir(), self.name)
self.depsdir = os.path.join(self.basedir, 'deps')
self.workdir = os.path.join(data_dir.get_tmp_dir(), self.name)
if not os.path.isdir(self.workdir):
......@@ -160,18 +170,24 @@ class Test(unittest.TestCase):
"""
Run test method, for compatibility with unittest.TestCase.
"""
sysinfo_logger = sysinfo.SysInfo(basedir=self.sysinfodir)
self.start_logging()
sysinfo_logger.start_job_hook()
try:
self.setup()
except Exception, details:
raise exceptions.TestSetupFail(details)
self.action()
self.cleanup()
self.status = 'PASS'
def run_avocado(self, result=None):
"""
Wraps the runTest metod, for execution inside the avocado runner.
"""
start_time = time.time()
try:
sysinfo_logger = sysinfo.SysInfo(basedir=self.sysinfodir)
self.start_logging()
sysinfo_logger.start_job_hook()
try:
self.setup()
except Exception, details:
raise exceptions.TestSetupFail(details)
self.action()
self.cleanup()
self.status = 'PASS'
self.runTest(result)
except exceptions.TestBaseException, detail:
self.status = detail.status
self.fail_reason = detail
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册