def run_unit_tests(module_name, dbname, position='at_install'): """ :returns: ``True`` if all of ``module_name``'s tests succeeded, ``False`` if any of them failed. :rtype: bool """ global current_test from odoo.tests.common import TagsSelector # Avoid import loop current_test = module_name mods = get_test_modules(module_name) threading.currentThread().testing = True config_tags = TagsSelector(tools.config['test_tags']) position_tag = TagsSelector(position) r = True for m in mods: tests = unwrap_suite(unittest.TestLoader().loadTestsFromModule(m)) suite = unittest.TestSuite(t for t in tests if position_tag.check(t) and config_tags.check(t)) if suite.countTestCases(): t0 = time.time() t0_sql = odoo.sql_db.sql_counter _logger.info('%s running tests.', m.__name__) result = unittest.TextTestRunner(verbosity=2, stream=TestStream(m.__name__)).run(suite) if time.time() - t0 > 5: _logger.log(25, "%s tested in %.2fs, %s queries", m.__name__, time.time() - t0, odoo.sql_db.sql_counter - t0_sql) if not result.wasSuccessful(): r = False _logger.error("Module %s: %d failures, %d errors", module_name, len(result.failures), len(result.errors)) current_test = None threading.currentThread().testing = False return r
def run_unit_tests(module_name, position='at_install', openupgrade_prefix=None): """ :param openupgrade_prefix: extension to be able to insert '.migrations' to the path of test files to be loaded (expecting tests in '<module_name>/migrations/tests/test_migration.py') :returns: ``True`` if all of ``module_name``'s tests succeeded, ``False`` if any of them failed. :rtype: bool """ global current_test from odoo.tests.common import TagsSelector # Avoid import loop current_test = module_name mods = get_test_modules(module_name, openupgrade_prefix=openupgrade_prefix) threading.currentThread().testing = True config_tags = TagsSelector(tools.config['test_tags'] or '') position_tag = TagsSelector(position) r = True for m in mods: tests = unwrap_suite(unittest.TestLoader().loadTestsFromModule(m)) suite = unittest.TestSuite(t for t in tests if position_tag.check(t) and config_tags.check(t)) if suite.countTestCases(): t0 = time.time() t0_sql = odoo.sql_db.sql_counter name = (openupgrade_prefix + '.' if openupgrade_prefix else '') + 'tests' _logger.info('%s running %s.', m.__name__, name) result = OdooTestRunner().run(suite) if time.time() - t0 > 5: _logger.log(25, "%s tested in %.2fs, %s queries", m.__name__, time.time() - t0, odoo.sql_db.sql_counter - t0_sql) if not result.wasSuccessful(): r = False _logger.error("Module %s: %d failures, %d errors", module_name, len(result.failures), len(result.errors)) current_test = None threading.currentThread().testing = False return r
def run_unit_tests(module_name, position='at_install'): """ :returns: ``True`` if all of ``module_name``'s tests succeeded, ``False`` if any of them failed, ``None`` if no tests were ran. :rtype: bool | None """ global current_test # avoid dependency hell from odoo.tests.common import TagsSelector, OdooSuite current_test = module_name mods = get_test_modules(module_name) threading.currentThread().testing = True config_tags = TagsSelector(tools.config['test_tags']) position_tag = TagsSelector(position) ran_tests = failures = False for m in mods: tests = unwrap_suite(unittest.TestLoader().loadTestsFromModule(m)) suite = OdooSuite(t for t in tests if position_tag.check(t) and config_tags.check(t)) if suite.countTestCases(): t0 = time.time() t0_sql = odoo.sql_db.sql_counter _logger.info('%s running tests.', m.__name__) result = OdooTestRunner().run(suite) log_level = logging.INFO if time.time() - t0 > 5: log_level = logging.RUNBOT _logger.log(log_level, "%s ran %s tests in %.2fs, %s queries", m.__name__, result.testsRun, time.time() - t0, odoo.sql_db.sql_counter - t0_sql) ran_tests = True if not result.wasSuccessful(): failures = True _logger.error("Module %s: %d failures, %d errors", module_name, len(result.failures), len(result.errors)) current_test = None threading.currentThread().testing = False if failures: return False if ran_tests: return True return None
def test_selector_selection(self): """Test check_tags use cases""" class Test_A(TransactionCase): pass @tagged('stock') class Test_B(): pass @tagged('stock', 'slow') class Test_C(): pass @tagged('standard', 'slow') class Test_D(): pass @tagged('-at_install', 'post_install') class Test_E(TransactionCase): pass no_tags_obj = Test_A() stock_tag_obj = Test_B() multiple_tags_obj = Test_C() multiple_tags_standard_obj = Test_D() post_install_obj = Test_E() # if 'standard' in not explicitly removed, tests without tags are # considered tagged standard and they are run by default if # not explicitly deselected with '-standard' or if 'standard' is not # selectected along with another test tag # same as "--test-tags=''" parameters: tags = TagsSelector('') self.assertFalse(tags.check(no_tags_obj)) # same as "--test-tags '+slow'": tags = TagsSelector('+slow') self.assertFalse(tags.check(no_tags_obj)) # same as "--test-tags '+slow,+fake'": tags = TagsSelector('+slow,fake') self.assertFalse(tags.check(no_tags_obj)) # same as "--test-tags '+slow,+standard'": tags = TagsSelector('slow,standard') self.assertTrue(no_tags_obj) # same as "--test-tags '+slow,-standard'": tags = TagsSelector('slow,-standard') self.assertFalse(tags.check(no_tags_obj)) # same as "--test-tags '-slow,-standard'": tags = TagsSelector('-slow,-standard') self.assertFalse(tags.check(no_tags_obj)) # same as "--test-tags '-slow,+standard'": tags = TagsSelector('-slow,+standard') self.assertTrue(tags.check(no_tags_obj)) tags = TagsSelector('') self.assertFalse(tags.check(stock_tag_obj)) tags = TagsSelector('slow') self.assertFalse(tags.check(stock_tag_obj)) tags = TagsSelector('standard') self.assertFalse(tags.check(stock_tag_obj)) tags = TagsSelector('slow,standard') self.assertFalse(tags.check(stock_tag_obj)) tags = TagsSelector('slow,-standard') self.assertFalse(tags.check(stock_tag_obj)) tags = TagsSelector('+stock') self.assertTrue(tags.check(stock_tag_obj)) tags = TagsSelector('stock,fake') self.assertTrue(tags.check(stock_tag_obj)) tags = TagsSelector('stock,standard') self.assertTrue(tags.check(stock_tag_obj)) tags = TagsSelector('-stock') self.assertFalse(tags.check(stock_tag_obj)) tags = TagsSelector('') self.assertFalse(tags.check(multiple_tags_obj)) tags = TagsSelector('-stock') self.assertFalse(tags.check(multiple_tags_obj)) tags = TagsSelector('-slow') self.assertFalse(tags.check(multiple_tags_obj)) tags = TagsSelector('slow') self.assertTrue(tags.check(multiple_tags_obj)) tags = TagsSelector('slow,stock') self.assertTrue(tags.check(multiple_tags_obj)) tags = TagsSelector('-slow,stock') self.assertFalse(tags.check(multiple_tags_obj)) tags = TagsSelector('slow,stock,-slow') self.assertFalse(tags.check(multiple_tags_obj)) tags = TagsSelector('') self.assertFalse(tags.check(multiple_tags_standard_obj)) tags = TagsSelector('standard') self.assertTrue(tags.check(multiple_tags_standard_obj)) tags = TagsSelector('slow') self.assertTrue(tags.check(multiple_tags_standard_obj)) tags = TagsSelector('slow,fake') self.assertTrue(tags.check(multiple_tags_standard_obj)) tags = TagsSelector('-slow') self.assertFalse(tags.check(multiple_tags_standard_obj)) tags = TagsSelector('-standard') self.assertFalse(tags.check(multiple_tags_standard_obj)) tags = TagsSelector('-slow,-standard') self.assertFalse(tags.check(multiple_tags_standard_obj)) tags = TagsSelector('standard,-slow') self.assertFalse(tags.check(multiple_tags_standard_obj)) tags = TagsSelector('slow,-standard') self.assertFalse(tags.check(multiple_tags_standard_obj)) # Mimic the real post_install use case # That uses a second tags selector tags = TagsSelector('standard') position = TagsSelector('post_install') self.assertTrue(tags.check(post_install_obj) and position.check(post_install_obj))