avocado.test: Make base test class inherit from unittest.TestCase

Compatibility among avocado and regular python unittests
is one of the goals we want to accomplish with making
test cases inherit from the base python unittest TestCase
class. Also, we get the benefit of interoperating with
other known python test frameworks, such as nose.

In order to do that, we make the Test() class to inherit
from the base TestCase class, then move the _run_test_instance
method to the test class (as it makes a lot more sense
there anyway), and turn it into the default runTest() method.

With this, avocado tests gain automatically all features
of the unittest.TestCase() class, namely, the assert
methods.
Signed-off-by: NLucas Meneghel Rodrigues <lmr@redhat.com>
上级 17e35d16
...@@ -18,16 +18,12 @@ Class that describes a sequence of automated operations. ...@@ -18,16 +18,12 @@ Class that describes a sequence of automated operations.
import imp import imp
import logging import logging
import os import os
import sys
import time import time
import traceback
from avocado.core import data_dir from avocado.core import data_dir
from avocado.core import output from avocado.core import output
from avocado.core import exceptions
from avocado.core import status from avocado.core import status
from avocado import test from avocado import test
from avocado import sysinfo
from avocado import result from avocado import result
...@@ -84,52 +80,12 @@ class Job(object): ...@@ -84,52 +80,12 @@ class Job(object):
test_instance = test_class(name=url, base_logdir=self.debugdir) test_instance = test_class(name=url, base_logdir=self.debugdir)
return test_instance return test_instance
def _run_test_instance(self, test_instance):
"""
Call the test instance methods in the right order.
Along with the test methods, it also collects syinfo.
:params test_instance: avocado.test.Test derived class instance.
"""
start_time = time.time()
try:
sysinfo_logger = sysinfo.SysInfo(basedir=test_instance.sysinfodir)
test_instance.start_logging()
sysinfo_logger.start_job_hook()
try:
test_instance.setup()
except Exception, details:
raise exceptions.TestSetupFail(details)
test_instance.action()
test_instance.cleanup()
test_instance.status = 'PASS'
except exceptions.TestBaseException, detail:
test_instance.status = detail.status
test_instance.fail_reason = detail
except Exception, detail:
exc_type, exc_value, exc_traceback = sys.exc_info()
tb_info = traceback.format_exception(exc_type, exc_value,
exc_traceback.tb_next)
tb_info = "".join(tb_info)
for e_line in tb_info.splitlines():
test_instance.log.error(e_line)
test_instance.status = 'FAIL'
test_instance.fail_reason = detail
finally:
end_time = time.time()
test_instance.time_elapsed = end_time - start_time
test_instance.report()
test_instance.stop_logging()
return test_instance
def run_test(self, url): def run_test(self, url):
""" """
Run a single test URL. Run a single test URL.
""" """
test_instance = self._load_test_instance(url) test_instance = self._load_test_instance(url)
self._run_test_instance(test_instance) test_instance.runTest()
return test_instance return test_instance
def _make_test_result(self, urls): def _make_test_result(self, urls):
......
...@@ -19,12 +19,18 @@ framework tests. ...@@ -19,12 +19,18 @@ framework tests.
import logging import logging
import os import os
import sys
import time
import traceback
import unittest
from avocado.core import data_dir from avocado.core import data_dir
from avocado.core import exceptions from avocado.core import exceptions
from avocado.utils import process from avocado.utils import process
from avocado import sysinfo
class Test(object): class Test(unittest.TestCase):
""" """
Base implementation for the test class. Base implementation for the test class.
...@@ -77,6 +83,13 @@ class Test(object): ...@@ -77,6 +83,13 @@ class Test(object):
self.fail_reason = None self.fail_reason = None
self.time_elapsed = None self.time_elapsed = None
unittest.TestCase.__init__(self)
def __str__(self):
return str(self.name)
def __repr__(self):
return "Test(%r)" % self.tagged_name
def get_deps_path(self, basename): def get_deps_path(self, basename):
return os.path.join(self.depsdir, basename) return os.path.join(self.depsdir, basename)
...@@ -141,6 +154,43 @@ class Test(object): ...@@ -141,6 +154,43 @@ class Test(object):
""" """
pass pass
def runTest(self, result=None):
"""
Run test method, for compatibility with unittest.TestCase.
"""
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'
except exceptions.TestBaseException, detail:
self.status = detail.status
self.fail_reason = detail
except AssertionError, detail:
self.status = 'FAIL'
self.fail_reason = detail
except Exception, detail:
exc_type, exc_value, exc_traceback = sys.exc_info()
tb_info = traceback.format_exception(exc_type, exc_value,
exc_traceback.tb_next)
tb_info = "".join(tb_info)
for e_line in tb_info.splitlines():
self.log.error(e_line)
self.status = 'FAIL'
self.fail_reason = detail
finally:
end_time = time.time()
self.time_elapsed = end_time - start_time
self.report()
self.stop_logging()
def report(self): def report(self):
if self.fail_reason is not None: if self.fail_reason is not None:
self.log.error("%s %s -> %s: %s", self.status, self.log.error("%s %s -> %s: %s", self.status,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册