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(BaseCase): pass @tagged('stock', 'slow') class Test_C(BaseCase): pass @tagged('standard', 'slow') class Test_D(BaseCase): 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.assertTrue(tags.check(stock_tag_obj)) tags = TagsSelector('slow,standard') self.assertTrue(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))
def test_selector_parser(self): """Test the parser part of the TagsSelector class""" tags = TagsSelector('+slow') self.assertEqual({ ('slow', None, None, None), }, tags.include) self.assertEqual(set(), tags.exclude) tags = TagsSelector('+slow,nightly') self.assertEqual( {('slow', None, None, None), ('nightly', None, None, None)}, tags.include) self.assertEqual(set(), tags.exclude) tags = TagsSelector('+slow,-standard') self.assertEqual({ ('slow', None, None, None), }, tags.include) self.assertEqual({ ('standard', None, None, None), }, tags.exclude) # same with space after the comma tags = TagsSelector('+slow, -standard') self.assertEqual({ ('slow', None, None, None), }, tags.include) self.assertEqual({ ('standard', None, None, None), }, tags.exclude) # same with space before and after the comma tags = TagsSelector('+slow , -standard') self.assertEqual({ ('slow', None, None, None), }, tags.include) self.assertEqual({ ('standard', None, None, None), }, tags.exclude) tags = TagsSelector('+slow ,-standard,+js') self.assertEqual({('slow', None, None, None), ('js', None, None, None)}, tags.include) self.assertEqual({ ('standard', None, None, None), }, tags.exclude) # without + tags = TagsSelector('slow, ') self.assertEqual({ ('slow', None, None, None), }, tags.include) self.assertEqual(set(), tags.exclude) # duplicates tags = TagsSelector('+slow,-standard, slow,-standard ') self.assertEqual({ ('slow', None, None, None), }, tags.include) self.assertEqual({ ('standard', None, None, None), }, tags.exclude) tags = TagsSelector('') self.assertEqual(set(), tags.include) self.assertEqual(set(), tags.exclude) tags = TagsSelector('/module') # all standard test of a module self.assertEqual({ ('standard', 'module', None, None), }, tags.include) self.assertEqual(set(), tags.exclude) tags = TagsSelector('*/module') # all tests of a module self.assertEqual({ (None, 'module', None, None), }, tags.include) self.assertEqual(set(), tags.exclude) tags = TagsSelector(':class') # all standard test of a class self.assertEqual({ ('standard', None, 'class', None), }, tags.include) self.assertEqual(set(), tags.exclude) tags = TagsSelector('.method') self.assertEqual({ ('standard', None, None, 'method'), }, tags.include) self.assertEqual(set(), tags.exclude) tags = TagsSelector(':class.method') self.assertEqual({ ('standard', None, 'class', 'method'), }, tags.include) self.assertEqual(set(), tags.exclude) tags = TagsSelector( '/module:class.method' ) # only a specific test func in a module (standard) self.assertEqual({ ('standard', 'module', 'class', 'method'), }, tags.include) self.assertEqual(set(), tags.exclude) tags = TagsSelector( '*/module:class.method') # only a specific test func in a module self.assertEqual({ (None, 'module', 'class', 'method'), }, tags.include) self.assertEqual(set(), tags.exclude) tags = TagsSelector('-/module:class.method' ) # disable a specific test func in a module self.assertEqual({ ('standard', None, None, None), }, tags.include) # all strandard self.assertEqual({ (None, 'module', 'class', 'method'), }, tags.exclude) # exept the test func tags = TagsSelector('-*/module:class.method') self.assertEqual({ ('standard', None, None, None), }, tags.include) self.assertEqual({ (None, 'module', 'class', 'method'), }, tags.exclude) tags = TagsSelector('tag/module') self.assertEqual({ ('tag', 'module', None, None), }, tags.include) self.assertEqual(set(), tags.exclude) tags = TagsSelector('tag.method') self.assertEqual({ ('tag', None, None, 'method'), }, tags.include) self.assertEqual(set(), tags.exclude) tags = TagsSelector( '*/module,-standard') # all non standard test of a module self.assertEqual({ (None, 'module', None, None), }, tags.include) # all in module self.assertEqual({ ('standard', None, None, None), }, tags.exclude) # exept standard ones
def test_selector_parser(self): """Test the parser part of the TagsSelector class""" tags = TagsSelector('+slow') self.assertEqual({ 'slow', }, tags.include) self.assertEqual(set(), tags.exclude) tags = TagsSelector('+slow,nightly') self.assertEqual({'slow', 'nightly'}, tags.include) self.assertEqual(set(), tags.exclude) tags = TagsSelector('+slow,-standard') self.assertEqual({ 'slow', }, tags.include) self.assertEqual({ 'standard', }, tags.exclude) # same with space after the comma tags = TagsSelector('+slow, -standard') self.assertEqual({ 'slow', }, tags.include) self.assertEqual({ 'standard', }, tags.exclude) # same with space befaore and after the comma tags = TagsSelector('+slow , -standard') self.assertEqual({ 'slow', }, tags.include) self.assertEqual({ 'standard', }, tags.exclude) tags = TagsSelector('+slow ,-standard,+js') self.assertEqual({ 'slow', 'js', }, tags.include) self.assertEqual({ 'standard', }, tags.exclude) tags = TagsSelector('slow, ') self.assertEqual({ 'slow', }, tags.include) self.assertEqual(set(), tags.exclude) tags = TagsSelector('+slow,-standard, slow,-standard ') self.assertEqual({ 'slow', }, tags.include) self.assertEqual({ 'standard', }, tags.exclude) tags = TagsSelector('') self.assertEqual(set(), tags.include) self.assertEqual(set(), tags.exclude)