[IMP]Odoo shell helpers function signature improved

This commit is contained in:
Fabien BOURGEOIS 2021-03-05 10:34:23 +01:00
parent d076bb9230
commit edff1295de
1 changed files with 22 additions and 4 deletions

View File

@ -20,21 +20,39 @@
from imp import reload from imp import reload
import unittest import unittest
def run_test(module, test_names='ALL', test_class_name='DEFAULT'): def run_test(module, test_names=False, test_class_name=''):
""" Helper to allow testing of single method or all from TestCase """ Helper to allow testing of single method or all from TestCase
Takes module as python module, and names as strings Takes module as python module, and names as strings
module is automatically reloaded for recent updates in shell module is automatically reloaded for recent updates in shell
test_names can be only one string or an array of strings test_names can be only one string or an array of strings
test_class_name can be automatically found from module name """ test_class_name can be automatically found from module name.
For example, from Odoo SHELL, first, import the modules :
>>> from shell_helpers import run_test
>>> from odoo.addons.addon.tests import test_file
Then, launch all tests from the module, and guessed className (here TestFile) :
>>> run_test(test_file)
Or launch only some tests :
>>> run_test(test_file, ['test_one', 'test_two'])
Or launch all tests from specific className :
>>> run_test(test_file, test_class_name='TestModelOne')
"""
module = reload(module) module = reload(module)
suite = unittest.TestSuite() suite = unittest.TestSuite()
if test_class_name == 'DEFAULT': if not test_class_name:
module_name = module.__name__.split('.')[-1] module_name = module.__name__.split('.')[-1]
test_class_name = ''.join(map(str.capitalize, module_name.split('_'))) test_class_name = ''.join(map(str.capitalize, module_name.split('_')))
if test_class_name not in dir(module): if test_class_name not in dir(module):
raise Exception('Generated class name (%s) not found in module, please ' raise Exception('Generated class name (%s) not found in module, please '
'specify' % test_class_name) 'specify' % test_class_name)
if test_names == 'ALL': if not test_names:
testCase = getattr(module, test_class_name) testCase = getattr(module, test_class_name)
tests = unittest.TestLoader().loadTestsFromTestCase(testCase) tests = unittest.TestLoader().loadTestsFromTestCase(testCase)
suite.addTests(tests) suite.addTests(tests)