Create method to load test case modules.

Change-Id: I0f8d79708bca80c3395289d395bf2d197ed02e42
This commit is contained in:
Federico Ressi 2018-12-21 14:51:32 +01:00
parent 0ef14525b7
commit b6d06dcdbb
3 changed files with 74 additions and 9 deletions

View File

@ -22,4 +22,5 @@ get_fixture = fixture_manager.FIXTURES.get
create_fixture = fixture_manager.FIXTURES.create
delete_fixture = fixture_manager.FIXTURES.delete
list_testcase_ids = testcase_manager.TESTCASES.list_ids
discover_testcases = testcase_manager.TESTCASES.discover
load_testcase_modules = testcase_manager.TESTCASES.load_modules

View File

@ -13,8 +13,17 @@
# under the License.
from __future__ import absolute_import
import importlib
import os
import sys
from oslo_log import log
from stestr import config_file
LOG = log.getLogger(__name__)
os.environ.setdefault('PYTHON', sys.executable)
class TestCaseManager(object):
@ -60,7 +69,42 @@ class TestCaseManager(object):
self.black_regex = black_regex
self.filters = filters
def list_ids(self, **kwargs):
def load_modules(self, **kwargs):
failed = {}
imported = {}
modules = []
for testcase_id in self.discover(**kwargs):
id_parts = testcase_id.split('.')
for i in range(len(id_parts)):
module_id = '.'.join(id_parts[:i + 1])
if module_id in failed:
# failed modules have no child modules to import
break
# import every module only once
module = imported.get(module_id)
if module is None:
try:
module = importlib.import_module(module_id)
except ImportError as ex:
failed[module_id] = ex
self.on_import_error(module_id)
# failed modules have no child modules to import
break
assert module_id == module.__name__
imported[module_id] = module
if is_leaf_module(module):
# leaf modules have no child modules to import
modules.append(module)
break
return modules
def on_import_error(self, test_module_id):
LOG.exception("Error importing module %r", test_module_id)
def discover(self, **kwargs):
"""Iterate over test_ids for a project
This method will print the test_ids for tests in a project. You can
filter the output just like with the run command to see exactly what
@ -89,6 +133,7 @@ class TestCaseManager(object):
top_dir=params['top_dir'])
not_filtered = filters is None and blacklist_file is None\
and whitelist_file is None and black_regex is None
try:
cmd.setUp()
# List tests if the fixture has not already needed to to filter.
@ -96,10 +141,18 @@ class TestCaseManager(object):
ids = cmd.list_tests()
else:
ids = cmd.test_ids
except SystemExit:
msg = ("Error discovering test cases IDs with parameters: "
"{!r}").format(params)
raise RuntimeError(msg)
finally:
cmd.cleanUp()
return ids
return sorted(ids)
def is_leaf_module(module):
return not os.path.basename(module.__file__).startswith('__init__.')
TESTCASES = TestCaseManager()

View File

@ -14,12 +14,13 @@
from __future__ import absolute_import
import os
import sys
from tobiko.tests import base
import tobiko
from tobiko.tests import unit
class TestCasesManagerTest(base.TobikoTest):
class TestCasesManagerTest(unit.TobikoUnitTest):
test_path = os.path.dirname(__file__)
@ -41,8 +42,18 @@ class TestCasesManagerTest(base.TobikoTest):
os.chdir(self.top_dir)
self.addCleanup(os.chdir, original_work_dir)
def test_list_testcase_ids(self):
testcases = tobiko.list_testcase_ids(test_path=self.test_path,
top_dir=self.top_dir,
repo_url=self.repo_url)
def test_discover(self):
testcases = tobiko.discover_testcases(test_path=self.test_path,
top_dir=self.top_dir,
repo_url=self.repo_url,
filters=[self.id()])
self.assertIn(self.id(), testcases)
def test_load_testcase_modules(self):
this_module = sys.modules[self.__module__]
modules = tobiko.load_testcase_modules(test_path=self.test_path,
top_dir=self.top_dir,
repo_url=self.repo_url,
filters=[self.id()])
self.assertIn(this_module, modules)