docker_images/odoo/golem/shell_helpers.py

47 lines
2.0 KiB
Python

# -*- coding: utf-8 -*-
# Copyright 2021 Fabien Bourgeois <fabien@yaltik.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
""" SHELL helpers """
from imp import reload
import unittest
def run_test(module, test_names='ALL', test_class_name='DEFAULT'):
""" Helper to allow testing of single method or all from TestCase
Takes module as python module, and names as strings
module is automatically reloaded for recent updates in shell
test_names can be only one string or an array of strings
test_class_name can be automatically found from module name """
module = reload(module)
suite = unittest.TestSuite()
if test_class_name == 'DEFAULT':
module_name = module.__name__.split('.')[-1]
test_class_name = ''.join(map(str.capitalize, module_name.split('_')))
if test_class_name not in dir(module):
raise Exception('Generated class name (%s) not found in module, please '
'specify' % test_class_name)
if test_names == 'ALL':
testCase = getattr(module, test_class_name)
tests = unittest.TestLoader().loadTestsFromTestCase(testCase)
suite.addTests(tests)
else:
if isinstance(test_names, str):
test_names = [test_names]
add = lambda test_name: suite.addTest(getattr(module, test_class_name)(test_name))
map(add, test_names)
unittest.TextTestRunner().run(suite)