From d076bb9230b50767f777863b74b31890afcd19cd Mon Sep 17 00:00:00 2001 From: Fabien BOURGEOIS Date: Tue, 23 Feb 2021 14:43:54 +0100 Subject: [PATCH] [ADD]Odoo SHELL helpers on GOLEM too --- odoo/golem/shell_helpers.py | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 odoo/golem/shell_helpers.py diff --git a/odoo/golem/shell_helpers.py b/odoo/golem/shell_helpers.py new file mode 100644 index 0000000..2d7c90c --- /dev/null +++ b/odoo/golem/shell_helpers.py @@ -0,0 +1,46 @@ +# -*- coding: utf-8 -*- + +# Copyright 2021 Fabien Bourgeois +# +# 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 . + +""" 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)